summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-31 07:37:27 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-31 07:37:27 +0300
commit1ee34b9d35a1ad6083967f858988180a425e43ec (patch)
tree06c95bb8b8437707bb0b5533a20b05bbb01ab41d
parent4deffd55ea12943051d1699ed658026945cfd535 (diff)
downloadc-chess-1ee34b9d35a1ad6083967f858988180a425e43ec.tar.gz
feat: discourage bad pawn structure
-rw-r--r--src/main.c40
1 files changed, 38 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 39346c2..c68c62e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -432,6 +432,41 @@ int list_available_moves(Move* moves, int* board, int color) {
return moves_count;
}
+// Count doubled, blocked and isolated pawns
+int count_bad_pawns(int* board, int color) {
+ int bad_pawns = 0;
+ long occupied_files = 0b00000000;
+
+ for (int file = 0; file < 8; file++) {
+ int file_pawns = 0;
+ for (int rank = 7; rank >= 0; rank--) {
+ int piece = board[rank * 16 + file];
+ if ((piece & NO_COLOR) == PAWN && piece % 2 == color) {
+ file_pawns++;
+ occupied_files |= 1 << file;
+
+ // If pawn can't advance, it's blocked
+ int advance_rank = rank + (color == WHITE ? 1 : -1);
+ if (board[advance_rank * 16 + file] != EMPTY) bad_pawns++;
+ }
+ }
+
+ // Doubled or tripled pawns will get punished
+ bad_pawns += file_pawns - 1;
+ }
+
+ // Shift everything to the left to get some space around right-most bit
+ occupied_files = occupied_files << 1;
+
+ // Count isolated pawns based on bits in occupied files
+ for (int bit = 1; bit < 9; bit++) {
+ long bit_mask = (1 << (bit + 1)) | (1 << (bit - 1));
+ if (!(occupied_files & bit_mask)) bad_pawns++;
+ }
+
+ return bad_pawns;
+}
+
/*
* Return a total board evaluation symbolizing advantage
* for WHITE, meaining WHITE tries to maximize this
@@ -448,7 +483,9 @@ int evaluate_position(int* board, int precomputed_mobility, int mobility_color)
int white_mobility_advantage = (mobility - opponent_mobility) * (mobility_color == WHITE ? 1 : -1);
- return white_material_advantage + white_mobility_advantage;
+ int pawn_structure_advantage = count_bad_pawns(board, BLACK) - count_bad_pawns(board, WHITE);
+
+ return white_material_advantage + white_mobility_advantage + 5 * pawn_structure_advantage;
}
int get_piece_raw_value(int piece) {
@@ -562,7 +599,6 @@ int main() {
int color = ply % 2;
printf("##### Ply %i #####\n", ply);
printf("Current evaluation: %i\n", evaluate_position(board, 0, WHITE));
- printf("Zobrist hash: %lo\n", hash);
if (color == PLAYER) {
printf("Enter a move for %s:\n", COLORS[color]);