Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: (Beginner/Intermediate) Tic Tac Toe game issues

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (Beginner/Intermediate) Tic Tac Toe game issues

    Hello all, been having some issues with this tic-tac-toe game as the title stated. For some reason it seems that a player cannot place an X or O in any but three of the positions. I had this working before, but then I went to code a 'checkwin' method and suddenly it no longer works. I'm completely baffled because this method is in no way required by the game to function.

    Also, it seems as if the checkwin method does not want to be called in the driver class. It displays a "cannot find symbol" error even though I made sure to make the method public.

    This is the driver file:

    import java.util.Scanner;
    import javax.swing.JOptionPane; 
    /**
       This program runs a TicTacToe game. It prompts the
       user to set positions on the board and prints out the
       result.
    */
    public class TicTacToeRunner
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          String player = "x";
          int row = 0;
          int column = 0; 
          String s1;
          String s2;
          int turn = 1;
          TicTacToe game = new TicTacToe();
          boolean done = false;
          while (!done)
          {
             System.out.print(game.toString()); 
     
             s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit");
             row = Integer.parseInt(s1);
             if (row == 0) done = true;
             else{
             while(row < 1 || row > 3)
             {
                 if (row == 0) done = true;
             else{
                     s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit");
             row = Integer.parseInt(s1);
             }
             }
             }
     
                s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit");
             column = Integer.parseInt(s1);
             if (column == 0) done = true;
             else{
             while(column < 1 || row > 3)
             {
                 if (column == 0) done = true;
             else{
                     s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit");
             column = Integer.parseInt(s1);
                 }
             }
             }
                 System.out.println("-----");
                 turn++;
                 game.set(row - 1, column - 1, player);
                 checkWin(turn);
                if (player.equals("x")) 
                   player = "o"; 
                else 
                   player = "x";    
     
          }
       }
    }

    This is the game file:

    import javax.swing.JOptionPane;
    /**
       A 3 x 3 tic-tac-toe board.
    */
    public class TicTacToe
    {
       private String[][] board;
       private static final int ROWS = 3;
       private static final int COLUMNS = 3;
     
       /**
          Constructs an empty board.
       */
       public TicTacToe()
       {
          board = new String[ROWS][COLUMNS];
          // Fill with spaces
          for (int i = 0; i < ROWS; i++)
             for (int j = 0; j < COLUMNS; j++)
                board[i][j] = " ";
       }
     
       /**
          Sets a field in the board. The field must be unoccupied.
          @param i the row index 
          @param j the column index 
          @param player the player ("x" or "o")
       */
       public void set(int i, int j, String player)
       {
     
          if (board[i][j].equals(" "))
             board[i][j] = player;
       }
     
       public void checkWin(int turn)	{
           boolean win = false;
           String message;
     
     
    		if (turn > 4)	{
    			if(board[1][1] == "x"){
                                if (board[1][2] == "x"){
                                    if (board[1][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[1][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[2][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[2][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[2][2] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[2][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[3][1] == "x"){
                                if (board[3][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][3] == "o"){
                                            if (board[3][2] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][1] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][2] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][2] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][2] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][2] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][3] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][3] == "o"){
                                            if (board[2][3] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
     
     
     
                    }
                    }
                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                    }
     
     
     
     
    		if(win || (!win && turn>9))	{
    			if(win)	{
    				if(turn % 2 == 0)
    					message = "X has won!";
    				else	
    					message = "O has won!";
                                    JOptionPane.showMessageDialog(null, message);
    				win = false;
    			}	else if(!win && turn>9)	{
    				message = "Both players have tied!\nBetter luck next time.";
                                    JOptionPane.showMessageDialog(null, message);
    			}
     
     
    		}
    	}
     
     
       /**
          Creates a string representation of the board, such as
          |x  o|
          |  x |
          |   o|
          @return the string representation
       */
       public String toString()
       {
          String r = "";
          for (int i = 0; i < ROWS; i++)
          {
             r = r + "|";
             for (int j = 0; j < COLUMNS; j++)         
                r = r + board[i][j];
             r = r + "|\n";
          }
          return r;
       }
    }

    Any help would be appreciated at this point. I'm a bit of a java noob so I have a feeling these are simple errors for you but I've been stuck on this for hours.

    Thanks in advance for any help.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: (Beginner/Intermediate) Tic Tac Toe game issues

    ...well, this is just speculation, but maybe it has something to do with this mess:
    if (turn > 4)	{
    			if(board[1][1] == "x"){
                                if (board[1][2] == "x"){
                                    if (board[1][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[1][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[2][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[2][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[2][2] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[2][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[3][1] == "x"){
                                if (board[3][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][3] == "o"){
                                            if (board[3][2] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][1] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][2] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][2] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][2] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][2] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][3] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][3] == "o"){
                                            if (board[2][3] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
     
     
     
                    }
                    }
                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                    }
    Firstly, I think you can clean up your if statements by using the && operator (scroll down page of link a little) instead of multiple if statements. Also, I think you have either a) WAAAAAAAAAAAAY too many }'s at the end of this block of code or b) they are all misplaced. I think it may be a combination of the two.

    Note that the syntax for if-else-if is:
    boolean a, b, c;
     
    //This is right
    if(a)
    {
       //code
    }
    else if(b)
    {
       //other code
    }
    else if(c)
    {
       //more code
    }
    else
    {
       //yet more code
    }

    and NOT THIS:

    boolean a, b, c;
     
    //This is wrong...
    if(a)
    {
       //code
     
    else if(b)
    {
       //other code
     
    else if(c)
    {
       //more code
     
    else
    {
       //yet more code
    }
    }
    }
    }
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner/Intermediate) Tic Tac Toe game issues

    Found out the error with the game functionality itself was a typo in the driver file which I have fixed. I redid the game file like this:
    (please ignore the commented out part lol)
    import javax.swing.JOptionPane;
    /**
       A 3 x 3 tic-tac-toe board.
    */
    public class TicTacToe
    {
       private String[][] board;
       private static final int ROWS = 3;
       private static final int COLUMNS = 3;
     
       /**
          Constructs an empty board.
       */
       public TicTacToe()
       {
          board = new String[ROWS][COLUMNS];
          // Fill with spaces
          for (int i = 0; i < ROWS; i++)
             for (int j = 0; j < COLUMNS; j++)
                board[i][j] = " ";
       }
     
       /**
          Sets a field in the board. The field must be unoccupied.
          @param i the row index 
          @param j the column index 
          @param player the player ("x" or "o")
       */
       public void set(int i, int j, String player)
       {
     
          if (board[i][j].equals(" "))
             board[i][j] = player;
       }
      /* 
       public void checkWin(int turn)	{
           boolean win = false;
           String message;
     
     
    		if (turn > 4)	{
    			if(board[1][1] == "x"){
                                if (board[1][2] == "x"){
                                    if (board[1][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[1][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[2][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[2][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[2][2] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[2][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[3][1] == "x"){
                                if (board[3][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][3] == "o"){
                                            if (board[3][2] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][1] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][1] == "o"){
                                                if (board[3][1] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][2] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][2] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][2] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][2] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][3] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][3] == "o"){
                                            if (board[2][3] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][1] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][3] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[1][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[3][3] == "o"){
                                        win = true;
                                    }
                                                else{
     
     
                                                if(board[1][3] == "x"){
                                if (board[2][2] == "x"){
                                    if (board[3][1] == "x"){
                                        win = true;
     
    		}
                                    else{
                                        if(board[3][1] == "o"){
                                            if (board[2][2] == "o"){
                                                if (board[1][3] == "o"){
                                        win = true;
                                    }
     
     
     
                    }
                    }
                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                    }
     
     
     
     
    		if(win || (!win && turn>9))	{
    			if(win)	{
    				if(turn % 2 == 0)
    					message = "X has won!";
    				else	
    					message = "O has won!";
                                    JOptionPane.showMessageDialog(null, message);
    				win = false;
    			}	else if(!win && turn>9)	{
    				message = "Both players have tied!\nBetter luck next time.";
                                    JOptionPane.showMessageDialog(null, message);
    			}
     
     
    		}
    	}
     
    */
       /**
          Creates a string representation of the board, such as
          |x  o|
          |  x |
          |   o|
          @return the string representation
       */
     
       private void checkWin() {
     
     
     
        // Check horizontal
     
        for (int i = 0; i < board.length; i++) {
     
            boolean rowWin = true;
     
            for (int j = 0; j < board[i].length; j++) {
     
                if (board[j][i] != player)
     
                    rowWin = false;
     
            }
     
            if (rowWin == true) {
     
                JOptionPane.showMessageDialog
     
                (null, "A Winner is you!",
     
                "Congratulations " + player, JOptionPane.INFORMATION_MESSAGE);
     
                return;
     
            }
     
        }
        // Check vertical
     
        for (int i = 0; i < game.length; i++) {
     
            boolean rowWin = true;
     
            for (int j = 0; j < game[i].length; j++) {
     
                if (game[i][j] != player)
     
                    rowWin = false;
     
            }
     
            if (rowWin == true) {
     
                JOptionPane.showMessageDialog
     
                (null, "A Winner is you!",
     
                "Congratulations " + player, JOptionPane.INFORMATION_MESSAGE);
     
                return;
     
            }
     
        }
     
        // Check diagonal
     
        boolean topWin = true;
     
        boolean bottomWin = true;
     
        for (int i = 0; i < board.length; i++) {
     
            if (board[i][i] != player)
     
                topWin = false;
     
            if (board[(board.length-(i+1))][i] != board)
     
                bottomWin = false;
     
        }
     
        if (topWin == true || bottomWin == true) {
     
            JOptionPane.showMessageDialog
     
                (null, "A Winner is you!",
     
                "Congratulations " + player, JOptionPane.INFORMATION_MESSAGE);
     
            return;
     
        }
     
        // If the array is full and there is no winner, it's a cat's game
     
        boolean isFull = true;
     
        for (int i = 0; i < board.length; i++)
     
            for (int j = 0; j < board[i].length; j++)
     
                if (board[i][j] == 0)
     
                    isFull = false;
     
        if (isFull) {
     
     
     
            JOptionPane.showMessageDialog
     
                (null, "Cat's game!",
     
                "Tie", JOptionPane.INFORMATION_MESSAGE);
     
            return;
     
        }
     
     
     
    }
     
       public String toString()
       {
          String r = "";
          for (int i = 0; i < ROWS; i++)
          {
             r = r + "|";
             for (int j = 0; j < COLUMNS; j++)         
                r = r + board[i][j];
             r = r + "|\n";
          }
          return r;
       }
    }

    However whenever I go to call the 'checkwin' method in the driver, it continues to display a "cannot find symbol." Also i'm trying to use the variable 'player' in order to display who won the game under the checkwin method but this is displaying the same error. It's as if the two files are for some reason not communicating. Have you ever had this error?

    Here is the fixed version of the driver file if you want it:

    import java.util.Scanner;
    import javax.swing.JOptionPane; 
    /**
       This program runs a TicTacToe game. It prompts the
       user to set positions on the board and prints out the
       result.
    */
    public class TicTacToeRunner
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          String player = "x";
          int row = 0;
          int column = 0; 
          String s1;
          String s2;
          int turn = 1;
          TicTacToe game = new TicTacToe();
          boolean done = false;
          while (!done)
          {
             System.out.print(game.toString()); 
     
             s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit");
             row = Integer.parseInt(s1);
             if (row == 0) done = true;
             else{
             while(row < 1 || row > 3)
             {
                 if (row == 0) done = true;
             else{
                     s1 = JOptionPane.showInputDialog(null, player + " Please input a row from 1 - 3 or 0 to quit");
             row = Integer.parseInt(s1);
             }
             }
             }
     
                s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit");
             column = Integer.parseInt(s2);
             if (column == 0) done = true;
             else{
             while(column < 1 || row > 3)
             {
                 if (column == 0) done = true;
             else{
                     s2 = JOptionPane.showInputDialog(null, player + " Please input a column from 1 - 3 or 0 to quit");
             column = Integer.parseInt(s2);
                 }
             }
             }
                 System.out.println("-----");
                 turn++;
                 game.set(row - 1, column - 1, player);
                 checkWin();
                if (player.equals("x")) 
                   player = "o"; 
                else 
                   player = "x";    
     
          }
       }
    }

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: (Beginner/Intermediate) Tic Tac Toe game issues

    I'm new at java, but you should do what snowguy13 told you, he just wants to help, and you should organize your programm and comment it properly so that anyone who sees the code can understand it easily and fast

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,352
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: (Beginner/Intermediate) Tic Tac Toe game issues

    if(board[1][1] == "x"){

    You should use the equals() method for comparing Strings' contents, not the == operator.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: (Beginner/Intermediate) Tic Tac Toe game issues

    Please post full error messages you receive here, not just error names.
    In addition to Snowguy's response, the OR ( || ) operator would clean it up alot more.

    One place where cannot find symbol 'player' would occur is within TicTacToe's checkWin() method.
    You haven't declared the variable player with a global scope in that class, and nor have you with a local scope so a variable player cannot be found.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Replies: 17
    Last Post: December 23rd, 2011, 04:38 PM
  2. Beginner Java Game Developement
    By grimrader22 in forum Java Theory & Questions
    Replies: 7
    Last Post: November 30th, 2011, 08:25 PM
  3. Replies: 3
    Last Post: October 20th, 2011, 12:55 AM
  4. Beginner. Need Help finishing a simple Shoot em up game ASAP!!!!
    By cbock55 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 09:13 AM
  5. error with a java game..(beginner)
    By Skat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2010, 03:04 PM