File size: 3,528 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0089", "name": "Implement perft (performance test) to verify move generation correctness"}
{"type": "message", "message": {"role": "system", "content": "You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance."}}
{"type": "message", "message": {"role": "user", "content": "Implement perft (performance test) to verify move generation correctness"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <cstdio>\n\nstruct PerftResult {\n    uint64_t nodes;\n    uint64_t captures;\n    uint64_t enPassant;\n    uint64_t castles;\n    uint64_t promotions;\n    uint64_t checks;\n    uint64_t checkmates;\n    uint64_t stalemates;\n};\n\nPerftResult perft(BitboardBoard& board, int depth) {\n    PerftResult result = {0, 0, 0, 0, 0, 0, 0, 0};\n\n    if (depth == 0) {\n        result.nodes = 1;\n        return result;\n    }\n\n    Move moves[256];\n    int numMoves = generateLegalMoves(board, moves);\n\n    if (depth == 1 && numMoves == 0) {\n        if (isInCheck(board)) result.checkmates = 1;\n        else result.stalemates = 1;\n        result.nodes = 1;\n        return result;\n    }\n\n    for (int i = 0; i < numMoves; i++) {\n        PerftResult child;\n        makeMove(board, moves[i]);\n\n        child = perft(board, depth - 1);\n\n        result.nodes += child.nodes;\n        result.captures += child.captures + (isCapture(moves[i]) ? 1 : 0);\n        result.enPassant += child.enPassant + (isEnPassant(moves[i]) ? 1 : 0);\n        result.castles += child.castles + (isCastle(moves[i]) ? 1 : 0);\n        result.promotions += child.promotions + (isPromotion(moves[i]) ? 1 : 0);\n        result.checks += child.checks + (isInCheck(board) ? 1 : 0);\n        result.checkmates += child.checkmates;\n        result.stalemates += child.stalemates;\n\n        unmakeMove(board, moves[i]);\n    }\n\n    return result;\n}\n\nvoid perftDivide(BitboardBoard& board, int depth) {\n    Move moves[256];\n    int numMoves = generateLegalMoves(board, moves);\n    uint64_t total = 0;\n\n    printf(\"Perft(%d) divide:\\n\", depth);\n    for (int i = 0; i < numMoves; i++) {\n        makeMove(board, moves[i]);\n        PerftResult result = perft(board, depth - 1);\n        unmakeMove(board, moves[i]);\n\n        char moveStr[8];\n        formatMove(moves[i], moveStr);\n        printf(\"%s: %llu\\n\", moveStr, result.nodes);\n        total += result.nodes;\n    }\n    printf(\"Total: %llu\\n\", total);\n}\n\nvoid runPerftTests() {\n    struct TestCase { const char* fen; int depth; uint64_t expected; };\n    TestCase tests[] = {\n        {\"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\", 5, 4865609ULL},\n        {\"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1\", 4, 4085603ULL},\n        {\"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1\", 4, 43237ULL},\n        {\"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1\", 4, 4085603ULL},\n        {\"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8\", 3, 62379ULL},\n    };\n\n    for (auto& tc : tests) {\n        BitboardBoard board;\n        board.loadFEN(tc.fen);\n        PerftResult result = perft(board, tc.depth);\n        printf(\"FEN: %s\\nDepth %d: %llu (expected %llu) %s\\n\",\n               tc.fen, tc.depth, result.nodes, tc.expected,\n               result.nodes == tc.expected ? \"PASS\" : \"FAIL\");\n    }\n}"}}