Skip to content

Commit af46fc7

Browse files
committed
correction beug evaluation position
1 parent 9ec1231 commit af46fc7

7 files changed

Lines changed: 103 additions & 48 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ BOT_OBJ_FILES := $(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(BOT_SRC_FILES:.c=.o)
2929
TEST_SRC_FILES := $(shell find $(TEST_DIR) -name "*.c")
3030
SRC_CHESSBOARD := $(shell find $(SRC_DIR)/chessboard -name "*.c")
3131
SRC_API := $(shell find $(SRC_DIR)/api -name "*.c")
32+
SRC_AI := $(shell find $(SRC_DIR)/ai -name "*.c")
33+
3234
TEST_OBJ_FILES := $(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(SRC_CHESSBOARD:.c=.o)) \
33-
$(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(SRC_API:.c=.o))
35+
$(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(SRC_API:.c=.o)) \
36+
$(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(SRC_AI:.c=.o))
3437

3538
# === RÈGLES ===
3639
all: $(EXEC)

bots/bot_v0

32 Bytes
Binary file not shown.

src/ai/ai.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,19 @@ static int quiescence_search(Chessboard *board, int alpha, int beta, SearchInfo
7171
int alpha_beta(Chessboard *board, int depth, int alpha, int beta, SearchInfo *info)
7272
{
7373
if (depth == 0)
74-
return quiescence_search(board, alpha, beta, info);
75-
74+
{
75+
//return quiescence_search(board, alpha, beta, info);
76+
info->nb_positions_evaluated += 1;
77+
return evaluate_position(board);
78+
}
79+
7680
Move moves[250];
7781
int count = get_all_legal_moves(board, moves);
7882

7983
if (count == 0)
8084
{
8185
if (is_check(board))
82-
return board->white_to_play ? -MAT + depth : MAT - depth;
86+
return -MAT - depth;
8387
return 0;
8488
}
8589

@@ -154,10 +158,10 @@ SearchInfo get_best_move(Chessboard board)
154158
{
155159
SearchInfo info = {0};
156160
info.nb_positions_evaluated = 0;
157-
info.depth = 3;
161+
info.depth = 4;
158162
info.log[0] = '\0';
159163

160164
info.move = search_best_move(&board, info.depth, &info);
161-
snprintf(info.log, SEARCH_LOG_SIZE, "depth=%d evaluations=%d", info.depth, info.nb_positions_evaluated);
165+
snprintf(info.log, SEARCH_LOG_SIZE, "depth=%d nb_positions=%d evaluation=%d ", info.depth, info.nb_positions_evaluated, info.move.score);
162166
return info;
163167
}

src/ai/evaluation.c

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@ int king_table[BOARD_SIZE];
99

1010
int ally_color;
1111

12-
static int count_material(Chessboard *board, int *total_value)
12+
static int mirror_square(int index)
1313
{
14-
int score = 0;
15-
*total_value = 0;
16-
17-
// Matériel blanc
18-
19-
score += count_bits(board->knights & board->occupied_white) * KNIGHT_VALUE;
20-
score += count_bits(board->bishops & board->occupied_white) * BISHOP_VALUE;
21-
score += count_bits(board->rooks & board->occupied_white) * ROOK_VALUE;
22-
score += count_bits(board->queens & board->occupied_white) * QUEEN_VALUE;
23-
24-
*total_value += score;
25-
26-
// Matériel noir (on soustrait)
27-
28-
score -= count_bits(board->knights & board->occupied_black) * KNIGHT_VALUE;
29-
score -= count_bits(board->bishops & board->occupied_black) * BISHOP_VALUE;
30-
score -= count_bits(board->rooks & board->occupied_black) * ROOK_VALUE;
31-
score -= count_bits(board->queens & board->occupied_black) * QUEEN_VALUE;
14+
int rank = index / 8;
15+
int file = index % 8;
3216

33-
*total_value += *total_value - score;
17+
return (7 - rank) * 8 + file;
18+
}
3419

35-
score += count_bits(board->pawns & board->occupied_white) * PAWN_VALUE;
36-
score -= count_bits(board->pawns & board->occupied_black) * PAWN_VALUE;
37-
return score;
20+
static int count_material(Chessboard *board)
21+
{
22+
int white_material = 0;
23+
int black_material = 0;
24+
25+
white_material += count_bits(board->pawns & board->occupied_white) * PAWN_VALUE;
26+
white_material += count_bits(board->knights & board->occupied_white) * KNIGHT_VALUE;
27+
white_material += count_bits(board->bishops & board->occupied_white) * BISHOP_VALUE;
28+
white_material += count_bits(board->rooks & board->occupied_white) * ROOK_VALUE;
29+
white_material += count_bits(board->queens & board->occupied_white) * QUEEN_VALUE;
30+
31+
black_material += count_bits(board->pawns & board->occupied_black) * PAWN_VALUE;
32+
black_material += count_bits(board->knights & board->occupied_black) * KNIGHT_VALUE;
33+
black_material += count_bits(board->bishops & board->occupied_black) * BISHOP_VALUE;
34+
black_material += count_bits(board->rooks & board->occupied_black) * ROOK_VALUE;
35+
black_material += count_bits(board->queens & board->occupied_black) * QUEEN_VALUE;
36+
37+
return white_material - black_material;
3838
}
3939

4040
static int evaluate_position_ally_pieces(Chessboard *board)
@@ -52,11 +52,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
5252
if (board->occupied_white & (1ULL << index))
5353
score += pawn_table[index];
5454
else
55-
{
56-
int rank = index / 8;
57-
int file = index % 8;
58-
score -= pawn_table[(7 - rank) * 8 + file];
59-
}
55+
score -= pawn_table[mirror_square(index)];
6056
bit &= bit - 1;
6157
}
6258

@@ -67,7 +63,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
6763
if (board->occupied_white & (1ULL << index))
6864
score += knight_table[index];
6965
else
70-
score -= knight_table[index];
66+
score -= knight_table[mirror_square(index)];
7167
bit &= bit - 1;
7268
}
7369

@@ -78,7 +74,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
7874
if (board->occupied_white & (1ULL << index))
7975
score += bishop_table[index];
8076
else
81-
score -= bishop_table[index];
77+
score -= bishop_table[mirror_square(index)];
8278
bit &= bit - 1;
8379
}
8480

