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;
  } else {
    currentPlayer = Color.RED;
  }
}

public void putDisc(int column) {
  if (column > 0 && column <= COLUMNS) {
    int numOfDiscs = getNumberOfDiscsInColumn(column - 1);
    if (numOfDiscs < ROWS) {
      board[column - 1][numOfDiscs] = currentPlayer;
      switchPlayer();
    }
  }
}
...
..................Content has been hidden....................

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