#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    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.

    Code:
    // 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
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    the error I get is: when run the program reads the first entry and print the grid and then stops
    What do you mean by "then stops"? Is there an error?
    Does the program exit? Does the program go into an endless loop?
    Try debugging the code to see why it stops. Add some System.out.println(...); statements to the code that print messages when methods are executed and that print out the values of variables as they are used and given values.
    The print out will show where the code is executing and what the values of the variable are that controls the program's execution.
    Last edited by NormR; June 22nd, 2013 at 06:22 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    What do you mean by "then stops"? Is there an error?
    Does the program exit? Does the program go into an endless loop?
    Try debugging the code to see why it stops. Add some System.out.println(...); statements to the code that print messages when methods are executed and that print out the values of variables as they are used and given values.
    The print out will show where the code is executing and what the values of the variable are that controls the program's execution.
    @NormR: I found the error which was an extra loop in there. please delete the post. Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo