Question
please help me figure out how to junit test the isOver() and getWinner(), im confused to the code given... implementation public class G { public
please help me figure out how to junit test the isOver() and getWinner(), im confused to the code given...
implementation
public class G {
public final static int NUM_SPACES = 9;
enum Mark { X, O; }
private Mark[] grid; private int turn;
public G() { grid = new Mark[NUM_SPACES]; 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; }
public boolean isOver() { return getWinner() != null || getTurn() == NUM_SPACES; } public Mark getWinner() { if(grid[0] != null && grid[0] == grid[1] && grid[0] == grid[2]) { return grid[0]; } else if(grid[3] != null && grid[3] == grid[4] && grid[0] == grid[5]) { return grid[3]; } else if(grid[6] != null && grid[6] == grid[7] && grid[6] == grid[8]) { return grid[6]; }else { checkValid(turn); } if(grid[0] != null && grid[0] == grid[3] && grid[0] == grid[6]) { return grid[0]; } else if(grid[1] != null && grid[1] == grid[4] && grid[1] == grid[7]) { return grid[1]; } else if(grid[2] != null && grid[2] == grid[3] && grid[2] == grid[6]) { return grid[2]; } else { checkValid(turn); }
if (grid[0] != null && grid[0] == grid[4] && grid[4] == grid[8]) { return grid[0]; } if (grid[2] != null && grid[2] == grid[4] && grid[4] == grid[6]) { return grid[2]; } return null; }
the junit testing
mport static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; class GTest { private static final long SEED = 20230123003L; private static final Random RAND = new Random(SEED); private Game g; @BeforeEach void init() { g = new Game(); } @Test void playGame() { System.out.println(g.toString()); for (int i = 0; i < Game.NUM_SPACES; i++) { System.out.printf("Current Player: %s Location: %d ", g.getCurrentPlayer(), g.getTurn()); g.placeMark(g.getTurn()); System.out.println(g.toString()); } } @Test void testZ() { for (Game.Mark space : g.getGrid()) { assertNull(space); } } @Test void testPlaceMark() { int randSpace = RAND.nextInt(Game.NUM_SPACES); assertTrue(g.placeMark(randSpace)); assertFalse(g.placeMark(randSpace)); // no longer empty Game.Mark[] grid = g.getGrid(); for (int i = 0; i < grid.length; i++) { assertTrue(grid[i] == null || i == randSpace, String.format("%d/%d", i, randSpace)); } }
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