summaryrefslogtreecommitdiff
path: root/referee.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'referee.cpp')
-rw-r--r--referee.cpp70
1 files changed, 57 insertions, 13 deletions
diff --git a/referee.cpp b/referee.cpp
index 7bf3a93..15f49c3 100644
--- a/referee.cpp
+++ b/referee.cpp
@@ -39,21 +39,45 @@ bool Referee::moveValid(int player, const string_view line) {
proc.writeLine(s);
- optional<string> response = proc.readLine();
+ optional<string> response = readLine();
if (response) {
- if (referee_verbose) {
- cout << "REF(" << proc.getPid() << ") read <" << *response << ">" << endl;
- }
-
+ bool result;
if (*response == "valid end") {
isEnd = true;
readScores();
- return true;
+ result = true;
} else if (*response == "valid") {
- return true;
+ result = true;
} else {
- return false;
+ result = false;
+ }
+
+ if (featureWriteLines) {
+ writeLinesList.clear();
+
+ while (true) {
+ optional<string> line = readLine();
+ if (!line) {
+ if (referee_verbose) {
+ cout << "REF(" << proc.getPid() << ") EOF!" << endl;
+ }
+ return false;
+ }
+
+ if (line == "write_end") break;
+
+ size_t idx = line->find(' ');
+ if (idx == string::npos) {
+ cerr << "Referee feature_write_lines protocol violation!" << endl;
+ exit(1);
+ }
+
+ int player = stoi(line->substr(0, idx));
+ writeLinesList.emplace_back(player, line->substr(idx + 1));
+ }
}
+
+ return result;
} else {
if (referee_verbose) {
cout << "REF(" << proc.getPid() << ") EOF!" << endl;
@@ -62,21 +86,41 @@ bool Referee::moveValid(int player, const string_view line) {
}
}
+vector<pair<int, string>> Referee::playerWriteLines() {
+ return writeLinesList;
+}
+
bool Referee::gameEnded() {
return isEnd;
}
+optional<string> Referee::readLine() {
+ while (true) {
+ optional<string> line = proc.readLine();
+ if (!line) return line;
+
+ if (referee_verbose) {
+ cout << "REF(" << proc.getPid() << ") read <" << *line << ">" << endl;
+ }
+
+ if (line->substr(0, 8) != "feature ") return line;
+
+ string featname = line->substr(8);
+ if (featname == "write_lines") featureWriteLines = true;
+ else {
+ cerr << "Referee requested unsupported feature '" << featname << "'" << endl;
+ exit(1);
+ }
+ }
+}
+
void Referee::readScores() {
- optional<string> line = proc.readLine();
+ optional<string> line = 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);