// TicTacToe Game // Chris Wilcox // wilcox // 11/10/2012 // CS160 import java.util.Random; import java.util.Scanner; public class TicTacToe { public enum eStatus { IN_PROGRESS, TIE_GAME, COMPUTER_WINS, PLAYER_WINS, } public char board[][] = new char[3][3]; public Scanner keyboard; public Random random; // Constructor public TicTacToe() { // Create scanner keyboard = new Scanner(System.in); // Create random random = new Random(); // Initialize board initBoard(); } // Main program public static void main(String args[]) { // Instantiate class TicTacToe tictactoe = new TicTacToe(); // Computer starts boolean nextTurn = false; while (tictactoe.status() == eStatus.IN_PROGRESS) { // Next move if (nextTurn) tictactoe.player(); else tictactoe.computer(); // Display board tictactoe.printBoard(); nextTurn = !nextTurn; } // Print status eStatus status = tictactoe.status(); if (status == eStatus.TIE_GAME) System.out.println("Game is tied!"); else if (status == eStatus.COMPUTER_WINS) System.out.println("Computer wins!"); else if (status == eStatus.PLAYER_WINS) System.out.println("Player wins!"); } // Initialize board private void initBoard() { for (int row = 0; row <= 2; ++row) { for (int col = 0; col <= 2; ++col) { board[row][col] = '-'; } } } // Print board private void printBoard() { System.out.println("------------------------------"); for (int row = 0; row <= 2; ++row) { for (int col = 0; col <= 2; ++col) { System.out.print(" " + board[row][col]); } System.out.println(); } System.out.println(); } // Game status private void markSquare(int row, int col, char c) { // Mark square board[row][col] = c; } // Computer moves private void computer() { while (true) { int row = (int) random.nextInt(3); int col = (int) random.nextInt(3); // Check occupied if (board[row][col] == '-') { markSquare(row, col, 'X'); break; } } } // Player moves private void player() { while (true) { System.out.print("Row: "); int row = keyboard.nextInt(); System.out.print("Column: "); int col = keyboard.nextInt(); // Check range if ((row < 0) || (row > 2)) System.out.println("Row out of range, try again!"); else if ((col < 0) || (col > 2)) System.out.println("Column out of range, try again!"); // Check occupied else if (board[row][col] != '-') System.out.println("Occupied square, try again!"); // Valid move else { markSquare(row, col, 'O'); break; } } } // Game status private eStatus status() { // Check tie boolean tieGame = true; for (int row = 0; row <= 2; ++row) for (int col = 0; col <= 2; ++col) if (board[row][col] == '-') tieGame = false; // Tie game if (tieGame) return eStatus.TIE_GAME; // Player wins else if (runOfThree('O')) return eStatus.PLAYER_WINS; // Computer wins else if (runOfThree('X')) return eStatus.COMPUTER_WINS; return eStatus.IN_PROGRESS; } // Run of three? private boolean runOfThree(char c) { // Check rows for (int row = 0; row <= 2; ++row) { if ((board[row][0] == c) && (board[row][1] == c) && (board[row][2] == c)) return true; } // Check columns for (int col = 0; col <= 2; ++col) { if ((board[0][col] == c) && (board[1][col] == c) && (board[2][col] == c)) return true; } // Check diagonals if ((board[0][0] == c) && (board[1][1] == c) && (board[2][2] == c)) return true; if ((board[2][0] == c) && (board[1][1] == c) && (board[0][2] == c)) return true; return false; } }