Refactoring

Even though the isWin method is not the scope of the last test, it can still be refactored even more. For once, we don't need to check all the combinations, but only those related to the position of the last piece played. The final version could look like the following:

private boolean isWin(int x, int y) { int playerTotal = lastPlayer * 3; char horizontal, vertical, diagonal1, diagonal2; horizontal = vertical = diagonal1 = diagonal2 = '\0'; for (int i = 0; i < SIZE; i++) { horizontal += board[i][y - 1]; vertical += board[x - 1][i]; diagonal1 += board[i][i]; diagonal2 += board[i][SIZE - i - 1]; } if (horizontal == playerTotal || vertical == playerTotal || diagonal1 == playerTotal || diagonal2 == playerTotal) { return true; } return ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.