Implement sit and go commands for bughouse
authorFabian Fichter <ianfab@users.noreply.github.com>
Fri, 10 Jan 2020 13:30:51 +0000 (14:30 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Fri, 10 Jan 2020 14:36:17 +0000 (15:36 +0100)
- Listen to `sit` and `go` commands
- Automatically sit when in a forced mate

src/parser.cpp
src/position.h
src/search.cpp
src/thread.h
src/variant.cpp
src/variant.h
src/variants.ini
src/xboard.cpp

index 52a66ca..55b9afb 100644 (file)
@@ -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
index 3ea59f3..b231417 100644 (file)
@@ -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<PieceType>& 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);
index d022a0f..b3b3e0f 100644 (file)
@@ -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;
           }
       }
index ac9de07..fc520fd 100644 (file)
@@ -110,6 +110,7 @@ struct ThreadPool : public std::vector<Thread*> {
 
   std::atomic_bool stop;
   std::atomic_bool abort;
+  std::atomic_bool sit;
 
   StateListPtr setupStates;
 
index 708cbcb..0c85ec1 100644 (file)
@@ -276,6 +276,7 @@ namespace {
     Variant* bughouse_variant() {
         Variant* v = crazyhouse_variant();
         v->variantTemplate = "bughouse";
+        v->twoBoards = true;
         v->capturesToHand = false;
         return v;
     }
index f1e1c80..d7be1f8 100644 (file)
@@ -38,6 +38,7 @@ struct Variant {
   Rank maxRank = RANK_8;
   File maxFile = FILE_H;
   bool chess960 = false;
+  bool twoBoards = false;
   std::set<PieceType> 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, ' ');
index 5f55b45..cbaa530 100644 (file)
@@ -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)
index bc65956..9972169 100644 (file)
@@ -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")
   {