Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions

Question

Describe the steps in a sampling operation

Answered: 1 week ago