@@ -89,7 +85,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
8985
if (board->occupied_white & (1ULL << index))
9086
score += rook_table[index];
9187
else
92-
score -= rook_table[index];
88+
score -= rook_table[mirror_square(index)];
9389
bit &= bit - 1;
9490
}
9591

@@ -100,7 +96,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
10096
if (board->occupied_white & (1ULL << index))
10197
score += queen_table[index];
10298
else
103-
score -= queen_table[index];
99+
score -= queen_table[mirror_square(index)];
104100
bit &= bit - 1;
105101
}
106102

@@ -111,7 +107,7 @@ static int evaluate_position_ally_pieces(Chessboard *board)
111107
if (board->occupied_white & (1ULL << index))
112108
score += king_table[index];
113109
else
114-
score -= king_table[index];
110+
score -= king_table[mirror_square(index)];
115111
bit &= bit - 1;
116112
}
117113

@@ -120,13 +116,12 @@ static int evaluate_position_ally_pieces(Chessboard *board)
120116

121117
int evaluate_position(Chessboard *board)
122118
{
123-
int total_value = 0;
124-
int score = count_material(board, &total_value);
119+
int score = count_material(board);
120+
score += evaluate_position_ally_pieces(board);
121+
122+
if (!board->white_to_play)
123+
score = -score;
125124

126-
if (board->white_to_play)
127-
score += evaluate_position_ally_pieces(board);
128-
else
129-
score -= evaluate_position_ally_pieces(board);
130125
return score;
131126
}
132127

src/chessboard/move/move.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void print_move(Move *move)
163163
{
164164
char f[3];
165165
char t[3];
166-
char p = ' ';
166+
char p = '\0';
167167
index_to_square(move->from, f);
168168
index_to_square(move->to, t);
169169

@@ -176,14 +176,17 @@ void print_move(Move *move)
176176
if (move->promotion_flag == PROMOTION_R)
177177
p = 'r';
178178

179-
printf("%s%s%c", f, t, p);
179+
if (p != '\0')
180+
printf("%s%s%c", f, t, p);
181+
else
182+
printf("%s%s", f, t);
180183
}
181184

182185
void move_to_string(Move *move, char *move_string)
183186
{
184187
char f[3];
185188
char t[3];
186-
char p = ' ';
189+
char p = '\0';
187190
index_to_square(move->from, f);
188191
index_to_square(move->to, t);
189192

tests/test_ai.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <criterion/criterion.h>
3+
#include <string.h>
4+
5+
#include "ai/ai.h"
6+
#include "chessboard/chessboard.h"
7+
#include "chessboard/move/move.h"
8+
9+
TestSuite(ai_search);
10+
11+
Test(ai_search, black_turn_selects_best_tactical_move)
12+
{
13+
Chessboard board;
14+
15+
initialize_tables();
16+
init_chessboard_from_fen(&board, "4k3/8/8/8/3q4/8/4r3/4K3 b - - 0 1");
17+
18+
SearchInfo info = get_best_move(board);
19+
char move[10] = {0};
20+
move_to_string(&info.move.move, move);
21+
22+
fprintf(stderr, "chosen move=%s score=%d\n", move, info.move.score);
23+
fprintf(stderr, "len=%zu raw=%c%c%c%c next=%d next2=%d\n", strlen(move), move[0], move[1], move[2], move[3], (unsigned char)move[4], (unsigned char)move[5]);
24+
cr_assert_str_eq(move, "e2f2", "Black should choose the best evaluated tactical move after perspective correction");
25+
cr_assert(info.move.score >= 0, "The chosen move should not be penalized for the side to move");
26+
}

tests/test_evaluation.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <criterion/criterion.h>
2+
3+
#include "ai/evaluation.h"
4+
#include "chessboard/chessboard.h"
5+
6+
void setup_evaluation(void)
7+
{
8+
cr_assert_eq(initialize_tables(), 0, "Tables should initialize successfully");
9+
}
10+
11+
TestSuite(evaluation, .init = setup_evaluation);
12+
13+
Test(evaluation, side_to_move_flips_score)
14+
{
15+
Chessboard board;
16+
17+
init_chessboard_from_fen(&board, "8/8/8/8/8/8/1N6/8 w - - 0 1");
18+
int score_white = evaluate_position(&board);
19+
20+
board.white_to_play = false;
21+
int score_black = evaluate_position(&board);
22+
23+
cr_assert_eq(score_white + score_black, 0, "Changing side to move should invert the evaluation");
24+
}

0 commit comments

Comments
 (0)