generate_fen method

String generate_fen()

Returns a FEN String representing the current position

Implementation

String generate_fen() {
  var empty = 0;
  var fen = '';

  for (var i = SQUARES_A8; i <= SQUARES_H1; i++) {
    if (board[i] == null) {
      empty++;
    } else {
      if (empty > 0) {
        fen += empty.toString();
        empty = 0;
      }
      var color = board[i]!.color;
      PieceType? type = board[i]!.type;

      fen += (color == WHITE) ? type.toUpperCase() : type.toLowerCase();
    }

    if (((i + 1) & 0x88) != 0) {
      if (empty > 0) {
        fen += empty.toString();
      }

      if (i != SQUARES_H1) {
        fen += '/';
      }

      empty = 0;
      i += 8;
    }
  }

  var cflags = '';
  if ((castling[WHITE] & BITS_KSIDE_CASTLE) != 0) {
    cflags += 'K';
  }
  if ((castling[WHITE] & BITS_QSIDE_CASTLE) != 0) {
    cflags += 'Q';
  }
  if ((castling[BLACK] & BITS_KSIDE_CASTLE) != 0) {
    cflags += 'k';
  }
  if ((castling[BLACK] & BITS_QSIDE_CASTLE) != 0) {
    cflags += 'q';
  }

  /* do we have an empty castling flag? */
  if (cflags == '') {
    cflags = '-';
  }
  final epflags = (ep_square == EMPTY) ? '-' : algebraic(ep_square!);
  final turnStr = (turn == Color.WHITE) ? 'w' : 'b';

  return [fen, turnStr, cflags, epflags, half_moves, move_number].join(' ');
}