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: tic tac toe java program error

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default tic tac toe java program error

    Hi
    I have a simple java program that simulates 2 players playing tic tac toe.
    the error I get is: when run the program reads the first entry and print the grid and then stops. it does not take any other entries. can't find the error. Help is much appreciated.


    // This program reads will allow two players to play a TicTacToe on a grid 3x3 by choosing a single number a time.
    // each player is allowed one entry at a time. 
     
    import java.util.Scanner;
     
    public class ticTacToe
    {
        private static char [][] grid = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
        public static void main ( String args[] )
        {
            boolean winner = false;
            boolean p1 = true;
            int row = 0;
            int col = 0;
            intro();  
            printGrid();
            do
            {
                int[] location = getInput();
                row = location[0];
                col = location[1];
                placeMarker(p1, row, col);
                printGrid();
                p1 = !p1;
     
            } while ( checkWin() == false || boardFull() != false );
        }
        public static void intro()
        {
            System.out.println ( "\nWelcome to ticTacToe v1.0!\n\nThis program simulates 2 players playing on a 3x3 grid. Each\nplayer will choose a number between 1 and 9 and hit enter to occupy a spot.\n\nThe player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.\n" );
        }
     
        public static void printGrid()
        {
            for ( int r = 0; r < grid.length; r++ )
            {
                if ( r == 1 )
                {
                    System.out.print ( " ---------\n" );
                }
                else if ( r == 2 )
                {   
                    System.out.print ( " ---------\n" );
                }
                for ( int c = 0; c < grid[r].length; c++ )
                {
                    System.out.printf ( "%2c", grid[r][c] );
                    if ( c == 0 )
                    {
                        System.out.print ( " |" );
                    }
                    else if ( c == 1 )
                    {
                        System.out.print ( " |" );
                    }
                }
                System.out.println();
            }
     
        }
        public static int [] getInput()
        {
            int [] location = new int [2];
            Scanner bo = new Scanner ( System.in );
            System.out.print ( "Please enter a number between 1 and 9: " );
            char number = bo.next().charAt(0);
     
            boolean found = false;
            do
            {
                while ( number < '1' || number > '9' )
                {
                    System.out.print ( "This is invalid entry.\nPlease enter a number between 1 and 9: " );
                    number = bo.next().charAt(0);
                }
     
                for ( int r = 0; r < grid.length; r++ )
                {
                    for ( int c = 0; c < grid.length; c ++ )
                    { 
                        if ( number == grid[r][c] )
                        {
                            location[0] = r;
                            location[1] = c;
                            found = true;
                        }
                    } 
                }
     
                if (found == false)
                {
                    System.out.print ( "This is invalid entry.\nPlease enter a number between 1 and 9: " );
                    number = bo.next().charAt(0);
                }
            } while (found == false);
            return location;
        }  
     
        public static void placeMarker( boolean p1, int r, int c )
        {
            if ( p1 == true )
                grid[r][c] = 'x';
            else
                grid[r][c] = 'o';
        }
     
        public static boolean checkWin()
        {
            boolean winner = false;
            do
            {
                if (((grid[0][0] == grid[0][1]) && (grid[0][1] == grid[0][2])) || ((grid[1][0] == grid[1][1]) && (grid[1][1] == grid[1][2])) || ((grid[2][0] == grid[2][1]) && (grid[2][1] == grid[2][2])) || ((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])) || ((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])) || ((grid[0][0] == grid[1][0]) && (grid[1][0] == grid[2][0])) || ((grid[0][1] == grid[1][1]) && (grid[1][1] == grid[2][1])) || ((grid[0][2] == grid[1][2]) && (grid[1][2] == grid[2][2])))
                    winner = true;
            } while ( winner == false );
     
            if ( winner == true ) 
            {
                System.out.print ( "\nCongratulations you won!\n" );
            }
            return winner;
        }
        public static boolean boardFull()
        { 
            for ( int r = 0; r < grid.length; r++ )
            {
                for ( int c = 0; c < grid.length; c ++ )
                {     if ( grid[r][c] > '0' && grid[r][c] <= '9' )
                    {
                        return false;
                    }
     
                }
            }
            return true;
        }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: tic tac toe java program error

    the program reads the first entry and print the grid and then stops.
    The program doesn't actually end. So evidently it gets into a never ending loop (with no output).

    public static boolean checkWin()
        {
            boolean winner = false;
            do
            {
                if (((grid[0][0] == grid[0][1]) && (grid[0][1] == grid[0][2])) || ((grid[1][0] == grid[1][1]) && (grid[1][1] == grid[1][2])) || ((grid[2][0] == grid[2][1]) && (grid[2][1] == grid[2][2])) || ((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])) || ((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])) || ((grid[0][0] == grid[1][0]) && (grid[1][0] == grid[2][0])) || ((grid[0][1] == grid[1][1]) && (grid[1][1] == grid[2][1])) || ((grid[0][2] == grid[1][2]) && (grid[1][2] == grid[2][2])))
                    winner = true;
            } while ( winner == false );
     
            // more stuff...

    I haven't peered too closely at that condition! But one thing is that the values of the grid array never change. So that if the condition ever evaluates to false winner will not be set to true and the program will never get out of that do loop.

    ---

    Do you really need a do loop here? You appear to be setting the value of winner. Why not do that just once with a single if statement?

    The condition of the if statement really is too long to be clear. It ought to be implemented in a separate method which does whatever it does with a number of steps. The intended behaviour of such a method should be documented, and the method itself tested to make sure it does what it is intended to do.

    Finally a small point, but, following Java coding conventions, the class really ought to be called TicTacToe with a capital T.

    [edit] Sorry, I mucked up the explanation near the start. It's when there is no winner yet that the problem with the loop arises.

    Here's the same code but better formatted, imao. It's more intelligible even with out using a separate method:

        public static boolean checkWin()
        {
            boolean winner = false;
            do
            {
                if (
                        ((grid[0][0] == grid[0][1]) && (grid[0][1] == grid[0][2])) 
                        || ((grid[1][0] == grid[1][1]) && (grid[1][1] == grid[1][2])) 
                        || ((grid[2][0] == grid[2][1]) && (grid[2][1] == grid[2][2])) 
                        || ((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])) 
                        || ((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])) 
                        || ((grid[0][0] == grid[1][0]) && (grid[1][0] == grid[2][0])) 
                        || ((grid[0][1] == grid[1][1]) && (grid[1][1] == grid[2][1])) 
                        || ((grid[0][2] == grid[1][2]) && (grid[1][2] == grid[2][2]))) 
                {
                    winner = true;
                }
            } while ( winner == false );
            // etc

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: tic tac toe java program error

    Thank you so much for this input. and yes my program should be called Tic Tac Toe!

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: tic tac toe java program error

    Basically what I'm suggesting is that you replace the whole do loop with a single assignment to the winner variable:

    public static boolean checkWin()
    {
        boolean winner = ((grid[0][0] == grid[0][1]) && (grid[0][1] == grid[0][2])) 
                    || ((grid[1][0] == grid[1][1]) && (grid[1][1] == grid[1][2])) 
                    || ((grid[2][0] == grid[2][1]) && (grid[2][1] == grid[2][2])) 
                    || ((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])) 
                    || ((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])) 
                    || ((grid[0][0] == grid[1][0]) && (grid[1][0] == grid[2][0])) 
                    || ((grid[0][1] == grid[1][1]) && (grid[1][1] == grid[2][1])) 
                    || ((grid[0][2] == grid[1][2]) && (grid[1][2] == grid[2][2])); 
        if ( winner ) 
        {
            System.out.print ( "\nCongratulations you won!\n" );
        }
        return winner;
    }

    Notice how you can assign the condition directly to the variable which is what you appear to be trying to do with the do loop.

    Also notice how "if(winner)" is used instead of "if(winner==true)": they mean the same thing but the former is more idiomatic. Likewise when you want a condition to be false you would say "do {/*whatever*/} while(!foo)" rather than "do {/*whatever*/} while(foo==false)".

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    JSF001 (June 22nd, 2013)

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: tic tac toe java program error

    I was wondering how to delete the post? I have no need for it anymore. thank you

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: tic tac toe java program error

    It's better, I think, for the thread to remain for the benefit of others.

Similar Threads

  1. Need help with a tic tac toe program
    By RodePope4546 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 22nd, 2012, 05:47 PM
  2. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2012, 08:11 PM
  3. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2012, 04:47 PM
  4. Help with tic tac toe java program?
    By Momar99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2011, 11:47 PM
  5. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 PM

Tags for this Thread