makeMove static method

GameBoard makeMove(
  1. GameBoard gameBoard,
  2. Move move
)

Makes a given move for a given game board

Implementation

static GameBoard makeMove(GameBoard gameBoard, Move move) {
  final boardPieces = List<BoardPiece>.from(gameBoard.boardPieces);
  final sentePiecesInHand =
      List<BoardPiece>.from(gameBoard.sentePiecesInHand);
  final gotePiecesInHand = List<BoardPiece>.from(gameBoard.gotePiecesInHand);

  if (move.isDrop) {
    final list = move.player.isSente ? sentePiecesInHand : gotePiecesInHand;
    final droppedPiece =
        list.firstWhere((piece) => piece.pieceType == move.piece);
    list.remove(droppedPiece);
  } else {
    if (move.isCapture) {
      final capturedPiece =
          boardPieces.firstWhere((piece) => piece.position == move.to);
      boardPieces.remove(capturedPiece);

      final list = move.player.isSente ? sentePiecesInHand : gotePiecesInHand;
      list.add(
        BoardPiece(
          player: move.player,
          pieceType: capturedPiece.pieceType.normalize(),
          position: null,
        ),
      );
    }

    final oldPiece =
        boardPieces.firstWhere((piece) => piece.position == move.from);
    boardPieces.remove(oldPiece);
  }

  final newPiece = BoardPiece(
    player: move.player,
    pieceType: move.isPromotion ? move.piece.promote() : move.piece,
    position: move.to,
  );
  boardPieces.add(newPiece);

  return GameBoard(
    boardPieces: boardPieces,
    sentePiecesInHand: sentePiecesInHand,
    gotePiecesInHand: gotePiecesInHand,
  );
}