summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-08 22:24:54 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-08 22:24:54 +0300
commit0681e521d8a81a640ba2a4019dcc73c8f9e64f5a (patch)
tree1c1f5eea36faf5e4186412d402aa4cb2da1c09d5
parent851cb160f0d2838c75af43dcc7b6fad9969d925c (diff)
downloadc-chess-0681e521d8a81a640ba2a4019dcc73c8f9e64f5a.tar.gz
feat: add only_captures flag to move generation
-rw-r--r--src/main.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/main.c b/src/main.c
index 4327b2e..b950144 100644
--- a/src/main.c
+++ b/src/main.c
@@ -463,7 +463,7 @@ int generate_moves_with_destination(Move* moves, int* board, int color, int dest
return moves_count;
}
-int generate_pseudolegal_moves(Move* moves, int* board, int color) {
+int generate_pseudolegal_moves(Move* moves, int* board, int color, int only_captures) {
int moves_count = 0;
for (int rank = 7; rank >= 0; rank--) {
@@ -494,7 +494,7 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color) {
while(ray_move && *ray_move) {
for (int destination = origin + *ray_move; !(destination & 0x88); destination += *ray_move) {
- if (board[destination] == EMPTY || board[destination] % 2 != color) {
+ if ((board[destination] == EMPTY && !only_captures) || board[destination] % 2 != color) {
if (moves) moves[moves_count] = (Move){ origin, destination, 0 };
moves_count++;
}
@@ -509,10 +509,10 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color) {
// Handle pawns separately
if (piece == (PAWN | WHITE)) {
- if (rank < 7 && board[origin + 16] == EMPTY) {
+ if (rank < 7 && board[origin + 16] == EMPTY && !only_captures) {
if (moves) moves[moves_count] = (Move){ origin, origin + 16, 0 };
moves_count++;
- if (rank == 1 && board[origin + 32] == EMPTY) { // Pawns on rank 1 can move 2 squares up
+ if (rank == 1 && board[origin + 32] == EMPTY && !only_captures) { // Pawns on rank 1 can move 2 squares up
if (moves) moves[moves_count] = (Move){ origin, origin + 32, 0 };
moves_count++;
}
@@ -528,10 +528,10 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color) {
}
}
if (piece == (PAWN | BLACK)) {
- if (rank > 0 && board[origin - 16] == EMPTY) {
+ if (rank > 0 && board[origin - 16] == EMPTY && !only_captures) {
if (moves) moves[moves_count] = (Move){ origin, origin - 16, 0 };
moves_count++;
- if (rank == 6 && board[origin - 32] == EMPTY) { // Pawns on rank 6 can move 2 squares down
+ if (rank == 6 && board[origin - 32] == EMPTY && !only_captures) { // Pawns on rank 6 can move 2 squares down
if (moves) moves[moves_count] = (Move){ origin, origin - 32, 0 };
moves_count++;
}
@@ -548,7 +548,7 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color) {
}
// Castling
- if (piece == (KING | WHITE) && origin == 4) {
+ if (piece == (KING | WHITE) && origin == 4 && !only_captures) {
if (board[1] == EMPTY && board[2] == EMPTY && board[3] == EMPTY && board[0] == (ROOK | WHITE)) {
if (moves) moves[moves_count] = (Move){ origin, origin - 2, 0 };
moves_count++;
@@ -556,7 +556,7 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color) {
if (moves) moves[moves_count] = (Move){ origin, origin + 2, 0 };
moves_count++;
}
- } else if (piece == (KING | BLACK) && origin == 116) {
+ } else if (piece == (KING | BLACK) && origin == 116 && !only_captures) {
if (board[113] == EMPTY && board[114] == EMPTY && board[115] == EMPTY && board[112] == (ROOK | BLACK)) {
if (moves) moves[moves_count] = (Move){ origin, origin - 2, 0 };
moves_count++;
@@ -618,8 +618,8 @@ int static_evaluate(int* board, int precomputed_mobility, int mobility_color) {
int white_material_advantage = compute_material_advantage(board, WHITE);
// If pre-computed mobility has not been passed, compute it
- int mobility = precomputed_mobility ? precomputed_mobility : generate_pseudolegal_moves(NULL, board, mobility_color);
- int opponent_mobility = generate_pseudolegal_moves(NULL, board, mobility_color ^ 1);
+ int mobility = precomputed_mobility ? precomputed_mobility : generate_pseudolegal_moves(NULL, board, mobility_color, 0);
+ int opponent_mobility = generate_pseudolegal_moves(NULL, board, mobility_color ^ 1, 0);
int white_mobility_advantage = (mobility - opponent_mobility) * (mobility_color == WHITE ? 1 : -1);
@@ -687,7 +687,7 @@ Move minimax_search(int* board, int color, int depth, int alpha, int beta, int*
// You only have available moves if your king hasn't been taken
if (compute_material_advantage(board, color) > -INFINITY / 2) {
Move pseudolegal_moves[MAX_AVAILABLE_MOVES];
- int pseudolegal_moves_count = generate_pseudolegal_moves(pseudolegal_moves, board, color);
+ int pseudolegal_moves_count = generate_pseudolegal_moves(pseudolegal_moves, board, color, 0);
int cache_hits = order_moves(pseudolegal_moves, pseudolegal_moves_count, board, hash, transposition_table, zobrist_seed);
for (int i = 0; i < pseudolegal_moves_count; i++) {