main function

void main()

Implementation

void main() async {
  // 1. Setup Architecture
  final transformer = TransformerDecoder(vocabSize: 4098, embedSize: 128);
  final model = MuZeroModel(transformer, 128);

  // 2. Load Weights
  print("Loading weights...");
  await loadModuleParameters(transformer, "muzero_chess_v1.json");

  // 3. Initialize Bishop Game
  final game = Game(variant: Variant.standard());
  List<int> history = [0]; // Model's sequence starting token

  print("\n--- Model Play Test ---\n");
  print(game.ascii()); // Print starting board

  for (int turn = 0; turn < 20; turn++) {
    // 4. Get Model Prediction
    final state = model.represent(history);
    final prediction = model.predict(state);
    final policyLogits = prediction['policy']!.data;

    // 5. Mask & Select Best Legal Move
    Move? bestMove;
    double maxLogit = -double.infinity;

    final legalMoves = game.generateLegalMoves();
    if (legalMoves.isEmpty) {
      print("Game Over: No legal moves left.");
      break;
    }

    for (var m in legalMoves) {
      int idx = encodeMove(m, game);
      if (policyLogits[idx] > maxLogit) {
        maxLogit = policyLogits[idx];
        bestMove = m;
      }
    }

    if (bestMove != null) {
      // 6. Execute Move
      String san = game.toSan(bestMove);
      int moveIdx = encodeMove(bestMove, game);

      game.makeMove(bestMove);
      history.add(moveIdx);
      if (history.length > 16) history.removeAt(0);

      print("Turn ${turn + 1}: Model played $san (Index: $moveIdx)");
      print(game.ascii());
      print("--------------------------------");
    } else {
      print("Error: Model couldn't pick a legal move.");
      break;
    }

    // Slight delay so you can watch the game
    await Future.delayed(Duration(milliseconds: 500));
  }

  print("\nFinal PGN: ${game.pgn()}");
}