stellar

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

commita8feaaca3d441954200377a3aac1c374bcaa1881
parenta5b0115a1f2e854c94cacdddc0640bf74f257d36
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateThu, 29 Sep 2022 14:35:11 +0200

Start writing UCI commands

Diffstat:
Minclude/utils.h|++
Msrc/CBoard.c|+-
Msrc/engine.c|++++++++++++++++++++++++++++++++----
Msrc/utils.c|++++

4 files changed, 39 insertions(+), 5 deletions(-)


diff --git a/include/utils.h b/include/utils.h

@@ -45,6 +45,7 @@ enum enumSquare {

typedef enum enumSquare Square;
extern const char *square_to_coordinates[];
Square coordinates_to_square(char * cord);
// board moving
typedef U64 (*direction_f)(U64);

@@ -72,4 +73,5 @@ int get_time_ms(void);

typedef U64 (*attack_f)(Square square, U64 occupancy);
#endif

diff --git a/src/CBoard.c b/src/CBoard.c

@@ -258,7 +258,7 @@ CBoard_T CBoard_fromFEN(CBoard_T board, char *fen) {

fen++;
if (*fen != '-') {
board->enpassant = (*(fen + 1) - '1') * 8 + (*fen - 'a');
board->enpassant = coordinates_to_square(fen);
}
return board;

diff --git a/src/engine.c b/src/engine.c

@@ -1,3 +1,4 @@

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

@@ -100,7 +101,7 @@ MoveList_T generate_moves(CBoard_T cboard, MoveList_T moves) {

{ // pawn moves
Piece_T Piece = Piece_get(PAWN, color);
int index = Piece_index(Piece);
U64 bitboard = CBoard_getPieceSet(cboard, Piece);
U64 bitboard = CBoard_pieceSet(cboard, Piece);
bitboard_for_each_bit(src, bitboard) {
{ // quiet
int add = (color == WHITE) ? +8 : -8;

@@ -143,7 +144,7 @@ MoveList_T generate_moves(CBoard_T cboard, MoveList_T moves) {

// All piece move
for (int piece = 1; piece < 6; piece++) {
Piece_T Piece = Piece_get(piece, color);
U64 bitboard = CBoard_getPieceSet(cboard, Piece);
U64 bitboard = CBoard_pieceSet(cboard, Piece);
bitboard_for_each_bit(src, bitboard) {
U64 attack = CBoard_piece_attacks(cboard, Piece, src) &
~CBoard_colorBB(cboard, color);

@@ -315,11 +316,38 @@ void init_all() {

init_sliders_attacks();
}
/* UCI */
Move parse_move(CBoard_T self, char *move_string) {
MoveList_T moves = generate_moves(self, NULL);
Square source = coordinates_to_square(move_string);
Square target = coordinates_to_square(move_string + 2);
for (int i = 0; i < moves->count; i++) {
Move move = moves->moves[i];
if (Move_source(move) == source && Move_target(move) == target) {
if (move_string[4]) {
Piece_T promoted = Piece_fromIndex(Move_promote(move));
if (tolower(Piece_code(promoted)) != move_string[4])
continue;
}
return move;
}
}
return 0;
}
int main(void) {
init_all();
CBoard_T board = CBoard_fromFEN(NULL, tricky_position);
perft_test(board, 5);
CBoard_T board = CBoard_fromFEN(NULL, start_position);
Move move = parse_move(board, "e2e4");
if (move) {
make_move(board, move, 0);
CBoard_print(board);
} else
printf("Illegal move!\n");
return 0;
}

diff --git a/src/utils.c b/src/utils.c

@@ -36,6 +36,10 @@ const char *square_to_coordinates[]={

"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", " "
};
// clang-format on
//
Square coordinates_to_square(char *cord) {
return (cord[1] - '1') * 8 + (cord[0] - 'a');
}
int bit_count(U64 bitboard) {
int count = 0;