/** This is the Driver file for the Tic Tac Toe game. Let's play! * (but this program contains a little mistake somewhere - can you find it?) * By the way, when the user enters row & column numbers, let's assume * they are separated by a space, not by punctuation (as in a comma). */ import java.util.Scanner; public class Driver { public static void main (String [] args) { Scanner kbd = new Scanner(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.printf("%s\n", game); System.out.printf("Okay, player %c, it's your turn.\n", player); // need a little loop to make sure input is valid boolean validInput = false; while(! validInput) { System.out.printf ("enter row & column numbers (in range 1-3): "); int row = kbd.nextInt(); int col = kbd.nextInt(); // 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.printf("win check says '%c'\n", result); if (result == 'T') { System.out.printf("%s\n", game); System.out.printf("Cat's game.\n"); break; } else if (result != ' ') { System.out.printf("%s\n", game); System.out.printf("Congratulations -- %c is the winner!", player); break; } // otherwise we keep going -- players take turns if (player == 'X') player = 'O'; else player = 'X'; } } }