This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.
In this exercise, you will create a class NoughtsAndCrosses which will represent a noughts-and-crosses board.
Note
For more information about ‘noughts-and-crosses’ (also known as ‘tic-tac-toe’), see http://en.wikipedia.org/wiki/Tic-tac-toe.
Warning
This exercise provides very little guidance or testing on the details of the implementation.
Implement a class NoughtsAndCrosses with the following API:
- public NoughtsAndCrosses(int[][] board)
-
Class constructor. The board will be passed as a \( 3 \times 3 \) two-dimensional array of ints. Each element of the array contains a number representing what is in that cell:
- 0 : nothing
- 1 : a nought
- 2 : a cross
- public boolean isDraw
- Returns true if neither player won, even if the game is not complete. Otherwise, if a player has three noughts or crosses in a line, returns false.
- public int whoWon
- Returns a 1 if there are three noughts in a line, and a 2 if there are three crosses in a line. Otherwise returns 0.
The following client code illustrates how the game works.
int[][] board = { { NONE, NONE, NOUGHTS }, { NONE, NONE, NOUGHTS }, { NONE, NONE, NOUGHTS }, }; NoughtsAndCrosses nc = new NoughtsAndCrosses(board); System.out.println(nc.isDraw()); // false System.out.println(nc.whoWon()); // NOUGHTS
An automated test has been created for this exercise: NoughtsAndCrossesTest.java.