Requirement 7 – win condition (III)

This is the second win condition, which is pretty similar to the previous one.

If a player inserts a disc and connects more than three discs of his color in a straight horizontal line, then that player wins.

This time, we are trying to win the game by inserting discs into adjacent columns:

@Test
public void when4HorizontalDiscsAreConnectedThenPlayerWins() {
  int column;
  for (column = 0; column < 3; column++) {
    tested.putDiscInColumn(column); // R
    tested.putDiscInColumn(column); // G
  }
  assertThat(tested.getWinner(), isEmptyString());
  tested.putDiscInColumn(column); // R
  assertThat(tested.getWinner(), is("R"));
}

The code to pass this test is put into the checkWinners method:

  if (winner.isEmpty()) { 
    String horizontal = Stream
.of(board[row])
.reduce(String::concat).get();
if (winPattern.matcher(horizontal).matches()) winner = colour; }
..................Content has been hidden....................

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