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 */ public static final char PLAYER = 'X'; /** * the game character symbol for the computer */ public static final char COMPUTER = 'O'; /** * the game character symbol for an empty place on the board */ public static final char EMPTY = '-'; /** * the current board */ // !!!!!! DECLARE BOARD HERE /** * the current player */ char currentPlayer; /** * 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 a player won? currentPlayer = swapPlayer(); // swap the currentPlayer moves++; } if (!hasWon) { System.out.println("No one 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. "); // !!!!!! 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."); int row = -1; int col = -1; do { System.out.println("Enter the row (0-2) and column (0-2) of your move:"); row = readValue(); col = readValue(); if (board[row][col] == EMPTY) { break; } else { System.out.println("This space is not empty. Try again."); } } while (board[row][col] != EMPTY); 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 = -1; while (true) { val = in.nextInt(); if (val < 0 || val > 2) { System.out.println("You have not entered a value in the range 0 to 2. Please try again."); } else { break; } } return val; } /** * Checks to see if the currentPlayer has won. * * @return true if the currentPlayer has won. */ public boolean checkWin() { // check rows String seqWin = "" + currentPlayer + currentPlayer + currentPlayer; boolean hasWon = false; String seq = ""; // check rows for (int i = 0; i < board.length; i++) { seq = ""; for (int j = 0; j < board.length; j++) { seq += board[i][j]; } if (seq.equals(seqWin)) { hasWon = true; } } // !!!!!! YOU NEED TO CHECK THE COLUMNS AND DIAGONALS! if (hasWon) { if (currentPlayer == PLAYER) { System.out.println("Congratulations - you have won!"); } else { System.out.println("Sorry, the computer won and you lost. " + "\nBetter luck next time"); } } return hasWon; } /** * @return string representation of the board. */ public String toString() { String b = "\t"; // !!!!!! CREATE STRING REPRESENTATION OF THE BOARD return b; } }