summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bitboard.h32
-rw-r--r--src/main.c30
-rw-r--r--src/search/pawn_moves.c78
3 files changed, 98 insertions, 42 deletions
diff --git a/src/bitboard.h b/src/bitboard.h
index 39f5d80..a3735d1 100644
--- a/src/bitboard.h
+++ b/src/bitboard.h
@@ -33,22 +33,22 @@ enum {
BLACK_TURN,
};
-#define BITMASK_RANK_A 255ULL
-#define BITMASK_RANK_B 65280ULL
-#define BITMASK_RANK_C 16711680ULL
-#define BITMASK_RANK_D 4278190080ULL
-#define BITMASK_RANK_E 1095216660480ULL
-#define BITMASK_RANK_F 280375465082880ULL
-#define BITMASK_RANK_G 71776119061217280ULL
-#define BITMASK_RANK_H 18374686479671623680ULL
-#define BITMASK_COLM_1 72340172838076673ULL
-#define BITMASK_COLM_2 144680345676153346ULL
-#define BITMASK_COLM_3 289360691352306692ULL
-#define BITMASK_COLM_4 578721382704613384ULL
-#define BITMASK_COLM_5 1157442765409226768ULL
-#define BITMASK_COLM_6 2314885530818453536ULL
-#define BITMASK_COLM_7 4629771061636907072ULL
-#define BITMASK_COLM_8 9259542123273814144ULL
+#define BITMASK_RANK_1 255ULL
+#define BITMASK_RANK_2 65280ULL
+#define BITMASK_RANK_3 16711680ULL
+#define BITMASK_RANK_4 4278190080ULL
+#define BITMASK_RANK_5 1095216660480ULL
+#define BITMASK_RANK_6 280375465082880ULL
+#define BITMASK_RANK_7 71776119061217280ULL
+#define BITMASK_RANK_8 18374686479671623680ULL
+#define BITMASK_FILE_A 72340172838076673ULL
+#define BITMASK_FILE_B 144680345676153346ULL
+#define BITMASK_FILE_C 289360691352306692ULL
+#define BITMASK_FILE_D 578721382704613384ULL
+#define BITMASK_FILE_E 1157442765409226768ULL
+#define BITMASK_FILE_F 2314885530818453536ULL
+#define BITMASK_FILE_G 4629771061636907072ULL
+#define BITMASK_FILE_H 9259542123273814144ULL
typedef struct {
bitboard_t bitboards[PIECE_TYPE_COUNT];
diff --git a/src/main.c b/src/main.c
index fdff93d..c2a2a52 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,22 +6,38 @@ int main() {
moves_t moves = moves_init();
position_t position = position_starting();
+// position.bitboards[WHITE_KING] = 16ULL;
+// position.bitboards[WHITE_QUEEN] = 8ULL;
+// position.bitboards[WHITE_ROOK] = 129ULL;
+// position.bitboards[WHITE_BISHOP] = 36ULL;
+// position.bitboards[WHITE_KNIGHT] = 66ULL;
+// position.bitboards[WHITE_PAWN] = 9261338880ULL;
+// position.bitboards[BLACK_KING] = 1152921504606846976ULL;
+// position.bitboards[BLACK_QUEEN] = 576460752303423488ULL;
+// position.bitboards[BLACK_ROOK] = 9295429630892703744ULL;
+// position.bitboards[BLACK_BISHOP] = 2594073385365405696ULL;
+// position.bitboards[BLACK_KNIGHT] = 4755801206503243776ULL;
+// position.bitboards[BLACK_PAWN] = 43628777082716160ULL;
position.bitboards[WHITE_KING] = 16ULL;
-position.bitboards[WHITE_QUEEN] = 8ULL;
-position.bitboards[WHITE_ROOK] = 129ULL;
+position.bitboards[WHITE_QUEEN] = 274877906944ULL;
+position.bitboards[WHITE_ROOK] = 33554560ULL;
position.bitboards[WHITE_BISHOP] = 36ULL;
position.bitboards[WHITE_KNIGHT] = 66ULL;
-position.bitboards[WHITE_PAWN] = 85899406080ULL;
+position.bitboards[WHITE_PAWN] = 18016057038217216ULL;
position.bitboards[BLACK_KING] = 1152921504606846976ULL;
position.bitboards[BLACK_QUEEN] = 576460752303423488ULL;
position.bitboards[BLACK_ROOK] = 9295429630892703744ULL;
-position.bitboards[BLACK_BISHOP] = 2594073385365405696ULL;
+position.bitboards[BLACK_BISHOP] = 288300744895889408ULL;
position.bitboards[BLACK_KNIGHT] = 4755801206503243776ULL;
-position.bitboards[BLACK_PAWN] = 69524353607270400ULL;
+position.bitboards[BLACK_PAWN] = 7459109431427072ULL;
- position.passantable_column = 4;
- position.turn = WHITE_TURN;
+ position.passantable_column = 3;
+ position.turn = BLACK_TURN;
get_pawn_moves(&moves, position);
+
+ for (int i = 0; i < moves.length; i++) {
+ printf("%d %d | ", (moves.moves[i].from), (moves.moves[i].to));
+ }
return 0;
}
diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c
index f015e7d..5d94571 100644
--- a/src/search/pawn_moves.c
+++ b/src/search/pawn_moves.c
@@ -1,5 +1,4 @@
#include "../search.h"
-#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
@@ -15,7 +14,7 @@ bitboard_t get_white_pawn_forwardmoves(moves_t* moves, position_t 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);
- bitboard_t first_move_x = x & BITMASK_RANK_C;
+ bitboard_t first_move_x = x & BITMASK_RANK_3;
first_move_x |= (first_move_x << 8) & (~y);
x |= first_move_x;
@@ -33,8 +32,8 @@ bitboard_t get_black_pawn_forwardmoves(moves_t* moves, position_t position) {
position.bitboards[BLACK_PAWN];
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;
+ bitboard_t x = ~((friendly_pawns >> 8) & y) & (friendly_pawns >> 8);
+ bitboard_t first_move_x = x & BITMASK_RANK_6;
first_move_x |= (first_move_x >> 8) & (~y);
x |= first_move_x;
@@ -47,38 +46,44 @@ void get_pawn_moves(moves_t* moves, position_t position) {
bitboard_t forward_moves;
bitboard_t friendly_pawns;
+ bitboard_t enemy_bitboard;
if (position.turn == WHITE_TURN) {
forward_moves = get_white_pawn_forwardmoves(moves, position);
friendly_pawns = position.bitboards[WHITE_PAWN];
+ enemy_bitboard = blacks(position);
} else {
forward_moves = get_black_pawn_forwardmoves(moves, position);
friendly_pawns = position.bitboards[BLACK_PAWN];
+ enemy_bitboard = whites(position);
}
- bitboard_t column_masks[8] = {
- BITMASK_COLM_1,
- BITMASK_COLM_2,
- BITMASK_COLM_3,
- BITMASK_COLM_4,
- BITMASK_COLM_5,
- BITMASK_COLM_6,
- BITMASK_COLM_7,
- BITMASK_COLM_8,
+ bitboard_t file_masks[8] = {
+ BITMASK_FILE_A,
+ BITMASK_FILE_B,
+ BITMASK_FILE_C,
+ BITMASK_FILE_D,
+ BITMASK_FILE_E,
+ BITMASK_FILE_F,
+ BITMASK_FILE_G,
+ BITMASK_FILE_H,
};
for (int i = 0; i < 8; i++) {
- bitboard_t item = column_masks[i] & forward_moves;
- bitboard_t pawns = column_masks[i] & friendly_pawns;
+ bitboard_t item = file_masks[i] & forward_moves;
+ bitboard_t pawns = file_masks[i] & friendly_pawns;
int from = __builtin_ctzll(pawns);
pawns &= (pawns - 1);
int next_from = __builtin_ctzll(pawns);
+ if (pawns == 0) next_from = 64; // to work around UB
+
while (item) {
int to = __builtin_ctzll(item);
if (to > next_from) {
from = next_from;
pawns &= (pawns - 1);
next_from = __builtin_ctzll(pawns);
+ if (pawns == 0) next_from = 64;
}
item &= (item - 1);
add_move(moves, from, to);
@@ -90,11 +95,11 @@ void get_pawn_moves(moves_t* moves, position_t position) {
int rank_shift;
int from_shift;
if (position.turn == WHITE_TURN) {
- PASSANT_RANK = BITMASK_RANK_E;
+ PASSANT_RANK = BITMASK_RANK_5;
rank_shift = 40;
from_shift = -9;
} else {
- PASSANT_RANK = BITMASK_RANK_D;
+ PASSANT_RANK = BITMASK_RANK_4;
from_shift = 7;
rank_shift = 16;
}
@@ -103,7 +108,7 @@ void get_pawn_moves(moves_t* moves, position_t position) {
if (position.passantable_column > 1) left = position.passantable_column - 1;
if (position.passantable_column < 8) right = position.passantable_column + 1;
if (left != 0) {
- bitboard_t MASK = column_masks[left - 1] & friendly_pawns & PASSANT_RANK;
+ bitboard_t MASK = file_masks[left - 1] & friendly_pawns & PASSANT_RANK;
if (MASK != 0) {
add_move(
moves,
@@ -113,7 +118,7 @@ void get_pawn_moves(moves_t* moves, position_t position) {
}
}
if (right != 0) {
- bitboard_t MASK = column_masks[right - 1] & friendly_pawns & PASSANT_RANK;
+ bitboard_t MASK = file_masks[right - 1] & friendly_pawns & PASSANT_RANK;
if (MASK != 0) {
add_move(
moves,
@@ -123,4 +128,39 @@ void get_pawn_moves(moves_t* moves, position_t position) {
}
}
}
+
+
+ bitboard_t ATTACK_MASK;
+ int shift;
+ if (position.turn == WHITE_TURN) {
+ shift = 0;
+ } else {
+ shift = -16;
+ }
+ for (int i = 0; i < 8; i++) {
+ if (i == 0) ATTACK_MASK = 512;
+ else if (i == 7) ATTACK_MASK = 128;
+ else {
+ ATTACK_MASK = 640;
+ }
+ bitboard_t pawns = file_masks[i] & friendly_pawns;
+ while (pawns) {
+ int from = __builtin_ctzll(pawns);
+ pawns &= pawns - 1;
+
+ bitboard_t MASK;
+ if (from + shift > 0) {
+ MASK = ATTACK_MASK << (from + shift);
+ } else {
+ MASK = ATTACK_MASK >> -(from + shift);
+ }
+
+ MASK &= enemy_bitboard;
+ while (MASK) {
+ int to = __builtin_ctzll(MASK);
+ MASK &= MASK - 1;
+ add_move(moves, from, to);
+ }
+ }
+ }
}