Question
Need help with this Tic Tac Toe program in Java. There is a TicTacToe class, its subclass SmartTicTacToe, and a class called Line, which represents
Need help with this Tic Tac Toe program in Java. There is a TicTacToe class, its subclass SmartTicTacToe, and a class called Line, which represents a row, a column, or a diagonal of the game grid. The constructor of Line takes as arguments of the indices of the line and the game grid as an array. Line class provides a method hasWon to tell whether a player has a winning play on that line and a method winningPlay to return the move that a player can make to win the game on the next play. I have commented on the sections of code I need help with. I state what needs to be executed.
public class Project { public static void main(String[] args) { TicTacToe game = new SmartTicTacToe(true); int i = 0; for(; i<9 && !game.isGameEnded(); i++) { game.play(); System.out.println("play " + (i+1) + ": "); System.out.print(game); System.out.println("\t\t"+game.getGameStatus()+" "); } } }
public class Line { private int[] line; private int[] grid;
Line(int[] line, int[] grid) { this.line = line; this.grid = grid; } private int sum() { int ret = 0; for(int i : line) { ret = ret + grid[i]; } return ret; }
/* * returns true iff this line contains the 'index' and the grid cell of this 'index' is blank */ private boolean hasBlankIndex(int index) { // TODO } /* * assume 'this' != 'that' * returns the index shared by both 'this' and 'that' lines, where the grid cell is empty * returns -1 otherwise */ int intersects (Line that) { // TODO }
/* * returns true iff this line has exactly one X (or O) as specified by the 'player' parameter */ boolean hasSinglePlay(int player) { return sum() == player; } /* * returns true iff this line has all X (or all O) as specified by the 'player' parameter. */ boolean hasWon(int player) { // TODO }
int winningPlay(int player) { // TODO }
int[] getLine() { return line; } }
public class TicTacToe { // The field "grid" represents a 3 by 3 Tic-Tac-Toe grid as an array of 9 integers, // which stores the cells of row 1, row 2, and row 3 consecutively in that order. // For each array element, 0 represents empty cell, 1 represents player X, 10 represents player O /* * O | | X * ----------- * X | X | * ----------- * X | O | O * * is represented by the array {10, 0, 1, 1, 1, 0, 1, 10, 10} */ private int[] grid = new int[9]; private boolean gameEnded = false; private int steps = 1; private String gameStatus = "still playing";
// The field "player" represents the current player (1 for 'X' or 10 for 'O'). protected int player; // An array of 'Line' objects that can be used to determine game-winning status and the possible winning plays protected Line[] lines; // The initial player is X if xStart is true public TicTacToe(boolean xStart) { player = xStart? 1 : 10; // indices of rows, columns, and diagonals int[][] lineIndices = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // indices of rows {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // indices of columns {0, 4, 8}, {2, 4, 6} // indices of diagonals }; lines = new Line[lineIndices.length]; for(int i=0; i // returns true iff either X or O has won the game // You should use the Line objects to implement this. private boolean checkWinner(int player) { // TODO } // generate a random index between 0 to 8 private int randomIndex() { return new Random().nextInt(9); } // Generate the string representation of a cell // 1. return a string 3 empty spaces if the cell is blank // 2. return " X " if "grid" at index "pos" is 1 // 3. return " O " if "grid" at index "pos" 2 private String getCell(int pos) { int player = grid[pos]; String ret = " "; if(player == 1) { ret = " X "; } else if (player == 10){ ret = " O "; } return ret; } // Change the static field "player" from either 1 to 2 or from 2 to 1 protected int flip(int player) { return player == 1 ? 10 : 1; } // Randomly pick the index of an empty cell protected int nextPlay() { int ret = randomIndex(); while(grid[ret] > 0) { ret = randomIndex(); } return ret; } // 1. Pick an empty cell and fill it with the integer representing the current player // That is, fill the empty cell with 1 if current player is X and fill it with 10 otherwise // 2. Flip the current player if there is no winner or a draw public void play() { grid[nextPlay()] = player; if(checkWinner(player)) { gameStatus = (player == 1 ? "X" : "O") + " won"; gameEnded = true; } else if (steps >= 9) { gameStatus = "it is a draw"; gameEnded = true; } else { steps = steps + 1; player = flip(player); gameStatus = (player == 1 ? 'X' : 'O') + " plays next"; } } public boolean isGameEnded() { return gameEnded; } public String getGameStatus() { return gameStatus; } // return string representation of the array "grid" as game board. For example, /* * If "grid" is {10, 0, 1, 1, 1, 0, 1, 10, 10}, you print * * O | | X * ----------- * X | X | * ----------- * X | O | O * */ @Override public String toString() { String ret = String.format("%s|%s|%s ", getCell(0), getCell(1), getCell(2)) + String.format("----------- ") + String.format("%s|%s|%s ", getCell(3), getCell(4), getCell(5)) + String.format("----------- ") + String.format("%s|%s|%s", getCell(6), getCell(7), getCell(8)); return ret; } } public class SmartTicTacToe extends TicTacToe { public SmartTicTacToe(boolean xStart) { super(xStart); } /* * return the index of the winning play for the input 'player' if there is one * otherwise return -1 */ private int winningPlay(int player) { int ret = -1; for(int i=0; ret == -1 && i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started