load method Null safety

bool load(
  1. String fen
)

Load a position from a FEN String

Implementation

bool load(String fen) {
  /// get FEN fields
  List tokens = fen.split(RegExp(r"\s+"));

  /// position is the Piece placement
  String position = tokens[0];
  int square = 0;
//    String valid = SYMBOLS + '12345678/';

  /// Check if FEN is valid
  Map validMap = validateFen(fen);
  if (!validMap["valid"]) {
    print(validMap["error"]);
    return false;
  }

  clear();

  for (int i = 0; i < position.length; i++) {
    String piece = position[i];

    if (piece == '/') {
      square += 8;
    } else if (isDigit(piece)) {
      square += int.parse(piece);
    } else {
      /// Upper case means WHITE, otherwise BLACK
      Color color = (piece == piece.toUpperCase()) ? WHITE : BLACK;

      /// Get piece type
      PieceType type = PIECE_TYPES[piece.toLowerCase()]!;

      put(Piece(type, color), algebraic(square));
      square++;
    }
  }

  /// Get turn to play
  if (tokens[1] == 'w') {
    turn = WHITE;
  } else {
    assert(tokens[1] == 'b');
    turn = BLACK;
  }

  /// Get castling availability
  if (tokens[2].indexOf('K') > -1) {
    castling[WHITE] |= BITS_KSIDE_CASTLE;
  }
  if (tokens[2].indexOf('Q') > -1) {
    castling[WHITE] |= BITS_QSIDE_CASTLE;
  }
  if (tokens[2].indexOf('k') > -1) {
    castling[BLACK] |= BITS_KSIDE_CASTLE;
  }
  if (tokens[2].indexOf('q') > -1) {
    castling[BLACK] |= BITS_QSIDE_CASTLE;
  }

  epSquare = (tokens[3] == '-') ? EMPTY : SQUARES[tokens[3]];
  halfMoves = int.parse(tokens[4]);
  moveNumber = int.parse(tokens[5]);

  updateSetup(generateFen());

  return true;
}