Requirement 3 – player shifts

The third requirement relates to the game logic.

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.

These tests cover the verification of the new functionality. For the sake of simplicity, the red player will always start the game:

@Test
public void whenFirstPlayerPlaysThenDiscColorIsRed() {
  assertThat(tested.getCurrentPlayer(), is("R"));
}

@Test
public void whenSecondPlayerPlaysThenDiscColorIsRed() {
  int column = 1;
  tested.putDiscInColumn(column);
  assertThat(tested.getCurrentPlayer(), is("G"));
}

A couple of methods need to be created to cover this functionality. The switchPlayer method is called before returning the row in the putDiscInColumn method:

private static final String RED = "R";

private static final String GREEN = "G";

private String currentPlayer = RED;

public Connect4TDD() {
  for (String[] row : board) Arrays.fill(row, EMPTY);
}

public String getCurrentPlayer() {
  return currentPlayer;
}
 
private void switchPlayer() {
  if (RED.equals(currentPlayer)) currentPlayer = GREEN;
  else currentPlayer = RED;
}
 
public int putDiscInColumn(int column) {
  ...
  switchPlayer();
  return row;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67