From 81c52f05e3577dfb0c47eba758ec4fab5220e675 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 14 Oct 2024 22:58:57 +0200 Subject: [PATCH] added stop UCI command + little adjustments --- .gitignore | 2 +- Makefile | 9 ++++++--- source/search.cpp | 23 ++++++++++++++++++----- source/search.h | 1 + source/uci.cpp | 15 +++++++++++++-- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index f60144a..cb0feea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ obj/ # Ignore executable files *.exe -Eleanor +Engine-Eleanor diff --git a/Makefile b/Makefile index 7b04fb9..229f359 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ SRC_DIR := source OBJ_DIR := obj +CXX := g++ +EXE := Engine-Eleanor + # Determine platform ifeq ($(OS),Windows_NT) EXE_EXT := .exe @@ -16,12 +19,12 @@ SRCS := $(wildcard $(SRC_DIR)/*.cpp) OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o))) # Target executable -Engine-Eleanor$(EXE_EXT): $(OBJS) - g++ -o $@ $^ -O3 +$(EXE)$(EXE_EXT): $(OBJS) + $(CXX) -o $@ $^ -O3 # Rule to compile source files to object files $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) - g++ -c -o $@ $< -O3 + $(CXX) -c -o $@ $< -O3 # Create the object directory if it doesn't exist $(OBJ_DIR): diff --git a/source/search.cpp b/source/search.cpp index 9593770..c8310c3 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -62,10 +62,12 @@ void ListScores(Board &board) { } static SearchResults Quiescence(Board board, int alpha, int beta) { + if (searchStopped) return 0; + auto currTime = std::chrono::high_resolution_clock::now(); int elapsed = std::chrono::duration_cast(currTime - timeStart).count(); if (elapsed >= timeToSearch) { - searchStopped = true; + StopSearch(); return 0; } @@ -109,10 +111,12 @@ static SearchResults Quiescence(Board board, int alpha, int beta) { } static SearchResults PVS(Board board, int depth, int alpha, int beta) { + if (searchStopped) return 0; + auto currTime = std::chrono::high_resolution_clock::now(); int elapsed = std::chrono::duration_cast(currTime - timeStart).count(); if (elapsed >= timeToSearch) { - searchStopped = true; + StopSearch(); return 0; } @@ -192,9 +196,6 @@ SearchResults ID(Board &board, SearchParams ¶ms) { safeResults = currentResults; } - std::cout << "info nodes " << nodes << '\n'; - std::cout << timeRemaining << std::endl; - if (searchStopped) break; } @@ -203,8 +204,20 @@ SearchResults ID(Board &board, SearchParams ¶ms) { void SearchPosition(Board &board, SearchParams ¶ms) { searchStopped = false; + + SearchResults results = ID(board, params); + auto currTime = std::chrono::high_resolution_clock::now(); + double elapsed = std::chrono::duration_cast< + std::chrono::duration>(currTime - timeStart).count(); + + std::cout << "info " << nodes << " nodes " << nodes/elapsed << " nps\n"; std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()] << squareCoords[results.bestMove.MoveTo()] << '\n'; nodes = 0; } + +void StopSearch() { + searchStopped = true; + std::cout << "Search stopped\n"; +} diff --git a/source/search.h b/source/search.h index 386d584..7a8f52f 100644 --- a/source/search.h +++ b/source/search.h @@ -20,6 +20,7 @@ public: }; void SearchPosition(Board &board, SearchParams ¶ms); +void StopSearch(); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, diff --git a/source/uci.cpp b/source/uci.cpp index 481ea84..b70f39f 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "types.h" #include "movegen.h" #include "search.h" @@ -162,8 +163,9 @@ void ParseGo(Board &board, std::string &command) { params.winc = ReadParam("winc", command); params.binc = ReadParam("binc", command); params.movesToGo = ReadParam("movestogo", command); - - SearchPosition(board, params); + + auto worker = std::thread(SearchPosition, std::ref(board), std::ref(params)); + worker.detach(); } void UCILoop(Board &board) { @@ -222,11 +224,20 @@ void UCILoop(Board &board) { break; } + // parse UCI "stop" command + if (input.find("stop") != std::string::npos) { + // stop the loop + StopSearch(); + continue; + } + // parse UCI "uci" command if (input.find("uci") != std::string::npos) { // print engine info std::cout << "id name Eleanor\n"; std::cout << "id name rektdie\n"; + std::cout << "option name Hash type spin default 1 min 1 max 1\n"; + std::cout << "option name Threads type spin default 1 min 1 max 1\n"; std::cout << "uciok\n"; continue; }