March 2018
Intermediate to advanced
324 pages
8h 30m
English
To fulfill this test, we need to check whether any horizontal line is filled by the same mark as the current player. Until this moment, we didn't care what was put on the board array. Now, we need to introduce not only which board boxes are empty, but also which player played them:
public String play(int x, int y) {
checkAxis(x);
checkAxis(y);
lastPlayer = nextPlayer();
setBox(x, y, lastPlayer);
for (int index = 0; index < 3; index++) {
if (board[0][index] == lastPlayer && board[1][index] == lastPlayer && board[2][index] == lastPlayer) {
return lastPlayer + " is the winner";
}
}
return "No winner";
}private void setBox(int x, int y, char lastPlayer) { if (board[x - 1][y - 1] != '\0') { throw new RuntimeException("Box is occupied"); ...Read now
Unlock full access