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; } }

Tweet This+ 1 thisPost To Linkedin