stellarUCI Chess engine written in C++20 |
git clone git://git.dimitrijedobrota.com/stellar.git |
Log | Files | Refs | README | LICENSE |
arena.cpp (2990B)
0 #include <format>
1 #include <functional>
2 #include <iostream>
3 #include <mutex>
4 #include <thread>
6 #include <bits/getopt_core.h>
7 #include <stdexcept>
9 #include "logger.hpp"
10 #include "match.hpp"
12 class Arena {
13 public:
14 Arena(const Arena &) = delete;
15 Arena &operator==(const Arena &) = delete;
16 Arena(const char *name1, const char *name2) : engine1(new Engine(name1)), engine2(new Engine(name2)) {
17 logger::log(std::format("Arena {}: created", id), logger::Debug);
18 }
20 ~Arena() {
21 delete (engine1), delete (engine2);
22 logger::log(std::format("Arena {}: destroyed", id), logger::Debug);
23 }
25 void operator()(const std::vector<std::string> positions, Match::Settings swhite,
26 Match::Settings sblack) {
27 Match match(*engine1, *engine2);
28 for (const std::string &fen : positions) {
29 logger::log(
30 std::format("Arena {}: match from {}", id, fen == Game::startPosition ? "startpos" : fen),
31 logger::Debug);
32 Arena::print(match.play(swhite, sblack, fen));
33 }
34 }
36 private:
37 static void print(const Game game) {
38 mutex.lock();
39 std::cout << game << std::endl;
40 mutex.unlock();
41 }
43 static uint16_t id_t;
44 uint16_t id = id_t++;
46 Engine *engine1;
47 Engine *engine2;
49 static std::mutex mutex;
50 };
52 uint16_t Arena::id_t = 0;
54 std::mutex Arena::mutex;
55 void usage(const char *program) {
56 std::cerr << program << ": ";
57 std::cerr << "[-f fen]";
58 std::cerr << "\n[-G wmovestogo -g bmovestogo]";
59 std::cerr << "\n[-D wdepth -d bdepth]";
60 std::cerr << "\n[-T wtime -t btime]";
61 std::cerr << "\n[-I winc -i binc]";
62 std::cerr << "\n-E engine1 -e engine2";
63 std::cerr << "\n[- startpos] ... fen ...";
64 }
66 int main(int argc, char *argv[]) {
67 char *engine1 = NULL, *engine2 = NULL;
68 Match::Settings settings1, settings2;
70 char c;
71 while ((c = getopt(argc, argv, "hE:e:D:d:T:t:I:i:G:g:N:")) != -1) {
72 switch (c) {
73 case 'E': engine1 = optarg; break;
74 case 'e': engine2 = optarg; break;
75 case 'D': settings1.depth = atoi(optarg); break;
76 case 'd': settings2.depth = atoi(optarg); break;
77 case 'T': settings1.time = atoll(optarg); break;
78 case 't': settings2.time = atoll(optarg); break;
79 case 'I': settings1.inc = atoll(optarg); break;
80 case 'i': settings2.inc = atoll(optarg); break;
81 case 'G': settings1.togo = atoi(optarg); break;
82 case 'g': settings2.togo = atoi(optarg); break;
83 case 'h': usage(argv[0]); return 1;
84 default: usage(argv[0]), abort();
85 }
86 }
88 if (!engine1 || !engine2) {
89 usage(argv[0]);
90 abort();
91 }
93 std::vector<std::string> positions;
94 for (int i = optind; i < argc; i++)
95 positions.push_back(!strcmp(argv[i], "-") ? start_position : argv[i]);
97 Arena arena(engine1, engine2);
98 arena(positions, settings1, settings2);
100 return 0;
101 }