March 2018
Intermediate to advanced
324 pages
8h 30m
English
In order to track who should play next, we need to store who played last:
private char lastPlayer = '\0';
public void play(int x, int y) {
checkAxis(x);
checkAxis(y);
setBox(x, y);
lastPlayer = nextPlayer();
}
public char nextPlayer() {
if (lastPlayer == 'X') {
return 'O';
}
return 'X';
}
You are probably starting to get the hang of it. Tests are small and easy to write. With enough experience, it should take a minute, if not seconds, to write a test and as much time or less to write the implementation.
Read now
Unlock full access