summaryrefslogtreecommitdiff
path: root/src/engine/moves.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-16 23:03:28 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-18 14:04:14 +0530
commitc34b4c1b036058b2906556850cc4ded6ececcf89 (patch)
tree0da79a6578ed85123abbc02141f5d1d989bb3aa3 /src/engine/moves.c
parentf1c6f59ae02a9d9db50171a69032dc2409a0e6c6 (diff)
fixed pawn gen, onto the next perf
Diffstat (limited to 'src/engine/moves.c')
-rw-r--r--src/engine/moves.c75
1 files changed, 36 insertions, 39 deletions
diff --git a/src/engine/moves.c b/src/engine/moves.c
index 3aff11e..0c923a1 100644
--- a/src/engine/moves.c
+++ b/src/engine/moves.c
@@ -221,14 +221,17 @@ void perft_divide(position_t position, int depth) {
moves_t moves;
moves_init(&moves);
get_legal_moves(&moves, position);
+ int nodes = 0;
for (int i = 0; i < moves.length; i++) {
position_t copy = position;
position_make_move(&copy, moves.moves[i]);
int c = count_positions(copy, depth - 1);
+ nodes += c;
printf(" %c%d%c%d: %d\n",
'a' + (moves.moves[i].from % 8), 1 + (moves.moves[i].from / 8),
'a' + (moves.moves[i].to % 8), 1 + (moves.moves[i].to / 8), c);
}
+ printf("\nNodes: %d\n", nodes);
moves_deinit(moves);
}
@@ -238,19 +241,17 @@ bool test_perft_starting_position() {
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
).position;
int d;
- d = count_positions(position, 0); printf("d0: %d\n", d); if (d != 1) return false;
- d = count_positions(position, 1); printf("d1: %d\n", d); if (d != 20) return false;
- d = count_positions(position, 2); printf("d2: %d\n", d); if (d != 400) return false;
- d = count_positions(position, 3); printf("d3: %d\n", d); if (d != 8902) return false;
- d = count_positions(position, 4); printf("d4: %d\n", d); if (d != 197281) return false;
+ d = count_positions(position, 0); if (d != 1) return false;
+ d = count_positions(position, 1); if (d != 20) return false;
+ d = count_positions(position, 2); if (d != 400) return false;
+ d = count_positions(position, 3); if (d != 8902) return false;
+ d = count_positions(position, 4); if (d != 197281) return false;
position_t castle_pos = load_fen(
"r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1"
).position;
- int cp = count_positions(castle_pos, 1);
- printf("R3K2R d1: %d (expected 26)\n", cp);
-
- d = count_positions(position, 5); printf("d5: %d\n", d); if (d != 4865609) return false;
+ d = count_positions(castle_pos, 1); if (d != 26) return false;
+ d = count_positions(position, 5); if (d != 4865609) return false;
return true;
}
@@ -259,37 +260,33 @@ bool test_perft_kiwipete_position() {
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
).position;
int d;
- d = count_positions(position, 1); printf("d1: %d (expected 48)\n", d); if (d != 48) return false;
- printf("--- Kiwipete d2 divide ---\n"); perft_divide(position, 2);
- d = count_positions(position, 2); printf("d2: %d (expected 2039)\n", d); if (d != 2039) return false;
+ d = count_positions(position, 1); if (d != 48) return false;
+ d = count_positions(position, 2); if (d != 2039) return false;
+ d = count_positions(position, 3); if (d != 97862) return false;
+ d = count_positions(position, 4); if (d != 4085603) return false;
- printf("\n--- Debug: investigate e5g6 ---\n");
- {
- moves_t moves;
- moves_init(&moves);
- get_legal_moves(&moves, position);
- for (int i = 0; i < moves.length; i++) {
- if (moves.moves[i].from == 36 && moves.moves[i].to == 46) {
- position_t copy = position;
- position_make_move(&copy, moves.moves[i]);
- printf("After Nxe5g6, turn=%d, is e8 attacked by white? %d\n",
- copy.turn, square_attacked(copy, 60, WHITE_TURN));
- moves_t black_moves;
- moves_init(&black_moves);
- get_legal_moves(&black_moves, copy);
- printf("Black has %d legal moves:\n", black_moves.length);
- for (int j = 0; j < black_moves.length; j++) {
- printf(" %c%d%c%d flags=%d\n",
- 'a' + (black_moves.moves[j].from % 8), 1 + (black_moves.moves[j].from / 8),
- 'a' + (black_moves.moves[j].to % 8), 1 + (black_moves.moves[j].to / 8),
- black_moves.moves[j].flags);
- }
- moves_deinit(black_moves);
- break;
- }
- }
- moves_deinit(moves);
- }
+ // my move gen too slow to verify this
+ // d = count_positions(position, 5); if (d != 193690690) return false;
+
+ return true;
+}
+
+bool test_perft_position3() {
+ position_t position = load_fen(
+ "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
+ ).position;
+ int d;
+ d = count_positions(position, 1); if (d != 14) return false;
+ d = count_positions(position, 2); if (d != 191) return false;
+ d = count_positions(position, 3); if (d != 2812) return false;
+ d = count_positions(position, 4); if (d != 43238) return false;
+ d = count_positions(position, 5); if (d != 674624) return false;
+ perft_divide(position, 6);
+ d = count_positions(position, 6);
+ printf("%d\n", d);
+ if (d != 11030083) return false;
+ d = count_positions(position, 7); if (d != 178633661) return false;
+ d = count_positions(position, 8); if (d != 3009794393) return false;
return true;
}