O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required