summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
Diffstat (limited to 'src/search')
-rw-r--r--src/search/moves.c13
-rw-r--r--src/search/pawn_moves.c47
2 files changed, 55 insertions, 5 deletions
diff --git a/src/search/moves.c b/src/search/moves.c
index 57797e7..7089886 100644
--- a/src/search/moves.c
+++ b/src/search/moves.c
@@ -1,11 +1,12 @@
#include "../search.h"
+#include <assert.h>
#include <stdlib.h>
-moves_t init_moves() {
- return init_moves_wcapacity(16);
+moves_t moves_init() {
+ return moves_init_wcapacity(16);
}
-moves_t init_moves_wcapacity(u32 capacity) {
+moves_t moves_init_wcapacity(u32 capacity) {
move_t* moves = malloc(capacity * sizeof(*moves));
return (moves_t) {
.moves = moves,
@@ -14,7 +15,7 @@ moves_t init_moves_wcapacity(u32 capacity) {
};
}
-moves_t empty_moves() {
+moves_t moves_empty() {
return (moves_t) {
.moves = 0,
.capacity = 0,
@@ -28,7 +29,9 @@ int min(int a, int b) {
}
void add_move(moves_t* moves, square_t from, square_t to) {
- if (moves->moves == 0) return;
+ assert(moves->moves != 0);
+ assert(from != to);
+
if (moves->length + 1 >= moves->capacity) {
moves->capacity += min(32, moves->capacity);
moves->moves = realloc(
diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c
index e69de29..58eac83 100644
--- a/src/search/pawn_moves.c
+++ b/src/search/pawn_moves.c
@@ -0,0 +1,47 @@
+#include "../search.h"
+#include <assert.h>
+
+void get_white_pawn_moves(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];
+ 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);
+ bitboard_t first_move_x = x | BITMASK_RANK_C;
+
+ first_move_x |= (first_move_x << 8) & (~y);
+ x |= first_move_x;
+
+ print_bitboard(x);
+}
+void get_black_pawn_moves(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];
+ 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);
+ bitboard_t first_move_x = x | BITMASK_RANK_F;
+
+ first_move_x |= (first_move_x >> 8) & (~y);
+ x |= first_move_x;
+}
+
+void get_pawn_moves(moves_t* moves, position_t position) {
+ assert_valid_position(position);
+
+ if (position.turn == WHITE_TURN) {
+ get_white_pawn_moves(moves, position);
+ } else {
+ get_black_pawn_moves(moves, position);
+ }
+}