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

Requirement 3 – player shifts

More game logic is introduced with this requirement.

It is a two-person game, so there is one colour for each player. One player uses red (R) and the other one uses green (G). Players alternate turns, inserting one disc every time.

We need to save the current player to determine which player is playing this turn. We also need a function to switch the players to recreate the logic of turns. Some lines of code become relevant in the putDisc function. Specifically, the board position assignment is made using the current player, and it is switched after every move, as the game rules say:

... private Color currentPlayer = Color.RED; private void switchPlayer() { if (Color.RED == currentPlayer) { currentPlayer = Color.GREEN; ...

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