- Position pos(StartFEN, false, Threads.main()); // The root position
- string token, cmd;
-
- for (int i = 1; i < argc; ++i)
- cmd += std::string(argv[i]) + " ";
-
- do {
- if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input
- cmd = "quit";
-
- istringstream is(cmd);
-
- token.clear(); // getline() could return empty or blank line
- is >> skipws >> token;
-
- if (token == "quit" || token == "stop" || token == "ponderhit")
- {
- // The GUI sends 'ponderhit' to tell us to ponder on the same move the
- // opponent has played. In case Signals.stopOnPonderhit is set we are
- // waiting for 'ponderhit' to stop the search (for instance because we
- // already ran out of time), otherwise we should continue searching but
- // switch from pondering to normal search.
- if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
- {
- Search::Signals.stop = true;
- Threads.main()->notify_one(); // Could be sleeping
- }
- else
- Search::Limits.ponder = false;
- }
- else if (token == "perft")
- {
- int depth;
- stringstream ss;
-
- is >> depth;
- ss << Options["Hash"] << " "
- << Options["Threads"] << " " << depth << " current " << token;
-
- benchmark(pos, ss);
- }
- else if (token == "key")
- sync_cout << hex << uppercase << setfill('0')
- << "position key: " << setw(16) << pos.key()
- << "\nmaterial key: " << setw(16) << pos.material_key()
- << "\npawn key: " << setw(16) << pos.pawn_key()
- << dec << nouppercase << setfill(' ') << sync_endl;
-
- else if (token == "uci")
- sync_cout << "id name " << engine_info(true)
- << "\n" << Options
- << "\nuciok" << sync_endl;
-
- else if (token == "ucinewgame") TT.clear();
- else if (token == "go") go(pos, is);
- else if (token == "position") position(pos, is);
- else if (token == "setoption") setoption(is);
- else if (token == "flip") pos.flip();
- else if (token == "bench") benchmark(pos, is);
- else if (token == "d") sync_cout << pos << sync_endl;
- else if (token == "isready") sync_cout << "readyok" << sync_endl;
- else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
- else
- sync_cout << "Unknown command: " << cmd << sync_endl;
-
- } while (token != "quit" && argc == 1); // Passed args have one-shot behaviour
-
- Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
+ Position pos;
+ std::string token, cmd;
+ StateListPtr states(new std::deque<StateInfo>(1));
+
+ pos.set(StartFEN, false, &states->back(), Threads.main());
+
+ for (int i = 1; i < argc; ++i)
+ cmd += std::string(argv[i]) + " ";
+
+ do
+ {
+ if (argc == 1
+ && !getline(std::cin, cmd)) // Wait for an input or an end-of-file (EOF) indication
+ cmd = "quit";
+
+ std::istringstream is(cmd);
+
+ token.clear(); // Avoid a stale if getline() returns nothing or a blank line
+ is >> std::skipws >> token;
+
+ if (token == "quit" || token == "stop")
+ Threads.stop = true;
+
+ // The GUI sends 'ponderhit' to tell that the user has played the expected move.
+ // So, 'ponderhit' is sent if pondering was done on the same move that the user
+ // has played. The search should continue, but should also switch from pondering
+ // to the normal search.
+ else if (token == "ponderhit")
+ Threads.main()->ponder = false; // Switch to the normal search
+
+ else if (token == "uci")
+ sync_cout << "id name " << engine_info(true) << "\n"
+ << Options << "\nuciok" << sync_endl;
+
+ else if (token == "setoption")
+ setoption(is);
+ else if (token == "go")
+ go(pos, is, states);
+ else if (token == "position")
+ position(pos, is, states);
+ else if (token == "ucinewgame")
+ Search::clear();
+ else if (token == "isready")
+ sync_cout << "readyok" << sync_endl;
+
+ // Add custom non-UCI commands, mainly for debugging purposes.
+ // These commands must not be used during a search!
+ else if (token == "flip")
+ pos.flip();
+ else if (token == "bench")
+ bench(pos, is, states);
+ else if (token == "d")
+ sync_cout << pos << sync_endl;
+ else if (token == "eval")
+ trace_eval(pos);
+ else if (token == "compiler")
+ sync_cout << compiler_info() << sync_endl;
+ else if (token == "export_net")
+ {
+ std::optional<std::string> filename;
+ std::string f;
+ if (is >> std::skipws >> f)
+ filename = f;
+ Eval::NNUE::save_eval(filename);
+ }
+ else if (token == "--help" || token == "help" || token == "--license" || token == "license")
+ sync_cout
+ << "\nStockfish is a powerful chess engine for playing and analyzing."
+ "\nIt is released as free software licensed under the GNU GPLv3 License."
+ "\nStockfish is normally used with a graphical user interface (GUI) and implements"
+ "\nthe Universal Chess Interface (UCI) protocol to communicate with a GUI, an API, etc."
+ "\nFor any further information, visit https://github.com/official-stockfish/Stockfish#readme"
+ "\nor read the corresponding README.md and Copying.txt files distributed along with this program.\n"
+ << sync_endl;
+ else if (!token.empty() && token[0] != '#')
+ sync_cout << "Unknown command: '" << cmd << "'. Type help for more information."
+ << sync_endl;
+
+ } while (token != "quit" && argc == 1); // The command-line arguments are one-shot