package tictactoe; import java.util.Scanner; /** * Tic Tac Toe The user ('X') plays against the computer ('O') The first player * is chosen randomly. * * @author gorr */ public class TicTacToe { /** * the game character symbol for the player */ private static final char PLAYER = 'X'; /** * the game character symbol for the computer */ private static final char COMPUTER = 'O'; /** * the game character symbol for an empty place on the board */ private static final char EMPTY = '-'; /** * the current board */ // !!!!!! DECLARE BOARD HERE /** * the current player */ private char currentPlayer; /** * the winner */ private char winner = ' '; /** * Use for reading input from the user */ Scanner in = new Scanner(System.in); /** * Plays the game of TicTacToe. Stops when either 1) a player wins or 2) no * spaces are left on the board. */ public void play() { openingGreeting(); // greeting currentPlayer = initPlayer(); // pick starting player initBoard(); // initialize the board int moves = 0; boolean hasWon = false; while (moves < 9 && !hasWon) { // game loop over moves and hasWon if (currentPlayer == PLAYER) { userTurn(); } else { computerTurn(); } System.out.println("The board is now: \n" + this); hasWon = checkWin(); // has the current player won? currentPlayer = swapPlayer(); // swap the currentPlayer moves++; } // !!!!!! CHECK TO SEE IF THERE IS WINNER. IF SO, SAY WHO. // !!!!!! OTHERWISE, SAY THAT NO ONE HAS WON. } /** * Welcome the player */ public void openingGreeting() { System.out.println("\nWELCOME TO TICTACTOE\n"); System.out.println("You will be X, the computer will be O."); System.out.println("The starting player will be picked randomly."); System.out.println("Enjoy the game and good luck!\n"); } /** * Initialize the values in the board to all blanks. Note, each board * position is either: PLAYER - this is the user COMPUTER - this is the * computer EMPTY - indicates the position is empty */ public void initBoard() { // !!!!!! INITIALIZE BOARD HERE } /** * Swap the current player. That is, if the current player is 'X', it is * swapped to be 'O', and vice versa. * * @return the new current player */ public char swapPlayer() { if (currentPlayer == PLAYER) { currentPlayer = COMPUTER; } else { currentPlayer = PLAYER; } return currentPlayer; } /** * Randomly pick which player (user or computer) goes first. Set the * currentPlayer to the first player. It is assumed that the user is always * "X" and the computer is always "O". * * @return the first player */ public char initPlayer() { if (Math.random() < .5) { currentPlayer = PLAYER; System.out.println("You are the first player."); } else { currentPlayer = COMPUTER; System.out.println("The computer is the first player."); } return currentPlayer; } /** * Chooses which position the computer will play. Sets that position of the * board to "O" */ public void computerTurn() { System.out.println("\nIt is the computer's turn. \n" + "Press enter to continue."); in.nextLine(); // reads in the uers's "enter" // !!!!!! DECIDE WHERE THE COMPUTER WILL PLAY } /** * Have the user choose which position to play. Sets that position of the * board to "X" */ public void userTurn() { System.out.println("\nIt is your turn."); System.out.print("Enter the row (0-2) of your move: "); int row = readValue(); System.out.println("You entered row " + row); System.out.print("Enter the column (0-2) of your move: "); int col = readValue(); System.out.println("You entered column " + col); while (board[row][col] != EMPTY) { System.out.println("\nThis space is not empty. Try again entering a row & column."); System.out.print("Enter the row (0-2) of your move: "); row = readValue(); System.out.println("You entered row " + row); System.out.print("Enter the column (0-2) of your move: "); col = readValue(); System.out.println("You entered column " + col); } System.out.println(""); board[row][col] = PLAYER; } /** * Utility method to prompt user for a value in the range of 0 to 2 It makes * sure that the user enters a number between 0 and 2. If not, it loops and * asks for another input. * * @return the value of the row or column */ public int readValue() { int val = Integer.parseInt(in.nextLine().trim().substring(0,1)); while (val < 0 || val > 2) { System.out.print("You have not entered a value in the " + "range 0 to 2. Please try again: "); val = Integer.parseInt(in.nextLine().trim().substring(0,1)); } return val; } /** * Checks to see if the currentPlayer has won. * * @return true if the currentPlayer has won. */ public boolean checkWin() { int cnt = 0; // counts the number of positions occupied by currentPlayer // check rows for (int i = 0; i < board.length; i++) { cnt = 0; for (int j = 0; j < board[i].length; j++) { if (board[i][j] == currentPlayer) { cnt++; } } if (cnt == 3) { winner = currentPlayer; return true; } } // !!!!!! HERE YOU NEED TO CHECK THE COLUMNS AND DIAGONALS! return false; // no one has won } /** * @return string representation of the board. */ public String toString() { String b = "\t"; // !!!!!! CREATE STRING REPRESENTATION OF THE BOARD return b; } }