commit b2f12e32a27d08f7318fd824c0ca627bacbe1de6
parent f60960a68551a765be30b6012e6f8bf4965c3bab
Author: Benjamin Paul <bpaul@bpaul.xyz>
Date: Wed, 27 Oct 2021 00:00:59 +1000
making progress but move gen is hard!
Diffstat:
5 files changed, 93 insertions(+), 5 deletions(-)
diff --git a/src/bb.c b/src/bb.c
@@ -18,3 +18,16 @@ popbit(bb *b, unsigned char sq) {
* subtract */
*b &= ~(1ULL << sq);
}
+
+unsigned char
+getlsb(bb b) {
+ return __builtin_ctzll(b);
+}
+
+unsigned char
+poplsb(bb *b) {
+ unsigned char bit;
+ bit = getlsb(*b);
+ *b &= *b - 1;
+ return bit;
+}
diff --git a/src/bb.h b/src/bb.h
@@ -6,4 +6,5 @@ typedef unsigned long long bb;
bool testbit(bb, unsigned char);
void setbit(bb *, unsigned char);
void popbit(bb *, unsigned char);
+unsigned char poplsb(bb *);
#endif
diff --git a/src/gen.c b/src/gen.c
@@ -9,9 +9,9 @@
* Get the attacks for the pieces
* Iterate through each piece and create each move based on their attacks
* For pawns, cry
- * For castling, cry */
-
-/* Move structure
+ * For castling, cry
+ *
+ * Move structure
* want a move to be relatively simple to store as we will be making a list of
* moves
* needed information:
@@ -38,13 +38,35 @@ storemove(char from, char to, char movetype, char extra) {
return from + (to << 6) + (movetype << 12) + (extra << 14);
}
+/* When generating moves, we want to make it so that we request for a move, and
+ * the generator gives the next move that needs to be generated. This means
+ * that there is no time wasted generating moves that wont be used, and there
+ * is also no time wasted storing the moves in a list */
+
void
gen_pseudo(pos *p) {
- bb our_pieces[6];
+ char from, to;
+ bb our_pieces[6], att;
for (char pc = PAWN; pc <= KING; pc++) {
our_pieces[pc] = p->pieces[pc] & p->sides[p->turn];
}
+ while ((from = poplsb(&our_pieces[KNIGHT]))) {
+ att = knight_att(from);
+ while ((to = poplsb(&att))) {
+ /* add to move list somehow */
+ storemove(from, to, 0, 0);
+ }
+ }
+
+ while ((from = poplsb(&our_pieces[KING]))) {
+ att = king_att(from);
+ while ((to = poplsb(&att))) {
+ /* add to move list somehow */
+ storemove(from, to, 0, 0);
+ }
+ }
+
return;
}
diff --git a/src/pos.c b/src/pos.c
@@ -169,3 +169,54 @@ print_board(pos *p) {
}
}
#endif
+
+/* Piece attack bitboards */
+
+bb knight_attacks[64];
+bb king_attacks[64];
+
+bb
+knight_att(char sq) {
+ return knight_attacks[sq];
+}
+
+bb
+king_att(char sq) {
+ return king_attacks[sq];
+}
+
+void
+gen_knight_attacks(void) {
+ char sq;
+ for (sq = 0; sq < 64; sq++) {
+ knight_attacks[sq] = (1ULL << sq >> 17) & 0xFEFEFEFEFEFEFEFE;
+ knight_attacks[sq] |= (1ULL << sq >> 15) & 0x7F7F7F7F7F7F7F7F;
+ knight_attacks[sq] |= (1ULL << sq >> 9) & 0xFCFCFCFCFCFCFCFC;
+ knight_attacks[sq] |= (1ULL << sq >> 7) & 0x3F3F3F3F3F3F3F3F;
+ knight_attacks[sq] |= (1ULL << sq << 7) & 0xFCFCFCFCFCFCFCFC;
+ knight_attacks[sq] |= (1ULL << sq << 9) & 0x3F3F3F3F3F3F3F3F;
+ knight_attacks[sq] |= (1ULL << sq << 15) & 0xFEFEFEFEFEFEFEFE;
+ knight_attacks[sq] |= (1ULL << sq << 17) & 0x7F7F7F7F7F7F7F7F;
+ }
+}
+
+void
+gen_king_attacks(void) {
+ char sq;
+ for (sq = 0; sq < 64; sq++) {
+ king_attacks[sq] = (1ULL << sq >> 9) & 0xFEFEFEFEFEFEFEFE;
+ king_attacks[sq] |= (1ULL << sq >> 8);
+ king_attacks[sq] |= (1ULL << sq >> 7) & 0x7F7F7F7F7F7F7F7F;
+ king_attacks[sq] |= (1ULL << sq >> 1) & 0xFEFEFEFEFEFEFEFE;
+ king_attacks[sq] |= (1ULL << sq << 1) & 0x7F7F7F7F7F7F7F7F;
+ king_attacks[sq] |= (1ULL << sq << 7) & 0xFEFEFEFEFEFEFEFE;
+ king_attacks[sq] |= (1ULL << sq << 8);
+ king_attacks[sq] |= (1ULL << sq << 9) & 0x7F7F7F7F7F7F7F7F;
+ }
+}
+
+void
+init_attacks(void) {
+ gen_knight_attacks();
+ gen_king_attacks();
+}
diff --git a/src/pos.h b/src/pos.h
@@ -42,5 +42,6 @@ void parse_fen(pos *, char *);
#ifndef NDEBUG
void print_board(pos *);
#endif
-
+bb knight_att(char);
+bb king_att(char);
#endif