Question
Creating a tic tac toe game, implement these method to the code below isOver : that returns a Boolean if the game is over getWinner
Creating a tic tac toe game, implement these method to the code below
isOver : that returns a Boolean if the game is over
getWinner : returns the mark of the winning player
public class G {
public final static int NUM_SPACE = 9;
enum Mark { X, O; }
private Mark[] grid; private int turn;
public G() { grid = new Mark[NUM_SPACE]; turn = 0; }
public boolean placeMark(int space) { return placeMark(getCurrentPlayer(), space); }
/** * @return Mark of the current player */ public Mark getCurrentPlayer() { return turn % 2 == 0 ? Mark.X : Mark.O; }
private boolean placeMark(Mark whichMark, int space) { boolean placed = false; checkValid(space); if (grid[space] == null) { grid[space] = whichMark; turn++; placed = true; } return placed; }
private void checkValid(int space) { if (space < 0 || space >= NUM_SPACES) { throw new IllegalArgumentException("space not on board"); }
}
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < NUM_SPACES; i++) { sb.append(grid[i] == null ? '-' : grid[i]); sb.append((i + 1) % 3 == 0 ? ' ' : '|'); } return sb.toString(); }
/** * @return copy of the game grid */ public Mark[] getGrid() { return grid.clone(); }
/** * @return turn count */ public int getTurn() { return turn; }
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