Requirement 6 – win condition (II)

The first win condition.

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

The checkWinCondition private method implements this rule by scanning whether or not the last move is a winning one:

... 
private Color winner;

public static final int DISCS_FOR_WIN = 4;
 
public void putDisc(int column) {
  ...
  if (numOfDiscs < ROWS) {
    board[column - 1][numOfDiscs] = currentPlayer;
    printBoard();
    checkWinCondition(column - 1, numOfDiscs);
    switchPlayer();
    ...
}
 
private void checkWinCondition(int col, int row) {
  Pattern winPattern = Pattern.compile(".*" +
currentPlayer + "{" + DISCS_FOR_WIN + "}.*"); // Vertical check StringJoiner stringJoiner = new StringJoiner(""); for (int auxRow = 0; auxRow < ROWS; ++auxRow) { stringJoiner.add(board[col][auxRow].toString()); } if (winPattern.matcher(stringJoiner.toString()).matches()) { winner = currentPlayer; System.out.println(currentPlayer + " wins"); } } public boolean isFinished() { if (winner != null) return true; ... } ...
..................Content has been hidden....................

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