ChessState constructor

ChessState(
  1. String fen, {
  2. ChessState? last,
})

Implementation

ChessState(this.fen, { this.last }) {
  Map<int, Map<int, StateEntry>> _board = {};

  if (fen == "") {
    for (var i = 0; i < 8; i++) {
      _board[i] = {};
      for (var j = 0; j < 8; j++) {
        _board[i]![j] = StateEntry("", SquarePosition(i, j), this);
      }
    }
  } else {
    int rank = 0;
    for (var fenRank in fen.split("/")) {
      int file = 0;
      int currRank = 7 - rank;
      _board[currRank] = {};

      for (var i = 0; i < fenRank.length; i++) {
        int piece = fenRank.codeUnitAt(i);
        if (piece == space) break;

        if (piece >= zero && piece <= nine) {
          for (int j = 0; j < piece-zero; j++) {
              _board[currRank]![file] = StateEntry("", SquarePosition(currRank, file), this);
              file++;
          }
        } else {
          String pieceNotation = String.fromCharCode(piece);
          _board[currRank]![file] = StateEntry(pieceNotation, SquarePosition(currRank, file), this);
          file++;
        }
      }
      rank++;
    }
  }

  board = _board;
}