fixed move generation
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <chess/bitboard.h>
|
||||
#include <stdio.h>
|
||||
#include <chess/print.h>
|
||||
|
||||
struct perf_t {
|
||||
char FEN[256];
|
||||
@ -11,11 +12,12 @@ struct perf_t {
|
||||
uint_least64_t nodes;
|
||||
};
|
||||
|
||||
uint_least64_t perft(const struct gameState_t gameState, const uint_least8_t depth) {
|
||||
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);
|
||||
@ -24,6 +26,27 @@ uint_least64_t perft(const struct gameState_t gameState, const uint_least8_t dep
|
||||
return nodes;
|
||||
}
|
||||
|
||||
static void test(struct perf_t perf, const uint_least8_t i) {
|
||||
uint_least64_t board[BITBOARD_LENGTH];
|
||||
const struct gameState_t gameState = newGameState(board, 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(gameState.board, 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
|
||||
@ -32,15 +55,16 @@ int main() {
|
||||
{"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) {
|
||||
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);
|
||||
}
|
||||
test(testPos[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user