/** This is the Driver file for the Tic Tac Toe game. Let's play! * (but this program contains a little mistake somewhere) */ import java.io.*; import java.util.*; public class Driver { public static void main (String [] args) throws IOException { BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); char player = 'X'; TicTacToe game = new TicTacToe(); // The logic of the game is contained in this loop. The loop continues // until the game is over (winner or tie). while (true) { // Show board and ask where the player wants to place a token. System.out.println(game); System.out.println("Okay, player " + player + ", it's your turn."); // need a little loop to make sure input is valid boolean validInput = false; while(! validInput) { System.out.print ("enter row & column numbers (in range 1-3): "); String input = kbd.readLine(); StringTokenizer tok = new StringTokenizer(input, ", "); int row = Integer.parseInt(tok.nextToken()); int col = Integer.parseInt(tok.nextToken()); // place the token validInput = game.set(row, col, player); } // see if somebody won -- the findWinner() function will return either // T (for tie), or ' ' (for no winner yet), or X or O. char result = game.findWinner(); System.out.println("win check says '" + result + "'"); if (result == 'T') { System.out.println(game); System.out.println("Cat's game."); break; } else if (result != ' ') { System.out.println(game); System.out.println("Congratulations -- " + player + " is the winner!"); break; } // otherwise we keep going -- players take turns if (player == 'X') player = 'O'; else player = 'X'; } } }