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 11 of 11

Thread: Need help with Logic of a Tic Tac Toe game.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help with Logic of a Tic Tac Toe game.

     
    /**
     * 
     * CSC 225 - Online
     * Tic Tac Toe
     */
     
     
    public class TicTacToe
    {
     
     
    public static void main(String[] args)
    {
     
     
     
        gameBoard(); 
     
    }
     
     
     
    public static void gameBoard( )
    {
     
        int[][] test = new int [3][3];
     
        int i, j;
     
        System.out.println( " Tic Tac Toe Game " );
     
        for (i = 0; i < 3; i++) 
        {
            System.out.println("----------------");
            for(j = 0; j < 3; j++)
            {
                System.out.print("|    " );
            }
            System.out.println("|      ");
        }
        System.out.println( "----------------"); 
     
        System.out.println(" ");
     
    }
     
     
     
    }

    I need help problem solving. I'm really struggling with my logic on creating my Tic Tac Toe game. I have a game board but I need to take the next step of creating input for the game. Can anyone coach me? Thanks Again!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    step of creating input for the game.
    Please explain what that means? Are you trying to get input from the user? The Scanner class has methods that will read user input from the console.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    Thanks for the replay Norm appreciated as always!

    What I'm having trouble understanding is creating a logic for my tic tac toe game. I have a rough draft pesudo code on whats going to happen. So I would like if you could help me understand the logic when I create the application. Here is what I have?


    Display Game board

    -Choose cell array

    -display game board

    -Allow next player to choose cell

    -display game board

    -Repeat until cells equal each other

    or

    -Cells are all occupied.


    Am I on the right track of this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    Where is the beginning of the loop that is being repeated?
    Where is a winning position tested for?
    Are there two players or does the computer play one side?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    Okay I will try to answer two of your questions. Cause I'm still trying to figure out the logic of my game. I learn a bit better when I write my code.
    Here is what I written so far I made a bit of progress since your last post.

    /**
     * 
     * CSC 225 - Online
     * Tic Tac Toe
     */
     
     
    import java.util.*;
     
    public class TicTacToe
    {
     
     
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
     
        int test[][] = new int[3][3];
     
     
        System.out.println( " Tic Tac Toe Game " );
     
        //Displays board for user
        for (int i = 0; i < 3; i++) 
        {
            System.out.println("----------------");
            for(int j = 0; j < 3; j++)
            {
                System.out.print("|    "  );
            }
            System.out.println("|      ");
        }
        System.out.println( "----------------"); 
        System.out.println(" ");
     
        //Runs method for move of player
        moveGame(test);
        gameLogic(test);
     
    }
     
       public static void moveGame( int [][] gameInput)
    {
     
        Scanner scan = new Scanner(System.in);
     
        int test[][] = new int[3][3];
     
        System.out.print( " Please input your move. \" ROW first followed by SPACE and then COLUMN 2nd.\" " );
     
     
     
        for( int row = 0; row < gameInput.length; row++)
        {
            for(int col = 0; col < gameInput[row].length; col++)
            {
                gameInput[row][col] = scan.nextInt();
            }
        }
     
    } 
     
     
    public static void gameLogic(int moveArray[][])
    {
     
     
        System.out.println(moveArray[1][1] + "X");
     
     
     
     
     
    }
     
     
     
     
    public static void gameBoard( int a [] [])
    {
     
        int[][] test = new int [3][3];
     
        int i, j;
     
        System.out.println( " Tic Tac Toe Game " );
     
        for (i = 0; i < test.length; i++) 
        {
            System.out.println("----------------");
            for(j = 0; j < test[i].length; j++)
            {
                System.out.print("|    "  + a );
            }
            System.out.println("|      ");
        }
        System.out.println( "----------------"); 
     
        System.out.println(" ");
     
    }
     
     
     
    public static boolean checkForWinner(char[][] b)
       {
        // checkForWinner() method determines if a pattern of data stored
        // in the 2 D char array indicates the a player has won the game.
     
            boolean flag = false;
            boolean flag1 = false;
            boolean flag2 = false;
            boolean flag3 = false;
            boolean flag4 = false;
     
            // checks the contents of each row for matching data   
            for (int i = 0; i <= 2; i++)
            {
                if ((b[i][0] == b[i][1] && b[i][1] == b[i][2]) && b[i][2] != ' ') 
                    flag1 = true;
            }
     
             // checks the contents of each column for matching data
            for (int j = 0; j <= 2; j++)
            {
                if ((b[0][j] == b[1][j] && b[1][j] == b[2][j]) && b[2][j] != ' ') 
                    flag2 = true;
            }
     
            // checks the contents of one diagonal for matching data
            if ((b[0][0] == b[1][1] && b[1][1] == b[2][2]) && b[2][2] != ' ') 
                    flag3 = true;
     
            // checks the contents of the other diagonal for matching data
            if ((b[0][2] == b[1][1] && b[1][1] == b[2][0]) && b[2][0] != ' ') 
                    flag4 = true;
     
            // checks if any of the previous conditions evaluated to true        
            if (flag1 == true || flag2 == true || flag3 == true || flag4 == true)
                flag = true;
     
           // returns true if a winner was found; returns false is no winner     
           return flag;
       } // end of checkForWinner method
     
     
    }

    For the "winning position" our professor wrote the method for us. I guess she didn't want us to struggle to much on it. But my question is I need help creating a method to evaluate my move. For example I want to accept the users array input. From accepting the array input I want to associate a string input lets say "X" to my gameBoard. For example I chose an array input of 0[row] and 1[column]. I then want my string "X" to show on that gameBoard from that array position. Do you have any suggestions? By the way this is a two player game. We will work on that later.

  6. #6
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    I have made great progress in my tic tac toe game. I need help creating a for loop for my game. I have many methods that I need to figure out how to make them work as one. I have a method for gameboard, playerOne, playerTwo and checkForWinner(Which my professor created for my class. She wanted us to use this method.). Do you have any suggestions on how I can finish my program? Or if I should modify my code a bit.

     
    /**
     * 
     * CSC 225 - Online
     * Tic Tac Toe
     */
     
     
    import java.util.*;
     
    public class TicTacToe
    {
     
     
    public static void main(String[] args)
    {
      Scanner scan = new Scanner(System.in);
     
        String board[][] = new String[3][3];
     
        for ( int x = 0 ; x < board.length; x++)
        {
            for( int y = 0; y < board[x].length; y++)
            {
                board[x][y] = "";
            }
        }
     
     
     
    }
     
     
     
    public static void playerOneMove (String gameInput[][])
       {
     
        Scanner scan = new Scanner(System.in);
     
        String test[][] = new String [3][3];
     
        int row = 0;
        int column = 0;
     
        System.out.print( " Please input your row? " );
        row = scan.nextInt() - 1;
     
        System.out.print( " Please input your column? " );
        column = scan.nextInt() - 1;
     
        gameInput[row][column] = "X";
       } 
     
    public static void playerTwoMove( String [][] gameInput)
       {
     
        Scanner scan = new Scanner(System.in);
     
        String test[][] = new String [3][3];
     
        int row = 0;
        int column = 0;
     
        System.out.print( " Please input your row? " );
        row = scan.nextInt() - 1;
     
        System.out.print( " Please input your column? " );
        column = scan.nextInt() - 1;
     
        gameInput[row][column] = "Y";
       } 
     
    public static void gameBoard( String a [] [])
        {
        String[][] test = new String [3][3];
     
        int i, j;
     
        System.out.println( " Tic Tac Toe Game " );
     
        for (i = 0; i < test.length; i++) 
        {
            System.out.println("----------------");
            for(j = 0; j < test[i].length; j++)
            {
                System.out.print("|    "  + a[i][j] );
            }
            System.out.println("|      ");
        }
        System.out.println( "----------------"); 
     
        System.out.println(" ");
        }
     
     
     
    public static boolean checkForWinner(char[][] b)
       {
        // checkForWinner() method determines if a pattern of data stored
        // in the 2 D char array indicates the a player has won the game.
     
            boolean flag = false;
            boolean flag1 = false;
            boolean flag2 = false;
            boolean flag3 = false;
            boolean flag4 = false;
     
            // checks the contents of each row for matching data   
            for (int i = 0; i <= 2; i++)
            {
                if ((b[i][0] == b[i][1] && b[i][1] == b[i][2]) && b[i][2] != ' ') 
                    flag1 = true;
            }
     
             // checks the contents of each column for matching data
            for (int j = 0; j <= 2; j++)
            {
                if ((b[0][j] == b[1][j] && b[1][j] == b[2][j]) && b[2][j] != ' ') 
                    flag2 = true;
            }
     
            // checks the contents of one diagonal for matching data
            if ((b[0][0] == b[1][1] && b[1][1] == b[2][2]) && b[2][2] != ' ') 
                    flag3 = true;
     
            // checks the contents of the other diagonal for matching data
            if ((b[0][2] == b[1][1] && b[1][1] == b[2][0]) && b[2][0] != ' ') 
                    flag4 = true;
     
            // checks if any of the previous conditions evaluated to true        
            if (flag1 == true || flag2 == true || flag3 == true || flag4 == true)
                flag = true;
     
           // returns true if a winner was found; returns false is no winner     
           return flag;
       } // end of checkForWinner method
     
     
    }

    Thanks Again!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    The Move methods should test if the square is occupied before making the move.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    Everything is almost perfect but I when I go through my do while loop I'm having an unexpected problem. For some reason I can not make my do while loop terminate when going through the iterations. Here is my code.

     
    /**
     * 
     * CSC 225 - Online
     * Tic Tac Toe
     */
     
     
    import java.util.*;
     
    public class TicTacToe
    {
     
     
    public static void main(String[] args)
    {
      Scanner scan = new Scanner(System.in);
     
        char board[][] = new char[3][3];
     
        for ( int x = 0 ; x < board.length; x++)
        {
            for( int y = 0; y < board[x].length; y++)
            {
                board[x][y] = ' ';
            }
        }
     
     
        do
        {
            gameBoard(board);
            playerOneMove(board);
            System.out.println("");
            gameBoard(board);
            System.out.println("");
            playerTwoMove(board);
            System.out.println("");
            gameBoard(board);      
     
        }while( checkForWinner(board) != false);
     
     
     
    }
     
     
     
    public static void playerOneMove (char gameInput[][])
       {
     
        Scanner scan = new Scanner(System.in);
     
        char test[][] = new char [3][3];
     
        int row = 0;
        int column = 0;
     
        System.out.print( "Player 1 : Please input your row? " );
        row = scan.nextInt() - 1;
     
        System.out.print( "Please input your column? " );
        column = scan.nextInt() - 1;
     
        gameInput[row][column] = 'X';
       } 
     
    public static void playerTwoMove( char [][] gameInput)
       {
     
        Scanner scan = new Scanner(System.in);
     
        char test[][] = new char [3][3];
     
        int row = 0;
        int column = 0;
     
        System.out.print( "Player 2: Please input your row? " );
        row = scan.nextInt() - 1;
     
        System.out.print( "Player 2: Please input your column? " );
        column = scan.nextInt() - 1;
     
        gameInput[row][column] = 'Y';
       } 
     
    public static void gameBoard( char a [] [])
        {
        char[][] test = new char [3][3];
     
        int i, j;
     
        System.out.println( " Tic Tac Toe Game " );
     
        for (i = 0; i < test.length; i++) 
        {
            System.out.println("----------------");
            for(j = 0; j < test[i].length; j++)
            {
                System.out.print("|    "  + a[i][j] );
            }
            System.out.println("|      ");
        }
        System.out.println( "----------------"); 
     
        System.out.println(" ");
        }
     
     
     
    public static boolean checkForWinner(char[][] b)
       {
        // checkForWinner() method determines if a pattern of data stored
        // in the 2 D char array indicates the a player has won the game.
     
            boolean flag = false;
            boolean flag1 = false;
            boolean flag2 = false;
            boolean flag3 = false;
            boolean flag4 = false;
     
            // checks the contents of each row for matching data   
            for (int i = 0; i <= 2; i++)
            {
                if ((b[i][0] == b[i][1] && b[i][1] == b[i][2]) && b[i][2] != ' ') 
                    flag1 = true;
            }
     
             // checks the contents of each column for matching data
            for (int j = 0; j <= 2; j++)
            {
                if ((b[0][j] == b[1][j] && b[1][j] == b[2][j]) && b[2][j] != ' ') 
                    flag2 = true;
            }
     
            // checks the contents of one diagonal for matching data
            if ((b[0][0] == b[1][1] && b[1][1] == b[2][2]) && b[2][2] != ' ') 
                    flag3 = true;
     
            // checks the contents of the other diagonal for matching data
            if ((b[0][2] == b[1][1] && b[1][1] == b[2][0]) && b[2][0] != ' ') 
                    flag4 = true;
     
            // checks if any of the previous conditions evaluated to true        
            if (flag1 == true || flag2 == true || flag3 == true || flag4 == true)
                flag = true;
     
           // returns true if a winner was found; returns false is no winner     
           return flag;
       } // end of checkForWinner method
     
     
    }

    Here is my output.

    Player 1 : Please input your row? 3
    Please input your column? 3

    Tic Tac Toe Game
    ----------------
    | X| Y| Y|
    ----------------
    | | X| |
    ----------------
    | | | X|
    ----------------


    Player 2: Please input your row? Tic Tac Toe Game
    ----------------
    | | | |
    ----------------
    | | | |
    ----------------
    | | | |
    ----------------

    Player 1 : Please input your row? 1
    Please input your column? 1

    Tic Tac Toe Game
    ----------------
    | X| | |
    ----------------
    | | | |
    ----------------
    | | | |
    ----------------


    Player 2: Please input your row? 3
    Player 2: Please input your column? 1

    Tic Tac Toe Game
    ----------------
    | X| | |
    ----------------
    | | | |
    ----------------
    | Y| | |
    ----------------

    The checkForWinner method was created by are professor. She wanted us to use it for are problem. When going though the loop I notice the loop will stop if I equal checkForWinner method to false. But when I use true the loop continues even after having tic tac toe and winning the game. My program basically never stops. It just continues. Any idea why norm?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    the loop continues even after having tic tac toe and winning the game
    Try some standalone testing of the method.
    Create an array with a winning position and call the checkForWinner() method and print out what it returns.
    Try it with lots of different winning positions to see if it works.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    okay let me try that

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Logic of a Tic Tac Toe game.

    Also use this for easy debugging:
        System.out.println("1 "+java.util.Arrays.deepToString(board));
    Add lots of them. Change the "1" to be unique in each println.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] trying to use hashtable in tic tac toe game
    By leonne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 12th, 2013, 03:12 PM
  2. Need help with tic tac toe game
    By ogpg2006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:34 AM
  3. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 07:34 AM
  4. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  5. [NEED HELP] to clear result on my tic-tac-toe game
    By bontet in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2010, 03:50 PM