diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 00:37:44 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 00:37:44 +0300 |
commit | 707a6261673b09b13709d7c2cf266471b881674c (patch) | |
tree | f9c23a1925e6ae3671d2d24959eaec03a89f1f96 | |
parent | 77c25a994fea0b01a51668feca4e04b1762d98b4 (diff) | |
download | c-chess-707a6261673b09b13709d7c2cf266471b881674c.tar.gz |
fix: account for mobility difference
-rw-r--r-- | src/main.c | 74 |
1 files changed, 18 insertions, 56 deletions
@@ -438,60 +438,6 @@ int compute_coverage(int* board, int color) { return counter; } -/* - * Compute player pieces score depending on how - * well they are positioned on the board - * - * Returns number in range(0, 112) - */ -int compute_positioning_score(int* board, int color) { - int total_score = 0; - double center = 3.5; - - for (double rank = 7; rank >= 0; rank--) { - for (double file = 0; file < 8; file++) { - int index = rank * 16 + file; - int piece = board[index]; - if (piece % 2 == color) { - int score = 7 - (fabs(rank - center) + fabs(file - center)); - - int multiplier = 1; - switch (piece & NO_COLOR) { - case KNIGHT: - multiplier = 4; - break; - case BISHOP: - multiplier = 3; - break; - case PAWN: - multiplier = 2; - break; - case KING: - multiplier = -5; // Discourage king in center (encourage castle) - // TODO: encourage king in center after opponent has low material <= 6 - break; - default: - multiplier = 1; - break; - } - total_score += score * multiplier; - } - } - } - - return total_score; -} - -/* - * Return a total board score symbolizing advantage - * for WHITE, meaining WHITE tries to maximize this - * value and BLACK tries to minimize it. - */ -int compute_score(int* board, int mobility) { - int material_advantage = compute_material_advantage(board, WHITE); - return material_advantage + mobility; -} - int list_available_moves(Move* moves, int* board, int color) { int moves_count = 0; @@ -505,7 +451,7 @@ int list_available_moves(Move* moves, int* board, int color) { int index_destination = rank_destination * 16 + file_destination; Move move = { index, index_destination, 0 }; if (validate_move(move, color, board) >= 0) { - moves[moves_count] = move; + if (moves) moves[moves_count] = move; moves_count++; } } @@ -517,6 +463,22 @@ int list_available_moves(Move* moves, int* board, int color) { return moves_count; } +/* + * Return a total board score symbolizing advantage + * for WHITE, meaining WHITE tries to maximize this + * value and BLACK tries to minimize it. + */ +int compute_score(int* board, int mobility, int mobility_color) { + Move dummy[MAX_AVAILABLE_MOVES]; + + int white_material_advantage = compute_material_advantage(board, WHITE); + + int opponent_mobility = list_available_moves(NULL, board, mobility_color ^ 1); + int white_mobility_advantage = (mobility - opponent_mobility) * (mobility_color == WHITE ? 1 : -1); + + return white_material_advantage + white_mobility_advantage; +} + int get_piece_raw_value(int piece) { switch (piece & NO_COLOR) { case KNIGHT: @@ -576,7 +538,7 @@ Move find_best_move(int* board, int color, int depth, int alpha, int beta, int* move.value = depth < MAX_DEPTH ? find_best_move(board, 1 - color, depth + 1, alpha, beta, metrics).value - : compute_score(board, color == WHITE ? availabel_moves_count : -availabel_moves_count); + : compute_score(board, availabel_moves_count, color); reverse_move(move, captured_piece, board); |