blob: 7bf3a93088f2d8f283ebf4527dd7c05169ceb920 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <iostream>
#include <cstdlib>
#include "referee.h"
#include "params.h"
Referee::Referee(const string_view execname, const vector<string> &players)
: proc(execname) {
proc.run();
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") starting for:";
for (const string &p : players) cout << " " << p;
cout << endl;
}
proc.writeLine(to_string(players.size()));
for (const string &p : players) {
proc.writeLine(p);
}
}
Referee::~Referee() {
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") stopping" << endl;
}
proc.wait();
proc.terminate();
}
bool Referee::moveValid(int player, const string_view line) {
string s = to_string(player) + " ";
s.append(line);
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") write <" << s << ">" << endl;
}
proc.writeLine(s);
optional<string> response = proc.readLine();
if (response) {
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") read <" << *response << ">" << endl;
}
if (*response == "valid end") {
isEnd = true;
readScores();
return true;
} else if (*response == "valid") {
return true;
} else {
return false;
}
} else {
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") EOF!" << endl;
}
return false;
}
}
bool Referee::gameEnded() {
return isEnd;
}
void Referee::readScores() {
optional<string> line = proc.readLine();
if (!line) {
cerr << "Referee stopped before providing scores" << endl;
exit(1);
}
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") readScores <" << *line << ">" << endl;
}
size_t cursor = 0;
while (cursor < line->size()) {
size_t idx = line->find(' ', cursor);
if (idx == string::npos) idx = line->size();
scores.push_back(stoi(line->substr(cursor, idx - cursor)));
cursor = idx + 1;
}
}
optional<vector<int>> Referee::getScores() {
if (isEnd) return scores;
else return nullopt;
}
void Referee::terminate() {
proc.terminate();
}
|