move method

PlayedMove? move(
  1. Move m
)

Performs a move m on the board and updates _gameStatus.

Returns the PlayedMove if game is not over, otherwise null. It also sets the current GameStatus to GameStatus.playing and if the King was captured, it sets it to either GameStatus.whiteCheckmated or GameStatus.blackCheckmated, depending on what color is the captured King.

Implementation

PlayedMove? move(Move m) {
  if (isGameOver ||
      board.at(m.from.row, m.from.col)?.color != playerOnTurn ||
      !_legalMoves.contains(m)) return null;

  final PlayedMove? move = _board.safeMove(m);
  if (move == null) return null;

  _gameStatus = GameStatus.playing;
  if (move.pieceOnLandingSquare is King) {
    _gameStatus = move.pieceOnLandingSquare is BlackKing
        ? GameStatus.whiteCheckmated
        : GameStatus.blackCheckmated;
  }
  _gameHistory.add(move);
  _calculateLegalMoves();
  return move;
}