#include "chess/types.h" #include #include #include #include #include struct perf_t { char FEN[256]; uint_least8_t depth; uint_least64_t nodes; }; 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; for(uint_least8_t i = 0; i < movesLength; ++i) { const struct gameState_t newGameState = makeMove(gameState, moves[i]); nodes += perft(newGameState, depth - 1); undoMove(gameState.board, moves[i], gameState.color); } return 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 }; initMagicTable(); for(uint_least16_t i = 0; i < LENGTH(testPos); ++i) { uint_least64_t board[BITBOARD_LENGTH]; const struct gameState_t gameState = newGameState(board, testPos[i].FEN); const uint_least64_t nodes = perft(gameState, testPos[i].depth); if(testPos[i].nodes != nodes) { printf("Test %" PRIuLEAST16 " failed: FEN: %s, depth: %" PRIuLEAST8 " calculated nodes: %" PRIuLEAST64 ", expected nodes: %" PRIuLEAST64 "\n", i, testPos[i].FEN, testPos[i].depth, nodes, testPos[i].nodes); } } }