   [java] Tic Tac Toe Game Help!
  Hey im sort of new to java. i took a class in school called JAVA and it talked about the basics. And know that the class is over i wanted to create something on my own and i decided to try and make tic tac toe the game.. it started out going good but once i made the board and set the turns i got stuck  . I can't figure out how to tell which player won the game and i also want to cheat proof my game , you can click the box twice and make it go from an x to an o or vice versa. Your help would be greatly appreciated and if you could could you explain what your doing so i can do it right the next time i try. Thanks for your help!Scott.import javax.swing.*;import BreezySwing.*;import java.awt.*;import java.awt.event.*;public class Tictactoe extends GBFrame{   //Declare varialbe for the window objects   private JLabel title;   private JButtontopleft;   private JButton topmiddle;   private JButton  topright;   private JButton middleleft;   private JButton middlemiddle;   private JButton middleright;   private JButton bottomleft;   private JButton bottommiddle;   private JButton bottomright;   private JButton newgame;   private int x, y=0; //constructor   public Tictactoe(){ //instantiate and add window objects to the window title = addLabel ("Tic Tac Toe" ,1,2,1,1);  topleft= addButton(" "   ,2,1,1,1); topmiddle = addButton  (" "   ,2,2,1,1); topright = addButton  (" "   ,2,3,1,1); middleleft = addButton  (" "   ,3,1,1,1); middlemiddle    = addButton  (" "   ,3,2,1,1); middleright    = addButton  (" "   ,3,3,1,1); bottomleft = addButton  (" "   ,4,1,1,1); bottommiddle    = addButton  (" "   ,4,2,1,1); bottomright    = addButton  (" "   ,4,3,1,1); newgame    = addButtom  ("New Game"  , 5,2,1,1);   } //respond to button click events public void buttonClicked (JButton buttonObj){   if (y=o){ y=1;   }    if (buttonObj == topleft){ if ((x%2)==o){    topleft.setText("X"); }else{    topleft.setText("O"); } x+=1;   }   if (buttonObj == topmiddle){ if ((x%2)==o){    topmiddle.setText("X"); }else{    topmiddle.setText("O"); } x+=1;   } if (buttonObj == topright){ if ((x%2)==o){    topright.setText("X"); }else{    topright.setText("O"); } x+=1;   }    if (buttonObj == middleleft){ if ((x%2)==o){    middleleft.setText("X"); }else{    middleleft.setText("O"); } x+=1;   }    if (buttonObj == middlemiddle){ if ((x%2)==o){    middlemiddle.setText("X"); }else{    middlemiddle.setText("O"); } x+=1;   }    if (buttonObj == middleright){ if ((x%2)==o){    middleright.setText("X"); }else{    middleright.setText("O"); } x+=1;   }   if (buttonObj == bottomleft){ if ((x%2)==o){    bottomleft.setText("X"); }else{    bottomleft.setText("O"); } x+=1;   }    if (buttonObj == bottommiddle){ if ((x%2)==o){    bottommiddle.setText("X"); }else{    bottommiddle.setText("O"); } x+=1;   }    if (buttonObj == bottomright){ if ((x%2)==o){    bottomright.setText("X"); }else{    bottomright.setText("O"); } x+=1;   }   if (buttonObj == newgame){ topleft.setText(" "); topmiddle.setText(" "); topright.setText(" "); middleleft.setText(" "); middlemiddle.setText(" "); middleright.setText(" "); bottomleft.setText(" "); bottommiddle.setText(" "); bottomright.setText(" ");  x = 0;   } } public static void main (String[] args){   Tictactoe theGUI = new Tictactoe(); theGUI.setSize (275, 205); theGUI.setLookAndFeel("Metal"); theGUI.setVisible (true);   }   }   
  I would look into game loops and write your program that way.Basically if you think about it players take it in turns, and each turn is one run through the game loop. Within the loop you generally take player inputs, calculate their effect and render the results to the screen.Due to your code structure, in a way your buttonClicked() can act as your game loop without much alteration.To stop people from clicking a box twice simply add a test at the start of your buttonPressed() such as:if(!buttonObj.getText().equals(" ")) {    return;}This checks whether or not the button is in its default state, and if not breaks out of the loopThis will not make any modifications and preserve the game status, though it will have to go before your button tests and after your newgame test(otherwise the new game will simply return also).Now you can cut down on a lot of code. You know that the button isnt the newgame button, and you also know that its valid to be clicked so you can get rid of all the messy if(button = x) stuff and just do:if ((x%2)==o){  buttonObj.setText("X");}else{  buttonObj.setText("O");}And that will deal with all you game buttons :)To check for winners, this should be done after each game loop (ie at the end of your buttonPressed() method). There are a few ways to do this. First you could get rid of all the x+=1s and at the end of the loop put something likeif(checkForWinner(x)) {  showWinner(x);else {  x+=1;}You'd probably want to do some other stuff to tidy up after showing the winner but this is good enough for now. As for deciding if someone has won, notice that there are only 8 ways to win in this game (8 possible lines). So given your ability I don't think it would be that bad if you simply hard-coded all the tests. For exampleboolean checkForWinner(x) {String player;if ((x%2)==o){  player ="X";}else{  player ="O";}if(topleft.getText().equals(player)&&topmiddle.getText().equals(player)&&topright.getText().equals(player)) {  return true;}//etc etcreturn false;}Bear in mind that since only player x has pressed a button the last turn, only player x can have made themselves winner so you only have to check for the players whos turn it wasHope that helps a bit :)   ;  Why are you testing all nine buttons? Why not just change the text on the button you press to "X"? Nine times less code to do the same thing. [grin]   ;  Thanks for the help ALOT! i understand the top part of your reply about making it so they can't click the same spot twice and it works also how to shorten up the code. But the part i dont understand is the part that has to do with the who won, i dont really get where to put it in and the checkForWinner thing doesnt make sense.. i mean would i have to put that last bit of code you showed be at the end of my buttonPressed()? This is what it looks like so far..I have like 2 cannot resolve symbol errors i dont understand whats wrong.import javax.swing.*;import BreezySwing.*;import java.awt.*;import java.awt.event.*;public class Test extends GBFrame{//Declare varialbe for the window objectsprivate JLabeltitle;private JButton  topleft;private JButtontopmiddle;private JButton   topright;private JButtonmiddleleft;private JButtonmiddlemiddle;private JButtonmiddleright;private JButtonbottomleft;private JButtonbottommiddle;private JButtonbottomright;private JButtonnewgame;private int x, y, showWinner;private String checkForWinner; //constructorpublic Test(){//instantiate and add window objects to the windowtitle = addLabel("Tic Tac Toe" ,1,2,1,1); topleft    = addButton  (" ",2,1,1,1);topmiddle= addButton (" ",2,2,1,1);topright= addButton (" ",2,3,1,1);middleleft= addButton (" ",3,1,1,1);middlemiddle = addButton (" ",3,2,1,1);middleright = addButton (" ",3,3,1,1);bottomleft= addButton (" ",4,1,1,1);bottommiddle = addButton (" ",4,2,1,1);bottomright = addButton (" ",4,3,1,1);newgame= addButton ("New Game"  ,5,2,1,1);} //respond to button click events public void buttonClicked (JButton buttonObj){if (y==0){y=1;}if(!buttonObj.getText().equals(" ")){    return;} if ((x%2)==0){  buttonObj.setText("X");}else{  buttonObj.setText("O");} if(checkForWinner(x)){showWinner(x);}else{x+=1;}if (buttonObj == newgame){topleft.setText(" ");topmiddle.setText(" ");topright.setText(" ");middleleft.setText(" ");middlemiddle.setText(" ");middleright.setText(" ");bottomleft.setText(" ");bottommiddle.setText(" ");bottomright.setText(" "); x = 0;}}public static void main (String[] args){ Test theGUI = new Test();theGUI.setSize (275, 205);theGUI.setLookAndFeel("Metal");theGUI.setVisible (true);} }   ;  Ok first thing, you've got to put your newgame test first in the buttonClicked() method, before the if(!buttonObj.getText().equals(" ")) bit. Otherwise if someone clicks the new game button it will fail the test and nothing will happen (since the text on the button doesn't equal " ").I would imagine that your cannot resolve symbol errors are because you haven't written the two methods to deal with a player winning.The two statements: checkForWinner(x) and showWinner(x) are method calls, presumably you are aware of methods. You could just put all the code in one chunk if you wanted, but its a bit cleaner this way.The first method, checkForWinner(x) would have the signatureprivate boolean checkForWinner(x) {//method code}and within that you write the method code which checks to see if theres a winner. Then when it gets to that point in the program the code within that method will be run and the result returned to where it was called from. Look again at the last bit of code I posted, it should make sense with a bit of thought. The other method showWinner(x) was just going to be something that indicates to the players who has won, by putting a message on the screen or whatever. But if you want you could just put the code in with the rest of it   ;  Gah that was me, damn place keeps logging me out :(   ;  Well, after clicking a button, the game needs to check if the player has won or not with his new move. The Method described by vyper does this by checking all possible winning cases (check for every possible line by comparing the text in the buttons). This method call is put at the end of the button click detection (and after you make the changes to the text in the button). If there is a winner, declare him/her.   ;  Okay i think im getting it .. so in this bit of codeif(topleft.getText().equals(player)&&topmiddle.getText().equals(player)&&topright.getText().equals(player)) {  return true;}//etc etcreturn false;}after the return true; } i put another if but with another combination of winning boxes?? and then for the showWinner one i put a box that pops up in the game telling them X or O won the game? and where would i put that code? at the bottom of my buttonClicked bit?And also thanks alot for your help so far!!ohh and i just found that theres this error asking for a ; after  boolean checkForWinner(x) {  its pointing in between Winner and the (x)..   ;  You're right about the first part, as for the second it seems you don't understand method signatures fully. I will try to explain quickly, but it would be a good idea to read up on them.Now say I have a method with the signature:public boolean checkForWinner(int x) This is what all of it means:public: This is how visible the method is. Don't worry about this bit too much at the moment, just use public for all your methods for now. If you understand variable access (eg private JButton topLeft) this is essentially the same but for methods.boolean: This is the return type of the method, the kind of value that a calling program can expect to get back from the method. It can be anything, primitives (int boolean etc), objects (JButton, ArrayList etc), nothing at all (void) or arrays of the previous (int[], JButton[][] etc)checkForWinner: this is simply the name of the method, programs use this name to access the method(int x): This is where you're going wrong. This bit is where you tell the method what parameters to use, and give them a name. Because Java is strongly typed, you must also tell java the type of the parameters. So here I am saying that I will pass in an int and I want to reference it as x within the method. If you want multiple inputs, seperate them with a comma eg public void testMethod(int x, short y, String z)Now how to use your method. When you put the code :if(checkForWinner(x))what you are doing is passing the parameter x into the method checkForWinner. Since checkForWinner() returns a boolean we can simply use the method as a boolean. So checkForWinner() will either return true or false depending on the input and this equates to simply writing if(true) or if(false)As for where you would put the code, you have to put it outside your buttonClicked method since it will be a seperate method. Java programs can have many, many methods and they are written in blocks defined by curly brackets, for example here are two seperate methods written in succession:public void foo() {//foo methodSystem.out.println("in foo");}public void bar() {//bar methodSystem.out.println("in bar");}   