/* UCI */
typedef struct Position_T *Position_T;
struct Position_T {
struct Score_T {
int value;
int score[64];
};
// clang-format off
struct Score_T Scores[] = {
[PAWN] = {
.value = 100,
.score = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -10, -10, 0, 0, 0,
0, 0, 0, 5, 5, 0, 0, 0,
5, 5, 10, 20, 20, 5, 5, 5,
10, 10, 10, 20, 20, 10, 10, 10,
20, 20, 20, 30, 30, 30, 20, 20,
30, 30, 30, 40, 40, 30, 30, 30,
90, 90, 90, 90, 90, 90, 90, 90,
}},
[KNIGHT] = {
.value = 300,
.score = {
-5, -10 , 0, 0, 0, 0, -10, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 5, 20, 10, 10, 20, 5, -5,
-5, 10, 20, 30, 30, 20, 10, -5,
-5, 10, 20, 30, 30, 20, 10, -5,
-5, 5, 20, 20, 20, 20, 5, -5,
-5, 0, 0, 10, 10, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
}},
[BISHOP] = {
.value = 350,
.score = {
0, 0, -10, 0, 0, -10, 0, 0,
0, 30, 0, 0, 0, 0, 30, 0,
0, 10, 0, 0, 0, 0, 10, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 0, 10, 10, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}},
[ROOK] = {
.value = 500,
.score = {
0, 0, 0, 20, 20, 0, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
0, 0, 10, 20, 20, 10, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50,
}},
[QUEEN] = {
.value = 1000,
.score = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}},
[KING] = {
.value = 10000,
.score = {
0, 0, 5, 0, -15, 0, 10, 0,
0, 5, 5, -5, -5, 0, 5, 0,
0, 0, 5, 10, 10, 5, 0, 0,
0, 5, 10, 20, 20, 10, 5, 0,
0, 5, 10, 20, 20, 10, 5, 0,
0, 5, 5, 10, 10, 5, 5, 0,
0, 0, 5, 5, 5, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}},
};
const int mirror_score[128] =
{
a8, b8, c8, d8, e8, f8, g8, h8,
a7, b7, c7, d7, e7, f7, g7, h7,
a6, b6, c6, d6, e6, f6, g6, h6,
a5, b5, c5, d5, e5, f5, g5, h5,
a4, b4, c4, d4, e4, f4, g4, h4,
a3, b3, c3, d3, e3, f3, g3, h3,
a2, b2, c2, d2, e2, f2, g2, h2,
a1, b1, c1, d1, e1, f1, g1, h1, no_sq,
};
// clang-format on
int Score_value(ePiece piece) { return Scores[piece].value; }
int Score_score(ePiece piece, eColor color, Square square) {
if (color == BLACK)
square = mirror_score[square];
return Scores[piece].score[square];
}
int evaluate(CBoard_T board) {
Square square;
eColor side = CBoard_side(board);
U64 occupancy = CBoard_colorBB(board, side);
int score = 0;
for (int i = 0; i < 6; i++) {
U64 bitboard = CBoard_pieceBB(board, i);
bitboard_for_each_bit(square, bitboard) {
if (bit_get(occupancy, square)) {
score += Score_value(i);
score += Score_score(i, side, square);
} else {
score -= Score_value(i);
score -= Score_score(i, !side, square);
}
}
}
return side == WHITE ? score : -score;
}
void search_position(CBoard_T cboard, int depth) {
(void)cboard;
(void)depth;
printf("bestmove d7d5\n");
}
void print_info(void) {
printf("id name chessTrainer\n");
printf("id author Dimitrije Dobrota\n");
printf("uciok\n");
}
typedef struct Instruction_T *Instruction_T;
struct Instruction_T {
char *command;
char *token;
char *crnt;
};
char *Position_token_next(Position_T self);
char *Instruction_token_next(Instruction_T self);
Position_T Position_new(char *command) {
Position_T p;
Instruction_T Instruction_new(char *command) {
Instruction_T p;
NEW0(p);
p->command = ALLOC(strlen(command) + 1);
p->token = ALLOC(strlen(command) + 1);
strcpy(p->command, command);
p->crnt = command;
Position_token_next(p);
Instruction_token_next(p);
return p;
}
void Position_free(Position_T *p) {
void Instruction_free(Instruction_T *p) {
FREE((*p)->command);
FREE((*p)->token);
FREE(*p);
}
char *Position_token(Position_T self) { return self->token; }
char *Position_token_n(Position_T self, int n) {
char *Instruction_token(Instruction_T self) { return self->token; }
char *Instruction_token_n(Instruction_T self, int n) {
while (isspace(*self->crnt) && *self->crnt != '\0')
self->crnt++;