load method

bool load(
  1. String fen
)

Load a position from a FEN String

Implementation

bool load(String fen) {
  List tokens = fen.split(RegExp(r'\s+'));
  String position = tokens[0];
  var square = 0;
  //String valid = SYMBOLS + '12345678/';

  final validMap = validate_fen(fen);
  if (!validMap['valid']) {
    print(validMap['error']);
    return false;
  }

  clear();

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

    if (piece == '/') {
      square += 8;
    } else if (is_digit(piece)) {
      square += int.parse(piece);
    } else {
      var color = (piece == piece.toUpperCase()) ? WHITE : BLACK;
      var type = PIECE_TYPES[piece.toLowerCase()]!;
      put(Piece(type, color), algebraic(square));
      square++;
    }
  }

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

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

  ep_square = (tokens[3] == '-') ? EMPTY : SQUARES[tokens[3]];
  half_moves = int.parse(tokens[4]);
  move_number = int.parse(tokens[5]);

  update_setup(generate_fen());

  return true;
}