sanMoves method Null safety

List<String> sanMoves()

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

Implementation

List<String> sanMoves() {
  /* pop all of history onto reversed_history */
  List<Move> reversedHistory = [];
  while (history.length > 0) {
    reversedHistory.add(undoMove()!);
  }

  List<String> moves = [];
  String moveString = '';
  int pgnMoveNumber = 1;

  /* build the list of moves.  a move_string looks like: "3. e3 e6" */
  while (reversedHistory.length > 0) {
    Move? move = reversedHistory.removeLast();

    /* if the position started with black to move, start PGN with 1. ... */
    if (pgnMoveNumber == 1 && move.color == BLACK) {
      moveString = '1. ...';
      pgnMoveNumber++;
    } else if (move.color == WHITE) {
      /* store the previous generated move_string if we have one */
      if (moveString.length != 0) {
        moves.add(moveString);
      }
      moveString = pgnMoveNumber.toString() + '.';
      pgnMoveNumber++;
    }

    moveString = moveString + ' ' + moveToSan(move);
    makeMove(move);
  }

  /* are there any other leftover moves? */
  if (moveString.length != 0) {
    moves.add(moveString);
  }

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

  return moves;
}