summaryrefslogtreecommitdiff
path: root/src/engine/moves
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/moves')
-rw-r--r--src/engine/moves/bishop.c22
-rw-r--r--src/engine/moves/king.c10
-rw-r--r--src/engine/moves/knight.c11
-rw-r--r--src/engine/moves/pawn.c50
-rw-r--r--src/engine/moves/rook.c21
-rw-r--r--src/engine/moves/vec.c17
6 files changed, 80 insertions, 51 deletions
diff --git a/src/engine/moves/bishop.c b/src/engine/moves/bishop.c
index 52b90b3..c51d405 100644
--- a/src/engine/moves/bishop.c
+++ b/src/engine/moves/bishop.c
@@ -14,10 +14,11 @@ void __forloop_bishop_moves_gen(
int og_file = from % 8; // range: [0, 7]
int rank = og_rank + 1, file = og_file + 1, index = rank * 8 + file;
-
+ bitboard_t occupied;
while ((rank < 8 && file < 8) && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = ++rank * 8 + ++file;
@@ -25,8 +26,9 @@ void __forloop_bishop_moves_gen(
rank = og_rank - 1, file = og_file - 1, index = rank * 8 + file;
while ((rank >= 0 && file >= 0) && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = --rank * 8 + --file;
@@ -34,8 +36,9 @@ void __forloop_bishop_moves_gen(
rank = og_rank - 1, file = og_file + 1, index = rank * 8 + file;
while ((rank >= 0 && file < 8) && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = --rank * 8 + ++file;
@@ -43,8 +46,9 @@ void __forloop_bishop_moves_gen(
rank = og_rank + 1, file = og_file - 1, index = rank * 8 + file;
while ((rank < 8 && file >= 0) && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = ++rank * 8 + --file;
diff --git a/src/engine/moves/king.c b/src/engine/moves/king.c
index 29bca79..e954ae5 100644
--- a/src/engine/moves/king.c
+++ b/src/engine/moves/king.c
@@ -7,11 +7,14 @@ void get_king_moves(moves_t* moves, position_t position) {
bitboard_t friendly_king;
int king_square;
bitboard_t friendly_pieces;
+ bitboard_t enemy_pieces;
if (position.turn == WHITE_TURN) {
friendly_pieces = whites(position);
+ enemy_pieces = blacks(position);
friendly_king = position.bitboards[WHITE_KING];
} else {
friendly_pieces = blacks(position);
+ enemy_pieces = whites(position);
friendly_king = position.bitboards[BLACK_KING];
}
king_square = __builtin_ctzll(friendly_king);
@@ -32,7 +35,12 @@ void get_king_moves(moves_t* moves, position_t position) {
while (movement) {
int to = __builtin_ctzll(movement);
movement &= movement - 1;
- add_move(moves, king_square, to);
+ add_move(
+ moves,
+ king_square,
+ to,
+ .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0,
+ );
}
}
diff --git a/src/engine/moves/knight.c b/src/engine/moves/knight.c
index 5f5f9ad..20d072f 100644
--- a/src/engine/moves/knight.c
+++ b/src/engine/moves/knight.c
@@ -79,11 +79,14 @@ void get_knight_moves(moves_t* moves, position_t position) {
bitboard_t friendly_knights;
bitboard_t friendly_pieces;
+ bitboard_t enemy_pieces;
if (position.turn == WHITE_TURN) {
friendly_pieces = whites(position);
+ enemy_pieces = blacks(position);
friendly_knights = position.bitboards[WHITE_KNIGHT];
} else {
friendly_pieces = blacks(position);
+ enemy_pieces = whites(position);
friendly_knights = position.bitboards[BLACK_KNIGHT];
}
@@ -95,7 +98,13 @@ void get_knight_moves(moves_t* moves, position_t position) {
while (ATTACK_MASK) {
int to = __builtin_ctzll(ATTACK_MASK);
ATTACK_MASK &= ATTACK_MASK - 1;
- add_move(moves, from, to);
+ int capture = 0;
+ add_move(
+ moves,
+ from,
+ to,
+ .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0,
+ );
}
}
}
diff --git a/src/engine/moves/pawn.c b/src/engine/moves/pawn.c
index 3cd28b8..0b8993f 100644
--- a/src/engine/moves/pawn.c
+++ b/src/engine/moves/pawn.c
@@ -1,14 +1,14 @@
#include "../moves.h"
+#define add_promote_moves(other_flags) \
+ add_move(moves, from, to, .flags = other_flags | MOVE_PROMOTE_Q); \
+ add_move(moves, from, to, .flags = other_flags | MOVE_PROMOTE_R); \
+ add_move(moves, from, to, .flags = other_flags | MOVE_PROMOTE_B); \
+ add_move(moves, from, to, .flags = other_flags | MOVE_PROMOTE_N)
+
bitboard_t get_white_pawn_forwardmoves(moves_t* moves, position_t position) {
bitboard_t enemy_bitboard = blacks(position);
- bitboard_t friendly_pieces =
- position.bitboards[WHITE_KING] |
- position.bitboards[WHITE_QUEEN] |
- position.bitboards[WHITE_ROOK] |
- position.bitboards[WHITE_BISHOP] |
- position.bitboards[WHITE_KNIGHT] |
- position.bitboards[WHITE_PAWN];
+ bitboard_t friendly_pieces = whites(position);
bitboard_t friendly_pawns = position.bitboards[WHITE_PAWN];
bitboard_t y = friendly_pieces | enemy_bitboard;
bitboard_t x = ~((friendly_pawns << 8) & y) & (friendly_pawns << 8);
@@ -21,13 +21,7 @@ bitboard_t get_white_pawn_forwardmoves(moves_t* moves, position_t position) {
}
bitboard_t get_black_pawn_forwardmoves(moves_t* moves, position_t position) {
bitboard_t enemy_bitboard = whites(position);
- bitboard_t friendly_pieces =
- position.bitboards[BLACK_KING] |
- position.bitboards[BLACK_QUEEN] |
- position.bitboards[BLACK_ROOK] |
- position.bitboards[BLACK_BISHOP] |
- position.bitboards[BLACK_KNIGHT] |
- position.bitboards[BLACK_PAWN];
+ bitboard_t friendly_pieces = blacks(position);
bitboard_t friendly_pawns = position.bitboards[BLACK_PAWN];
bitboard_t y = friendly_pieces | enemy_bitboard;
bitboard_t x = ~((friendly_pawns >> 8) & y) & (friendly_pawns >> 8);
@@ -84,7 +78,9 @@ void get_pawn_moves(moves_t* moves, position_t position) {
if (pawns == 0) next_from = 64;
}
item &= (item - 1);
- add_move(moves, from, to);
+ if (to >= 56 || to <= 7) {
+ add_promote_moves(0);
+ } else add_move(moves, from, to);
}
}
@@ -108,20 +104,24 @@ void get_pawn_moves(moves_t* moves, position_t position) {
if (left != 0) {
bitboard_t MASK = file_masks[left - 1] & friendly_pawns & PASSANT_RANK;
if (MASK != 0) {
- add_move(
- moves,
- left + rank_shift + from_shift,
- position.passantable_file + rank_shift - 1
+ int from = left + rank_shift + from_shift;
+ int to = position.passantable_file + rank_shift - 1;
+ if (to >= 56 || to <= 7) {
+ add_promote_moves(MOVE_EN_PASSANT | MOVE_CAPTURE);
+ } else add_move(
+ moves, from, to, .flags = MOVE_EN_PASSANT | MOVE_CAPTURE
);
}
}
if (right != 0) {
bitboard_t MASK = file_masks[right - 1] & friendly_pawns & PASSANT_RANK;
if (MASK != 0) {
- add_move(
- moves,
- right + rank_shift + from_shift,
- position.passantable_file + rank_shift - 1
+ int from = right + rank_shift + from_shift;
+ int to = position.passantable_file + rank_shift - 1;
+ if (to >= 56 || to <= 7) {
+ add_promote_moves(MOVE_EN_PASSANT | MOVE_CAPTURE);
+ } else add_move(
+ moves, from, to, .flags = MOVE_EN_PASSANT | MOVE_CAPTURE
);
}
}
@@ -157,7 +157,9 @@ void get_pawn_moves(moves_t* moves, position_t position) {
while (MASK) {
int to = __builtin_ctzll(MASK);
MASK &= MASK - 1;
- add_move(moves, from, to);
+ if (to >= 56 || to <= 7) {
+ add_promote_moves(MOVE_CAPTURE);
+ } else add_move(moves, from, to, .flags = MOVE_CAPTURE);
}
}
}
diff --git a/src/engine/moves/rook.c b/src/engine/moves/rook.c
index c5867d5..dfd9c32 100644
--- a/src/engine/moves/rook.c
+++ b/src/engine/moves/rook.c
@@ -15,9 +15,11 @@ void __forloop_rook_moves_gen(
int rank = og_rank + 1, file = og_file, index = rank * 8 + file;
+ bitboard_t occupied;
while (rank < 8 && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = ++rank * 8 + file;
@@ -25,8 +27,9 @@ void __forloop_rook_moves_gen(
rank = og_rank - 1, file = og_file, index = rank * 8 + file;
while (rank >= 0 && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = --rank * 8 + file;
@@ -34,8 +37,9 @@ void __forloop_rook_moves_gen(
rank = og_rank, file = og_file + 1, index = rank * 8 + file;
while (file < 8 && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = rank * 8 + ++file;
@@ -43,8 +47,9 @@ void __forloop_rook_moves_gen(
rank = og_rank, file = og_file - 1, index = rank * 8 + file;
while (file >= 0 && !occupied_by(friendly_pieces, index)) {
- add_move(moves, from, index);
- if (occupied_by(enemy_pieces, index)) {
+ occupied = occupied_by(enemy_pieces, index);
+ add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
+ if (occupied) {
break;
}
index = rank * 8 + --file;
diff --git a/src/engine/moves/vec.c b/src/engine/moves/vec.c
index 6192706..6a5f3c6 100644
--- a/src/engine/moves/vec.c
+++ b/src/engine/moves/vec.c
@@ -2,10 +2,6 @@
#include <assert.h>
#include <stdlib.h>
-void position_make_move(position_t* position, move_t* move) {
-
-}
-
moves_t moves_init() {
return moves_init_wcapacity(16);
}
@@ -32,7 +28,12 @@ int min(int a, int b) {
return a;
}
-void add_move(moves_t* moves, square_t from, square_t to) {
+void _add_move(
+ moves_t* moves,
+ square_t from,
+ square_t to,
+ struct add_move_params params
+) {
assert(moves->moves != 0);
assert(from != to);
@@ -43,9 +44,9 @@ void add_move(moves_t* moves, square_t from, square_t to) {
moves->capacity * sizeof(*moves->moves)
);
}
- moves->moves[moves->length] = (move_t){
+ moves->moves[moves->length++] = (move_t) {
.from = from,
- .to = to
+ .to = to,
+ .flags = params.flags,
};
- moves->length++;
}