movesFromFile method

  1. @override
List<Move> movesFromFile(
  1. String file, {
  2. GameBoard? initialBoard,
})

Converts a file of the form

1: ☗P77-76
2: ☗S79-68

to a list of game moves.

Implementation

@override
List<Move> movesFromFile(String file, {GameBoard? initialBoard}) {
  /// firstly split file into a list of moves, ignoring any prepending number indicators
  final movesAsText = file.replaceAll(RegExp(r'\d+\:\s'), '').split('\n');
  movesAsText.remove(''); // remove any empty strings

  final moves = <Move>[];
  for (final moveAsText in movesAsText) {
    // convert the move into a list of components
    final components = _convertMoveAsTextIntoComponents(moveAsText);
    if (components != null) {
      // parse each component
      final player =
          components[_CaptureGroup.player.index] == BoardConfig.gote
              ? PlayerType.gote
              : PlayerType.sente;
      final piece = PackageUtils.pieceStringToType(
          components[_CaptureGroup.piece.index]!);
      final from = Position.fromString(components[_CaptureGroup.from.index]!);
      final isCapture =
          components[_CaptureGroup.movement.index] == _captureSymbol;
      final isDrop = components[_CaptureGroup.movement.index] == _dropSymbol;
      final to = Position.fromString(components[_CaptureGroup.to.index]!);
      final isPromotion =
          components[_CaptureGroup.promotion.index] == _promotionSymbol;

      moves.add(
        Move(
          player: player,
          piece: piece,
          from: from,
          to: to,
          isCapture: isCapture,
          isDrop: isDrop,
          isPromotion: isPromotion,
        ),
      );
    }
  }

  return moves;
}