Skip to content

Commit

Permalink
Queue commands received during search.
Browse files Browse the repository at this point in the history
  • Loading branch information
petur committed Sep 25, 2022
1 parent a10bca7 commit d2ad746
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
14 changes: 5 additions & 9 deletions profile.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/bin/bash

(
printf 'uci\nsetoption name Hash value 16\nucinewgame\nisready\n\nposition startpos\ngo wtime 3600000\n'
sleep 8
printf 'stop\n'
printf 'position fen rn1qkb1r/1pp2pP1/3p4/p5Bp/4P3/2N5/PPP2PPP/R2bKB1R b KQkq - 0 9\ngo btime 3600000\n'
sleep 2
printf 'stop\n'
printf 'position fen rn2kb2/p3p1p1/2p1r3/1P2q3/3PBPb1/4Q2p/1P1P2PP/R1B1K2R w KQq - 0 17\ngo wtime 3600000\n'
sleep 2
printf 'stop\nquit\n'
printf 'uci\nsetoption name Hash value 64\nucinewgame\nisready\n\nposition startpos\ngo wtime 180000\n'
printf 'position fen rn1qkb1r/1pp2pP1/3p4/p5Bp/4P3/2N5/PPP2PPP/R2bKB1R b KQkq - 0 9\ngo btime 60000\n'
printf 'position fen rn2kb2/p3p1p1/2p1r3/1P2q3/3PBPb1/4Q2p/1P1P2PP/R1B1K2R w KQq - 0 17\ngo wtime 60000\n'
printf 'position fen 8/8/7k/8/8/7K/1P6/8 b - - 0 1\ngo btime 10000\n'
printf 'quit\n'
) | "$1"
28 changes: 23 additions & 5 deletions seawall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cassert>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
Expand Down Expand Up @@ -1140,6 +1141,7 @@ struct Search
{
std::istream& in;
std::ostream& out;
std::deque<std::string>& commands;
std::clock_t total_time;
std::clock_t increment;
std::clock_t movetime;
Expand All @@ -1151,7 +1153,9 @@ struct Search
bool stopped;
Stack* stack;

Search(std::istream& i, std::ostream& o, std::clock_t time, std::clock_t inc, std::clock_t movetime, int moves_to_go, Stack* stack);
Search(
std::istream& i, std::ostream& o, std::deque<std::string>& cmd,
std::clock_t time, std::clock_t inc, std::clock_t movetime, int moves_to_go, Stack* stack);

bool is_stopped();
bool check_stop_command();
Expand All @@ -1172,8 +1176,10 @@ struct Search
void undo_move(Move mv, const Memo& memo) { position.undo_move(mv, memo); }
};

Search::Search(std::istream& i, std::ostream& o, std::clock_t time, std::clock_t inc, std::clock_t mt, int mtg, Stack* st)
: in{i}, out{o}, total_time{time}, increment{inc}, movetime{mt}, moves_to_go{mtg}, max_time{mt},
Search::Search(
std::istream& i, std::ostream& o, std::deque<std::string>& cmd,
std::clock_t time, std::clock_t inc, std::clock_t mt, int mtg, Stack* st)
: in{i}, out{o}, commands{cmd}, total_time{time}, increment{inc}, movetime{mt}, moves_to_go{mtg}, max_time{mt},
start{std::clock()}, nodes{}, sel_depth{}, stopped{}, stack{st}
{
if (total_time != static_cast<std::clock_t>(-1))
Expand Down Expand Up @@ -1211,6 +1217,8 @@ bool Search::check_stop_command()
return true;
else if (token == "isready")
out << "readyok" << std::endl;
else
commands.push_back(line);
}
return false;
}
Expand Down Expand Up @@ -1577,6 +1585,15 @@ bool find_best_value(std::pair<int*, double>& variable, double& best_error, int

constexpr char startfen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

bool get_command(std::istream& in, std::deque<std::string>& commands, std::string& line)
{
if (commands.empty())
return static_cast<bool>(getline(in, line));
line = commands.front();
commands.pop_front();
return true;
}

int main()
{
std::ios::sync_with_stdio(false);
Expand Down Expand Up @@ -1675,8 +1692,9 @@ int main()
std::size_t hash_mb = 1;
bool debug = false;
Stack stack[256] = {};
std::deque<std::string> commands;

while (getline(std::cin, line))
while (get_command(std::cin, commands, line))
{
std::istringstream parser{line};
std::string token;
Expand Down Expand Up @@ -1793,7 +1811,7 @@ int main()
}

++hash_generation;
Search{std::cin, std::cout, time, inc, movetime, moves_to_go, &stack[position.halfmove_clock]}.iterate(max_depth);
Search{std::cin, std::cout, commands, time, inc, movetime, moves_to_go, &stack[position.halfmove_clock]}.iterate(max_depth);
}
else if (token == "quit")
{
Expand Down
7 changes: 0 additions & 7 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ ucinewgame
isready'
echo 'position fen rn1qkb1r/1pp2pP1/3p4/p5Bp/4P3/2N5/PPP2PPP/R2bKB1R b KQkq - 0 9'
echo 'go wtime 2404 winc 30 btime 2503 binc 30'
sleep 0.4
echo 'go wtime 2404 winc 30 btime 2503 binc 30'
sleep 0.4
echo 'stop'
echo 'position fen rn2kb2/p3p1p1/2p1r3/1P2q3/3PBPb1/4Q2p/1P1P2PP/R1B1K2R w KQq - 0 17'
echo 'go wtime 2095 winc 30 btime 1849 binc 30'
sleep 0.4
echo 'go wtime 2095 winc 30 btime 1849 binc 30'
sleep 0.4
echo 'quit'
) | ./seawall

0 comments on commit d2ad746

Please sign in to comment.