san_moves method

List<String?> san_moves()

return the san string representation of each move in history. Each string corresponds to one move.

Implementation

List<String?> san_moves() {
  /* pop all of history onto reversed_history */
  final reversed_history = <Move?>[];
  while (history.isNotEmpty) {
    reversed_history.add(undo_move());
  }

  var start_move_number = 1;
  if (header['FEN'] != null) {
    final move_number_string = header['FEN'].split(' ')[5];
    start_move_number = int.parse(move_number_string);
  }

  final moves = <String?>[];
  var move_string = '';
  var pgn_move_number = start_move_number;

  /* build the list of moves.  a move_string looks like: "3. e3 e6" */
  while (reversed_history.isNotEmpty) {
    final move = reversed_history.removeLast()!;

    /* if the position started with black to move, start PGN with ${start_move_number}. ... */
    if (pgn_move_number == start_move_number && move.color == BLACK) {
      move_string = '$start_move_number. ...';
      pgn_move_number++;
    } else if (move.color == WHITE) {
      /* store the previous generated move_string if we have one */
      if (move_string.isNotEmpty) {
        moves.add(move_string);
      }
      move_string = pgn_move_number.toString() + '.';
      pgn_move_number++;
    }

    move_string = move_string + ' ' + move_to_san(move);
    make_move(move);
  }

  /* are there any other leftover moves? */
  if (move_string.isNotEmpty) {
    moves.add(move_string);
  }

  /* is there a result? */
  if (header['Result'] != null) {
    moves.add(header['Result']);
  }

  return moves;
}