From a5a1c3d654c34ff9e043182098b1e49a2da71e09 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Fri, 10 Jan 2020 14:30:51 +0100 Subject: [PATCH] Implement sit and go commands for bughouse - Listen to `sit` and `go` commands - Automatically sit when in a forced mate --- src/parser.cpp | 1 + src/position.h | 6 ++++++ src/search.cpp | 4 ++-- src/thread.h | 1 + src/variant.cpp | 1 + src/variant.h | 1 + src/variants.ini | 1 + src/xboard.cpp | 19 ++++++++++++++++++- 8 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 52a66ca..55b9afb 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -124,6 +124,7 @@ Variant* VariantParser::parse(Variant* v) { parse_attribute("maxRank", v->maxRank); parse_attribute("maxFile", v->maxFile); parse_attribute("chess960", v->chess960); + parse_attribute("twoBoards", v->twoBoards); parse_attribute("startFen", v->startFen); parse_attribute("promotionRank", v->promotionRank); // promotion piece types diff --git a/src/position.h b/src/position.h index 3ea59f3..b231417 100644 --- a/src/position.h +++ b/src/position.h @@ -95,6 +95,7 @@ public: const Variant* variant() const; Rank max_rank() const; File max_file() const; + bool two_boards() const; Bitboard board_bb() const; Bitboard board_bb(Color c, PieceType pt) const; const std::set& piece_types() const; @@ -317,6 +318,11 @@ inline File Position::max_file() const { return var->maxFile; } +inline bool Position::two_boards() const { + assert(var != nullptr); + return var->twoBoards; +} + inline Bitboard Position::board_bb() const { assert(var != nullptr); return board_size_bb(var->maxFile, var->maxRank); diff --git a/src/search.cpp b/src/search.cpp index d022a0f..b3b3e0f 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -260,7 +260,7 @@ void MainThread::search() { // GUI sends a "stop" or "ponderhit" command. We therefore simply wait here // until the GUI sends one of those commands. - while (!Threads.stop && (ponder || Limits.infinite)) + while (!Threads.stop && (ponder || Limits.infinite || (rootPos.two_boards() && (Threads.sit || this->rootMoves[0].score <= VALUE_MATED_IN_MAX_PLY) && Time.elapsed() < Limits.time[us] - 1000))) {} // Busy wait for a stop or a ponder reset // Stop the threads if not already stopped (also raise the stop if @@ -558,7 +558,7 @@ void Thread::search() { // keep pondering until the GUI sends "ponderhit" or "stop". if (mainThread->ponder) mainThread->stopOnPonderhit = true; - else + else if (!Threads.sit && !(rootPos.two_boards() && bestValue <= VALUE_MATED_IN_MAX_PLY)) Threads.stop = true; } } diff --git a/src/thread.h b/src/thread.h index ac9de07..fc520fd 100644 --- a/src/thread.h +++ b/src/thread.h @@ -110,6 +110,7 @@ struct ThreadPool : public std::vector { std::atomic_bool stop; std::atomic_bool abort; + std::atomic_bool sit; StateListPtr setupStates; diff --git a/src/variant.cpp b/src/variant.cpp index 708cbcb..0c85ec1 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -276,6 +276,7 @@ namespace { Variant* bughouse_variant() { Variant* v = crazyhouse_variant(); v->variantTemplate = "bughouse"; + v->twoBoards = true; v->capturesToHand = false; return v; } diff --git a/src/variant.h b/src/variant.h index f1e1c80..d7be1f8 100644 --- a/src/variant.h +++ b/src/variant.h @@ -38,6 +38,7 @@ struct Variant { Rank maxRank = RANK_8; File maxFile = FILE_H; bool chess960 = false; + bool twoBoards = false; std::set pieceTypes = { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING }; std::string pieceToChar = " PNBRQ" + std::string(KING - QUEEN - 1, ' ') + "K" + std::string(PIECE_TYPE_NB - KING - 1, ' ') + " pnbrq" + std::string(KING - QUEEN - 1, ' ') + "k" + std::string(PIECE_TYPE_NB - KING - 1, ' '); diff --git a/src/variants.ini b/src/variants.ini index 5f55b45..cbaa530 100644 --- a/src/variants.ini +++ b/src/variants.ini @@ -98,6 +98,7 @@ # maxRank: maximum rank [Rank] (default: 8) # maxFile: maximum file [File] (default: 8) # chess960: allow chess960 castling [bool] (default: false) +# twoBoards: the game is influenced by a second board (e.g., bughouse) [bool] (default: false) # startFen: FEN of starting position (default: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1) # promotionRank: relative rank required to reach for promotion [Rank] (default: 8) # promotionPieceTypes: pawn promotion options using their one-letter representations (default: nbrq) diff --git a/src/xboard.cpp b/src/xboard.cpp index bc65956..9972169 100644 --- a/src/xboard.cpp +++ b/src/xboard.cpp @@ -124,6 +124,7 @@ void StateMachine::process_command(Position& pos, std::string token, std::istrin setboard(pos, states); // play second by default playColor = ~pos.side_to_move(); + Threads.sit = false; } else if (token == "variant") { @@ -236,7 +237,23 @@ void StateMachine::process_command(Position& pos, std::string token, std::istrin else if (token == "partner") {} // ignore for now else if (token == "ptell") { - // TODO: parse requests by partner + // parse requests by partner + is >> token; + if (token == "help") + sync_cout << "tellics ptell I listen to the commands help, sit, and go." << sync_endl; + else if (token == "hi" || token == "hello") + sync_cout << "tellics ptell hi" << sync_endl; + else if (token == "sit") + { + Threads.stop = false; + Threads.sit = true; + sync_cout << "tellics ptell I sit, tell me 'go' to continue" << sync_endl; + } + else if (token == "go") + { + Threads.sit = false; + Threads.stop = true; + } } else if (token == "holding") { -- 1.7.0.4