@@ -9,32 +9,32 @@ int king_table[BOARD_SIZE];
99
1010int 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
4040static 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
121117int 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
0 commit comments