#include "chess/types.h" #include #include #include #include #include #include struct perf_t { char FEN[256]; uint_least8_t depth; uint_least64_t nodes; }; static uint_least64_t perft(const struct gameState_t gameState, const uint_least8_t depth) { struct move_t moves[MAX_VALID_MOVES]; uint_least64_t nodes = 0; const uint_least8_t movesLength = validMoves(gameState, moves); if(depth == 1) return movesLength; //if(depth == 0) return 1; for(uint_least8_t i = 0; i < movesLength; ++i) { const struct gameState_t newGameState = makeMove(gameState, moves[i]); nodes += perft(newGameState, depth - 1); undoMove(newGameState, moves[i], gameState.color); } return nodes; } static void test(struct perf_t perf, const uint_least8_t i) { uint_least64_t board[BITBOARD_LENGTH]; struct zobristTableElement repetitionTableStore[REPETETION_TABLE_LENGTH]; const struct gameState_t gameState = newGameState(board, repetitionTableStore, perf.FEN); const uint_least64_t nodes = perft(gameState, perf.depth); if(perf.nodes != nodes) { printf("Test %" PRIuLEAST16 " failed: FEN: %s, depth: %" PRIuLEAST8 " calculated nodes: %" PRIuLEAST64 ", expected nodes: %" PRIuLEAST64 "\n", i, perf.FEN, perf.depth, nodes, perf.nodes); struct move_t moves[MAX_VALID_MOVES]; const uint_least8_t movesLength = validMoves(gameState, moves); for(uint_least8_t j = 0; j < movesLength; ++j) { const struct gameState_t newGameState = makeMove(gameState, moves[j]); const uint_least64_t nodes = perft(newGameState, perf.depth - 1); undoMove(newGameState, moves[j], gameState.color); { printMove(moves[j]); printf(" %" PRIuLEAST64 "\n", nodes); } } } } int main() { struct perf_t testPos[] = { {"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 5, 4865609}, // start Position 0 {"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq 0 1", 4, 4085603}, // 1 {"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - 0 1", 5, 674624}, // 2 {"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", 4, 422333}, // 3 {"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8", 4, 2103487}, // 4 {"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10", 4, 3894594}, // 5 {"5k2/5pp1/1b3pP1/6P1/rN3P2/p5Nn/3r4/5n1K w - - 0 1", 5, 3949613}, // 6 {"8/r2q2rQ/1b1pbkPp/BPPnNB2/pP2ppRP/n2pPpp1/PKR4P/1N6 w - - 0 1", 4, 2349215}, // 7 {"n2Q2R1/K1B1pkbP/2p2P1P/2rPpB2/pq1p1Ppn/1PRN1bP1/P2p1p2/Nr6 w - - 0 1", 4, 4382892}, // 8 {"B3n2N/1p1p3p/1Pr1P2B/3ppk2/P2q1p1p/Pbr1p1PP/P4NP1/b2RKnRQ w - - 0 1", 4, 1185726}, // 9 {"8/1P2Q3/7R/3P4/R7/5p2/K7/2k4n w - - 0 1", 5, 5509638}, // 10 {"8/N6P/K7/8/2RP4/p2n4/r7/5k2 w - - 0 1", 5, 4119222}, // 11 {"n7/6kq/8/4P1p1/K7/3P4/3p1p2/8 w - - 0 1", 6, 11762127}, // 12 }; initMagicTable(); for(uint_least16_t i = 0; i < LENGTH(testPos); ++i) { test(testPos[i], i); } }