stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

commit 60dd166598e91141690a89aebb8af8cdbb6ae8ba
parent 64d04dd24113fa32bcca79e79ed14c55b148c640
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 31 Jul 2023 22:07:56 +0200

Perft count only leave move types, fix promotion

Diffstat:
MCMakeLists.txt | 4++--
Msrc/board/board.c | 3++-
Msrc/moves/moves.c | 5++---
Msrc/perft/CMakeLists.txt | 6+-----
Msrc/perft/perft.c | 49++++++++++++++++++++++++-------------------------
5 files changed, 31 insertions(+), 36 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( Stellar - VERSION 0.0.8 + VERSION 0.0.9 DESCRIPTION "Chess engine written in C" HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git LANGUAGES C @@ -11,6 +11,6 @@ project( set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_C_EXTENSIONS ON) add_subdirectory(src) diff --git a/src/board/board.c b/src/board/board.c @@ -145,8 +145,9 @@ Board board_from_FEN(Board board, const char *fen) { } else if (*fen == '/') { file = 0; rank--; - } else + } else { assert(0); + } } fen++; diff --git a/src/moves/moves.c b/src/moves/moves.c @@ -1,8 +1,8 @@ #include <stdio.h> #include <stdlib.h> -#include <cul/mem.h> #include <cul/assert.h> +#include <cul/mem.h> #include "moves.h" @@ -88,8 +88,7 @@ MoveList move_list_generate(MoveList moves, Board board) { { // quiet int add = (color == WHITE) ? +8 : -8; tgt = src + add; - if (tgt > a1 && tgt < h8 && - !board_square_isOccupied(board, tgt)) { + if (!board_square_isOccupied(board, tgt)) { if (pawn_canPromote(color, src)) { pawn_promote(src, tgt, Piece, 0); } else { diff --git a/src/perft/CMakeLists.txt b/src/perft/CMakeLists.txt @@ -1,14 +1,10 @@ project( perft - VERSION 1.0.0 + VERSION 1.1.0 DESCRIPTION "Performance Test" LANGUAGES C ) -set(CMAKE_C_STANDARD 99) -set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_C_EXTENSIONS ON) - add_executable(perft perft.c) option(WITH_FULL_COUNT "Make count on types of moves" OFF) diff --git a/src/perft/perft.c b/src/perft/perft.c @@ -1,5 +1,4 @@ #include <pthread.h> -#include <semaphore.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -33,6 +32,8 @@ void perft_result_print(PerftResult res) { // printf("Discovered Checks: %llu\n", res.checkDiscovered); // printf(" Dobule Checks: %llu\n", res.checkDouble); // printf(" Checkmates: %llu\n", res.checkmate); +#else + printf("Other stats are disabled in this build...\n"); #endif } @@ -52,11 +53,6 @@ void perft_result_add(PerftResult *base, PerftResult *add) { void perft_driver(Board board, struct MoveList *moveList, int depth, PerftResult *result) { - if (depth == 0) { - result->node++; - return; - } - MoveList list = move_list_generate(&moveList[depth], board); Board copy = board_new(); @@ -65,15 +61,18 @@ void perft_driver(Board board, struct MoveList *moveList, int depth, board_copy(board, copy); if (!move_make(move, copy, 0)) continue; + if (depth != 1) { + perft_driver(copy, moveList, depth - 1, result); + } else { + result->node++; #ifdef USE_FULL_COUNT - if (board_isCheck(copy)) result->check++; - if (move_capture(move)) result->capture++; - if (move_enpassant(move)) result->enpassant++; - if (move_castle(move)) result->castle++; - if (move_promote(move)) result->promote++; + if (board_isCheck(copy)) result->check++; + if (move_capture(move)) result->capture++; + if (move_enpassant(move)) result->enpassant++; + if (move_castle(move)) result->castle++; + if (move_promote(move)) result->promote++; #endif - - perft_driver(copy, moveList, depth - 1, result); + } } move_list_reset(list); @@ -87,7 +86,6 @@ struct perf_shared { int depth; pthread_mutex_t mutex; unsigned int index; - sem_t finish; PerftResult result; }; @@ -114,18 +112,20 @@ void *perft_thread(void *arg) { board_copy(board, copy); if (!move_make(move, copy, 0)) continue; + if (shared->depth != 1) { + perft_driver(copy, moveList, shared->depth - 1, &result); + } else { + result.node++; #ifdef USE_FULL_COUNT - if (board_isCheck(copy)) result.check++; - if (move_capture(move)) result.capture++; - if (move_enpassant(move)) result.enpassant++; - if (move_castle(move)) result.castle++; - if (move_promote(move)) result.promote++; + if (board_isCheck(copy)) result.check++; + if (move_capture(move)) result.capture++; + if (move_enpassant(move)) result.enpassant++; + if (move_castle(move)) result.castle++; + if (move_promote(move)) result.promote++; #endif - - perft_driver(copy, moveList, shared->depth, &result); + } } board_free(&board); - sem_post(&shared->finish); return NULL; } @@ -136,17 +136,16 @@ PerftResult perft_test(const char *fen, int depth, int thread_num) { pthread_t threads[thread_num]; perf_shared shared = (perf_shared){ .list = move_list_generate(NULL, board_from_FEN(NULL, fen)), - .depth = depth - 1, + .depth = depth, .fen = fen, }; - sem_init(&shared.finish, 0, 0); pthread_mutex_init(&shared.mutex, NULL); for (int i = 0; i < thread_num; i++) pthread_create(threads + i, NULL, perft_thread, (void *)(&shared)); for (int i = 0; i < thread_num; i++) - sem_wait(&shared.finish); + pthread_join(threads[i], NULL); move_list_free(&shared.list); return shared.result;