stellarUCI Chess engine written in C++20 |
git clone git://git.dimitrijedobrota.com/stellar.git |
Log | Files | Refs | README | LICENSE | |
commit | 2ec2544c4e36610a55075faf8d495c6dab7cc752 |
parent | 9ce8b54dde4118111d96fb90d307b9fa691c92f5 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Mon, 31 Jul 2023 16:13:36 +0200 |
Slight rename
Diffstat:M | CMakeLists.txt | | | +- |
M | src/board/board.c | | | +++++++++++++++++++++++++++++++++++++++++----------------------------------------- |
M | src/engine/engine.c | | | ++-- |
M | src/include/board.h | | | ++++++----- |
M | src/moves/moves.c | | | +++-- |
5 files changed, 73 insertions(+), 70 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(
Stellar
VERSION 0.0.6
VERSION 0.0.7
DESCRIPTION "Chess engine written in C"
HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git
LANGUAGES C
diff --git a/src/board/board.c b/src/board/board.c
@@ -8,8 +8,8 @@
#include "board.h"
struct Board {
U64 colorBB[2];
U64 pieceBB[6];
U64 color[2];
U64 piece[6];
eColor side;
Square enpassant;
eCastle castle;
@@ -28,49 +28,50 @@ void board_copy(Board self, Board dest) { *dest = *self; }
Square board_enpassant(Board self) { return self->enpassant; }
eCastle board_castle(Board self) { return self->castle; }
eColor board_side(Board self) { return self->side; }
U64 board_colorBB(Board self, eColor color) { return self->colorBB[color]; }
U64 board_pieceBB(Board self, ePiece piece) { return self->pieceBB[piece]; }
U64 board_color(Board self, eColor color) { return self->color[color]; }
U64 board_piece(Board self, ePiece piece) { return self->piece[piece]; }
U64 board_occupancy(Board self) {
return self->colorBB[WHITE] | self->colorBB[BLACK];
return self->color[WHITE] | self->color[BLACK];
}
U64 board_pieceBB_get(Board self, ePiece piece, Square target) {
return bit_get(self->pieceBB[piece], target);
return bit_get(self->piece[piece], target);
}
U64 board_pieceSet(Board self, Piece piece) {
return self->pieceBB[piece_piece(piece)] &
self->colorBB[piece_color(piece)];
return self->piece[piece_piece(piece)] & self->color[piece_color(piece)];
}
void board_enpassant_set(Board self, Square target) {
self->enpassant = target;
}
void board_colorBB_pop(Board self, eColor color, Square target) {
bit_pop(self->colorBB[color], target);
void board_color_pop(Board self, eColor color, Square target) {
bit_pop(self->color[color], target);
}
void board_colorBB_set(Board self, eColor color, Square target) {
bit_set(self->colorBB[color], target);
void board_color_set(Board self, eColor color, Square target) {
bit_set(self->color[color], target);
}
U64 board_colorBB_get(Board self, eColor color, Square target) {
return bit_get(self->colorBB[color], target);
U64 board_color_get(Board self, eColor color, Square target) {
return bit_get(self->color[color], target);
}
int board_piece_get(Board self, Square square) {
for (int i = 0; i < 6; i++)
if (bit_get(self->pieceBB[i], square)) return i;
if (bit_get(self->piece[i], square)) return i;
return -1;
}
void board_piece_pop(Board self, Piece piece, Square square) {
bit_pop(self->pieceBB[piece_piece(piece)], square);
bit_pop(self->colorBB[piece_color(piece)], square);
bit_pop(self->piece[piece_piece(piece)], square);
bit_pop(self->color[piece_color(piece)], square);
}
void board_piece_set(Board self, Piece piece, Square square) {
bit_set(self->pieceBB[piece_piece(piece)], square);
bit_set(self->colorBB[piece_color(piece)], square);
bit_set(self->piece[piece_piece(piece)], square);
bit_set(self->color[piece_color(piece)], square);
}
void board_piece_move(Board self, Piece Piece, Square source, Square target) {
@@ -97,7 +98,7 @@ void board_castle_and(Board self, int exp) { self->castle &= exp; }
void board_side_switch(Board self) { self->side = !self->side; }
int board_isCheck(Board self) {
U64 king = self->pieceBB[KING] & self->colorBB[self->side];
U64 king = self->piece[KING] & self->color[self->side];
return board_square_isAttack(self, bit_lsb_index(king), !self->side);
}
int board_square_isOccupied(Board self, Square square) {
@@ -105,11 +106,11 @@ int board_square_isOccupied(Board self, Square square) {
}
int board_square_isAttack(Board self, Square square, eColor side) {
U64 occupancy = self->colorBB[WHITE] | self->colorBB[BLACK];
U64 occupancy = self->color[WHITE] | self->color[BLACK];
for (int i = 0; i < 6; i++) {
if (piece_attacks(piece_get(i, !side))(square, occupancy) &
self->pieceBB[i] & self->colorBB[side])
self->piece[i] & self->color[side])
return 1;
}
@@ -122,42 +123,6 @@ Piece board_square_piece(Board self, Square square, eColor color) {
return NULL;
}
void board_print(Board self) {
for (int rank = 0; rank < 8; rank++) {
for (int file = 0; file < 8; file++) {
Square square = (7 - rank) * 8 + file;
Piece piece = NULL;
int color = -1;
if (bit_get(self->colorBB[WHITE], square))
color = WHITE;
else if (bit_get(self->colorBB[BLACK], square))
color = BLACK;
if (color != -1) {
for (int piece_index = 0; piece_index < 6; piece_index++) {
if (bit_get(self->pieceBB[piece_index], square)) {
piece = piece_get(piece_index, color);
break;
}
}
}
if (!file) printf(" %d ", 8 - rank);
printf("%s", (piece) ? piece_unicode(piece) : ". ");
}
printf("\n");
}
printf(" A B C D E F G H\n");
printf(" Side: %s\n", (self->side == WHITE) ? "white" : "black");
printf("Enpassant: %s\n", square_to_coordinates[self->enpassant]);
printf(" Castling: %c%c%c%c\n", (self->castle & WK) ? 'K' : '-',
(self->castle & WQ) ? 'Q' : '-', (self->castle & BK) ? 'k' : '-',
(self->castle & BQ) ? 'q' : '-');
printf("\n");
}
Board board_from_FEN(Board board, const char *fen) {
if (!board) NEW(board);
@@ -172,8 +137,8 @@ Board board_from_FEN(Board board, const char *fen) {
Square square = rank * 8 + file;
if (isalpha(*fen)) {
if (!(piece = piece_from_code(*fen))) assert(0);
bit_set(board->colorBB[piece_color(piece)], square);
bit_set(board->pieceBB[piece_piece(piece)], square);
bit_set(board->color[piece_color(piece)], square);
bit_set(board->piece[piece_piece(piece)], square);
file++;
} else if (isdigit(*fen)) {
file += *fen - '0';
@@ -220,3 +185,39 @@ Board board_from_FEN(Board board, const char *fen) {
return board;
}
void board_print(Board self) {
for (int rank = 0; rank < 8; rank++) {
for (int file = 0; file < 8; file++) {
Square square = (7 - rank) * 8 + file;
Piece piece = NULL;
int color = -1;
if (bit_get(self->color[WHITE], square))
color = WHITE;
else if (bit_get(self->color[BLACK], square))
color = BLACK;
if (color != -1) {
for (int piece_index = 0; piece_index < 6; piece_index++) {
if (bit_get(self->piece[piece_index], square)) {
piece = piece_get(piece_index, color);
break;
}
}
}
if (!file) printf(" %d ", 8 - rank);
printf("%s", (piece) ? piece_unicode(piece) : ". ");
}
printf("\n");
}
printf(" A B C D E F G H\n");
printf(" Side: %s\n", (self->side == WHITE) ? "white" : "black");
printf("Enpassant: %s\n", square_to_coordinates[self->enpassant]);
printf(" Castling: %c%c%c%c\n", (self->castle & WK) ? 'K' : '-',
(self->castle & WQ) ? 'Q' : '-', (self->castle & BK) ? 'k' : '-',
(self->castle & BQ) ? 'q' : '-');
printf("\n");
}
diff --git a/src/engine/engine.c b/src/engine/engine.c
@@ -73,11 +73,11 @@ void move_list_sort(Stats_T stats, MoveList list) {
int evaluate(Board board) {
Square square;
eColor side = board_side(board);
U64 occupancy = board_colorBB(board, side);
U64 occupancy = board_color(board, side);
int score = 0;
for (int i = 0; i < 6; i++) {
U64 bitboard = board_pieceBB(board, i);
U64 bitboard = board_piece(board, i);
bitboard_for_each_bit(square, bitboard) {
if (bit_get(occupancy, square)) {
score += Score_value(i);
diff --git a/src/include/board.h b/src/include/board.h
@@ -17,9 +17,9 @@ Board board_new(void);
void board_free(Board *p);
void board_copy(Board self, Board dest);
U64 board_colorBB(Board self, eColor color);
U64 board_color(Board self, eColor color);
U64 board_occupancy(Board self);
U64 board_pieceBB(Board self, ePiece piece);
U64 board_piece(Board self, ePiece piece);
eCastle board_castle(Board self);
eColor board_side(Board self);
@@ -30,14 +30,15 @@ U64 board_pieceSet(Board self, Piece piece);
U64 board_piece_attacks(Board self, Piece piece, Square src);
void board_piece_capture(Board self, Piece piece, Piece taken, Square source,
Square target);
void board_piece_move(Board self, Piece Piece, Square square, Square target);
void board_piece_pop(Board self, Piece Piece, Square square);
void board_piece_set(Board self, Piece Piece, Square square);
int board_piece_get(Board self, Square square);
U64 board_colorBB_get(Board self, eColor color, Square target);
void board_colorBB_pop(Board self, eColor color, Square target);
void board_colorBB_set(Board self, eColor color, Square target);
U64 board_color_get(Board self, eColor color, Square target);
void board_color_pop(Board self, eColor color, Square target);
void board_color_set(Board self, eColor color, Square target);
void board_castle_and(Board self, int exp);
void board_castle_pop(Board self, eCastle castle);
diff --git a/src/moves/moves.c b/src/moves/moves.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <cul/mem.h>
#include <cul/assert.h>
#include "moves.h"
@@ -105,7 +106,7 @@ MoveList move_list_generate(MoveList moves, Board board) {
}
{ // capture
U64 attack = board_piece_attacks(board, Piece, src) &
board_colorBB(board, !color);
board_color(board, !color);
bitboard_for_each_bit(tgt, attack) {
if (pawn_canPromote(color, src)) {
pawn_promote(src, tgt, Piece,
@@ -139,7 +140,7 @@ MoveList move_list_generate(MoveList moves, Board board) {
U64 bitboard = board_pieceSet(board, Piece);
bitboard_for_each_bit(src, bitboard) {
U64 attack = board_piece_attacks(board, Piece, src) &
~board_colorBB(board, color);
~board_color(board, color);
bitboard_for_each_bit(tgt, attack) {
/* int take = bit_get(board_colorBB(board, !color), tgt); */
move_list_add(