Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 16th, 2010, 10:59 AM
spithaz spithaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 12 spithaz User rank is Sergeant (500 - 2000 Reputation Level)spithaz User rank is Sergeant (500 - 2000 Reputation Level)spithaz User rank is Sergeant (500 - 2000 Reputation Level)spithaz User rank is Sergeant (500 - 2000 Reputation Level)spithaz User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 h 12 m 5 sec
Reputation Power: 0
Homework - TicTacToe Applet, reset function

Description lies within the code.

I want the game to reset when i call the startup(), that's why all that isn't in the init().

There is no check for wins, losses, ties yet.

The problem is that when i press the reset button the game freezes. The buttons keep their labels and are disabled.

My first approach was to have TicTacToe class implementing ActionListener and the 2nd actionPerformed like this:

Code:
public void actionPerformed(ActionEvent e){
		
			if(e.getSource() == reset)
				startup();
			else
			for(int x = 0; x < button.length; x++){
....


but that resulted to the same problem


Code Below:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*	This is a simple Tic Tac Toe Applet.
 *	This applet focuses on a 2 player game
 *	of Tic Tac Toe, there is no AI implemented.
 *
 *	@author 
 *
 */

public class TicTacToe extends JApplet {

	public JPanel grid, north;
	public JLabel lScore, lTurn;
	public int xScore, oScore, ties, count = 0;
	public boolean bTurn = true;
	public JButton reset;
	
	public JButton[] button = new JButton[9];
	

	/*	This could also be in the init() method but this way
	 *	i can call on the startup() method to reset the grid
	 *	and keep the scores.
	 */
	public void startup(){
		
		grid = new JPanel(new GridLayout(3,3));
		lTurn = new JLabel(" It's X's turn");
		north = new JPanel();
		reset = new JButton("Reset");
		reset.addActionListener(new Reset());
		
		
		for(int x = 0; x < 9; x++){
			button[x] = new JButton("");
			button[x].addActionListener(new Button());
			button[x].setFont(new Font("sansserif",Font.BOLD,100));
			grid.add(button[x]);
		}
		
		
		add(north, BorderLayout.NORTH);
		add(grid, BorderLayout.CENTER);
		add(lTurn, BorderLayout.SOUTH);
		north.add(reset, BorderLayout.NORTH);
		north.add(lScore, BorderLayout.CENTER);
			
		
		setSize(500, 500);
	}

	class Reset implements ActionListener{
		public void actionPerformed(ActionEvent e){
			startup();
		}
	}
	
	class Button implements ActionListener{
		public void actionPerformed(ActionEvent e){
		
			for(int x = 0; x < button.length; x++){
				if(e.getSource() == button[x]){
					System.out.println("Button "+ x + " has just been pressed");
					symbolCheck();
				
			
					if(bTurn == true){
						button[x].setText("X");
						lTurn.setText(" It's O's turn");
					}
					else{
						button[x].setText("O");
						lTurn.setText(" It's X's turn");
					}
					button[x].setEnabled(false);
				
				}
			}
		}
	}
	
	/*	This method checks what symbol should be set as text
	 *	on the button that has been clicked.
	 *	If you divide count by 2 and the result is not an int (contains decimals)
	 *	then it sets the text to "X", if the result is an int
	 *	it then sets it to "O".
	 */
	public boolean symbolCheck(){
		if(count==0 || count%2==0){
			bTurn = true;
			++count;
		}
		else{
			bTurn = false;
			++count;
		}
		
		return bTurn;
	}
	
	public void winCheck(){
	
		
	
	
	}
	
	public void init(){
	
		
		lScore = new JLabel(" X won " + xScore + " times    O won "+ oScore +" times    Tied Games: "+ ties);
		startup();
		
	}
}


I just need something to reset the grid but keep the score.
It's probably something minor that i can't see but i'd appreciate any help.

thanks in advance
/Spithaz

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - TicTacToe Applet, reset function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap