decodeMove function

Move? decodeMove(
  1. int actionIdx,
  2. Game game
)

Implementation

Move? decodeMove(int actionIdx, Game game) {
  if (actionIdx == 0) return null; // Padding/Start token

  int flatIdx = actionIdx - 1;
  int from64 = flatIdx ~/ 64;
  int to64 = flatIdx % 64;

  int fromX = from64 % 8;
  int fromY = from64 ~/ 8;
  int toX = to64 % 8;
  int toY = to64 ~/ 8;

  // Convert 8x8 coords back to Bishop's internal indices
  int fromSq = game.size.square(fromX, fromY);
  int toSq = game.size.square(toX, toY);

  // Find the matching legal move
  final legalMoves = game.generateLegalMoves();
  for (var m in legalMoves) {
    if (m.from == fromSq && m.to == toSq) return m;
  }
  return null;
}