diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 05:51:56 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 05:56:59 +0300 |
commit | 4deffd55ea12943051d1699ed658026945cfd535 (patch) | |
tree | 86b9404a1c37f4531ef9336b9dd6d74fd957831f | |
parent | 03371b847296bb4ba70a4cc048a7abd8f0930c9b (diff) | |
download | c-chess-4deffd55ea12943051d1699ed658026945cfd535.tar.gz |
refactor: mass rename
-rw-r--r-- | src/config.h | 2 | ||||
-rw-r--r-- | src/main.c | 42 |
2 files changed, 22 insertions, 22 deletions
diff --git a/src/config.h b/src/config.h index c179aa9..517181c 100644 --- a/src/config.h +++ b/src/config.h @@ -5,4 +5,4 @@ #define INFINITY 1000000 #define MAX_ZOBRIST_SEEDS 1500 #define TRANSPOSITION_TABLE_SIZE 1000000 -#define PLAYER WHITE +#define PLAYER BLACK @@ -37,7 +37,7 @@ int zobrist_hash(long* seed, int* board, int color_to_move) { return hash; } -long apply_move_zobrist(Move move, int* board, long* zobrist_seed, long hash) { +long hash_after_move(Move move, int* board, long* zobrist_seed, long hash) { int piece = board[move.origin]; int target_piece = board[move.destination]; @@ -261,7 +261,7 @@ char* VALIDATION_ERRORS[] = { "castle is not allowed", }; -int apply_move(Move move, int* board) { +int make_move(Move move, int* board) { int piece = board[move.origin]; int target_piece = board[move.destination]; @@ -307,7 +307,7 @@ int apply_move(Move move, int* board) { return target_piece; } -void reverse_move(Move move, int captured_piece, int* board) { +void unmake_move(Move move, int captured_piece, int* board) { int piece = board[move.destination]; if (captured_piece & PROMOTION_HAPPENED) { @@ -433,19 +433,19 @@ int list_available_moves(Move* moves, int* board, int color) { } /* - * Return a total board score symbolizing advantage + * Return a total board evaluation 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) { +int evaluate_position(int* board, int precomputed_mobility, int mobility_color) { Move dummy[MAX_AVAILABLE_MOVES]; int white_material_advantage = compute_material_advantage(board, WHITE); // If pre-computed mobility has not been passed, compute it - if (!mobility) mobility = list_available_moves(NULL, board, mobility_color); - + int mobility = precomputed_mobility ? precomputed_mobility : list_available_moves(NULL, board, mobility_color); 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; @@ -474,12 +474,12 @@ int compare_moves(const void* a, const void* b) { return ((Move*)b)->value - ((Move*)a)->value; } -int sort_moves(Move* moves, int moves_count, int* board, long hash, int* transposition_table, long* zobrist_seed) { +int order_moves(Move* moves, int moves_count, int* board, long hash, int* transposition_table, long* zobrist_seed) { int cache_hits = 0; for (int i = 0; i < moves_count; i++) { int color = board[moves[i].origin] % 2; // Lookup move in transpoition table - long move_hash = apply_move_zobrist(moves[i], board, zobrist_seed, hash); + long move_hash = hash_after_move(moves[i], board, zobrist_seed, hash); if (transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE]) { moves[i].value = transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE] * (color == WHITE ? 1 : -1); cache_hits++; @@ -501,7 +501,7 @@ int sort_moves(Move* moves, int moves_count, int* board, long hash, int* transpo * Alpha is the best value for maximizer (white) * Beta is the best value for minimizer (black) */ -Move find_best_move(int* board, int color, int depth, int alpha, int beta, int* metrics, long hash, int* transposition_table, long* zobrist_seed) { +Move minimax_search(int* board, int color, int depth, int alpha, int beta, int* metrics, long hash, int* transposition_table, long* zobrist_seed) { int is_maximizer = (color == 0); Move best_move = { -100, -100, 0 }; @@ -511,22 +511,22 @@ Move find_best_move(int* board, int color, int depth, int alpha, int beta, int* if (compute_material_advantage(board, color) > -INFINITY / 2) { Move available_moves[MAX_AVAILABLE_MOVES]; int available_moves_count = list_available_moves(available_moves, board, color); - int cache_hits = sort_moves(available_moves, available_moves_count, board, hash, transposition_table, zobrist_seed); + int cache_hits = order_moves(available_moves, available_moves_count, board, hash, transposition_table, zobrist_seed); for (int i = 0; i < available_moves_count; i++) { *metrics += 1; Move move = available_moves[i]; - long move_hash = apply_move_zobrist(move, board, zobrist_seed, hash); - int captured_piece = apply_move(move, board); + long move_hash = hash_after_move(move, board, zobrist_seed, hash); + int captured_piece = make_move(move, board); move.value = depth < MAX_DEPTH - ? find_best_move(board, 1 - color, depth + 1, alpha, beta, metrics, hash, transposition_table, zobrist_seed).value - : compute_score(board, available_moves_count, color); + ? minimax_search(board, 1 - color, depth + 1, alpha, beta, metrics, hash, transposition_table, zobrist_seed).value + : evaluate_position(board, available_moves_count, color); transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE] = move.value; - reverse_move(move, captured_piece, board); + unmake_move(move, captured_piece, board); if (is_maximizer) { if (move.value > best_move.value) best_move = move; @@ -561,7 +561,7 @@ int main() { while (1) { int color = ply % 2; printf("##### Ply %i #####\n", ply); - printf("Current score: %i\n", compute_score(board, 0, WHITE)); + printf("Current evaluation: %i\n", evaluate_position(board, 0, WHITE)); printf("Zobrist hash: %lo\n", hash); if (color == PLAYER) { @@ -572,14 +572,14 @@ int main() { clock_t start, end; int metrics = 0; start = clock(); - move = find_best_move(board, color, 0, -INFINITY, +INFINITY, &metrics, hash, transposition_table, zobrist_seed); + move = minimax_search(board, color, 0, -INFINITY, +INFINITY, &metrics, hash, transposition_table, zobrist_seed); end = clock(); printf("[%i positions analyzed in %f seconds]\n", metrics, (double)(end - start) / CLOCKS_PER_SEC); char move_in_notation[] = "xy XY"; index_to_notation(move.origin, move_in_notation); index_to_notation(move.destination, move_in_notation + 3); - printf("%s: %s with score at worst %i on Ply %i\n", COLORS[color], move_in_notation, move.value, ply + MAX_DEPTH); + printf("%s: %s with evaluation %c=%i on Ply %i\n", COLORS[color], move_in_notation, color == WHITE ? '>' : '<', move.value, ply + MAX_DEPTH); } int error = validate_move(move, color, board); @@ -589,8 +589,8 @@ int main() { printf("Checkmate, %s is victorious!\n", COLORS[1 - color]); break; } else { - hash = apply_move_zobrist(move, board, zobrist_seed, hash); - apply_move(move, board); + hash = hash_after_move(move, board, zobrist_seed, hash); + make_move(move, board); print_board(board); |