algebraicLegalMoves function

IMap<String, ISet<String>> algebraicLegalMoves(
  1. Position<Position> pos, {
  2. bool isChess960 = false,
})

Gets all the legal moves of this position in the algebraic coordinate notation.

Includes both possible representations of castling moves (unless chess960 is true).

Implementation

IMap<String, ISet<String>> algebraicLegalMoves(Position pos,
    {bool isChess960 = false}) {
  final Map<String, ISet<String>> result = {};
  for (final entry in pos.legalMoves.entries) {
    final dests = entry.value.squares;
    if (dests.isNotEmpty) {
      final from = entry.key;
      final destSet = dests.map((e) => toAlgebraic(e)).toSet();
      if (!isChess960 &&
          from == pos.board.kingOf(pos.turn) &&
          squareFile(entry.key) == 4) {
        if (dests.contains(0)) {
          destSet.add('c1');
        } else if (dests.contains(56)) {
          destSet.add('c8');
        }
        if (dests.contains(7)) {
          destSet.add('g1');
        } else if (dests.contains(63)) {
          destSet.add('g8');
        }
      }
      result[toAlgebraic(from)] = ISet(destSet);
    }
  }
  return IMap(result);
}