stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

commit b14f53fa424df7b70d8ac02b8ae7b619016e8d02
parent 397202fe942470ad24b0bc52461a1c1526cefd84
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 23 Sep 2022 23:17:10 +0200

Generate quiet pawn moves

Diffstat:
Minclude/utils.h | 6+++++-
Msrc/attack.c | 18------------------
Msrc/engine.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/utils.c | 39+++++++++++++++++++++++++++++++++++++++
4 files changed, 106 insertions(+), 42 deletions(-)

diff --git a/include/utils.h b/include/utils.h @@ -17,10 +17,14 @@ extern const U64 universe; extern const U64 notAFile; extern const U64 notHFile; -// useful bit operations +// useful bit functions #define bit_get(bitboard, square) (((bitboard) >> (square)) & C64(1)) #define bit_set(bitboard, square) ((bitboard) |= C64(1) << (square)) #define bit_pop(bitboard, square) ((bitboard) &= ~(C64(1) << (square))) +int bit_count(U64 bitboard); +int bit_lsb_index(U64 bitboard); + +void bitboard_print(U64 bitboard); // squares // clang-format off diff --git a/src/attack.c b/src/attack.c @@ -87,24 +87,6 @@ int hash(U64 key, U64 magic, int relevant_bits) { return (key * magic) >> (64 - relevant_bits); } -static inline int bit_count(U64 bitboard) { - int count = 0; - - while (bitboard > 0) { - count++; - bitboard &= bitboard - 1; - } - - return count; -} - -static inline int bit_lsb_index(U64 bitboard) { - if (!bitboard) - return -1; - - return bit_count((bitboard & -bitboard) - 1); -} - // pseudo random numbers U32 state = C32(1804289383); diff --git a/src/engine.c b/src/engine.c @@ -164,6 +164,66 @@ int CBoard_square_isAttack(CBoard_T self, Square square, eColor side) { return 0; } +int square_index_extractor(U64 btbrd) { + static U64 bitboard; + + if (btbrd != no_sq) + bitboard = btbrd; + + if (bitboard == C64(0)) + return no_sq; + + int index = bit_lsb_index(bitboard); + bit_pop(bitboard, index); + return index; +} + +void CBoard_move_generate(CBoard_T self) { + Square source, target; + U64 bitboard, attack; + + U64 occupancy = self->colorBB[WHITE] | self->colorBB[BLACK]; + for (int color = 0; color < 2; color++) { + // Generate quiet pawn moves + { + int add = (color == WHITE) ? +8 : -8; + bitboard = self->pieceBB[PAWN] & self->colorBB[color]; + while (bitboard) { + target = source = bit_lsb_index(bitboard); + target += add; + if (target > a1 && target < h8 && !bit_get(occupancy, target)) { + // promote + if ((color == WHITE && source >= a7 && source <= h7) || + (color == BLACK && source >= a2 && source <= h2)) { + // add move to move list + printf("PROMOTION!!! "); + } else { + // one ahead + // add move to move list + printf("SINGLE PUSH!!! "); + + // two ahead + if (((color == BLACK && source >= a7 && source <= h7) || + (color == WHITE && source >= a2 && source <= h2)) && + !bit_get(occupancy, target + add)) { + // add to move list; + printf("DOUBLE PUSH!!! "); + } + } + printf("%s pawn: %s; target: %s\n", + (color == WHITE) ? "white" : "black", + square_to_coordinates[source], square_to_coordinates[target]); + } + bit_pop(bitboard, source); + } + + /* for (int piece = 0; piece < 6; piece++) { */ + /* bitboard = self->pieceBB[piece] & self->colorBB[color]; */ + /* } */ + } + } +} + void CBoard_print(CBoard_T self) { for (int rank = 0; rank < 8; rank++) { for (int file = 0; file < 8; file++) { @@ -217,25 +277,6 @@ void CBoard_print_attacked(CBoard_T self, eColor side) { printf("\n"); } -void bitboard_print(U64 bitboard) { - for (int rank = 0; rank < 8; rank++) { - for (int file = 0; file < 8; file++) { - Square square = (7 - rank) * 8 + file; - - if (!file) - printf(" %d ", 8 - rank); - - printf("%d ", bit_get(bitboard, square) ? 1 : 0); - } - printf("\n"); - } - - printf("\n A B C D E F G H\n\n"); - printf(" Bitboard: %llud\n\n", bitboard); -} - -// clang-format on - void init_all() { init_leapers_attacks(); init_sliders_attacks(); @@ -245,12 +286,10 @@ int main(void) { init_all(); CBoard_T board; - board = CBoard_fromFEN( - NULL, "rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1 "); + board = CBoard_fromFEN(NULL, tricky_position); CBoard_print(board); - CBoard_print_attacked(board, WHITE); - CBoard_print_attacked(board, BLACK); + CBoard_move_generate(board); FREE(board); return 0; diff --git a/src/utils.c b/src/utils.c @@ -1,3 +1,5 @@ +#include <stdio.h> + #include "utils.h" const U64 universe = C64(0xffffffffffffffff); // @@ -17,6 +19,7 @@ U64 noWeOne(U64 b) { return (b & notAFile) << 7; } U64 rotateLeft(U64 x, int s) { return (x << s) | (x >> (64 - s)); } U64 rotateRight(U64 x, int s) { return (x >> s) | (x << (64 - s)); } +// clang-format off const char *square_to_coordinates[]={ "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", @@ -27,3 +30,39 @@ const char *square_to_coordinates[]={ "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7", "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", " " }; +// clang-format on + +int bit_count(U64 bitboard) { + int count = 0; + + while (bitboard > 0) { + count++; + bitboard &= bitboard - 1; + } + + return count; +} + +int bit_lsb_index(U64 bitboard) { + if (!bitboard) + return -1; + + return bit_count((bitboard & -bitboard) - 1); +} + +void bitboard_print(U64 bitboard) { + for (int rank = 0; rank < 8; rank++) { + for (int file = 0; file < 8; file++) { + Square square = (7 - rank) * 8 + file; + + if (!file) + printf(" %d ", 8 - rank); + + printf("%d ", bit_get(bitboard, square) ? 1 : 0); + } + printf("\n"); + } + + printf("\n A B C D E F G H\n\n"); + printf(" Bitboard: %llud\n\n", bitboard); +}