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());
  }

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

  /* 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 1. ... */
    if (pgn_move_number == 1 && move.color == BLACK) {
      move_string = '1. ...';
      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;
}