diff options
Diffstat (limited to 'referee.h')
-rw-r--r-- | referee.h | 95 |
1 files changed, 50 insertions, 45 deletions
@@ -5,49 +5,58 @@ #include <string_view> #include <optional> #include <utility> +#include <functional> +#include <variant> #include "process.h" using namespace std; -struct Features { - // If true, players should not receive the last move on stdin. - // Used by the match runner. - bool noLastMove = false; - - // If true, the referee program can write additional lines to the players. - // Used by the Referee class to produce playerWriteLines() output. - bool writeLines = false; -}; - class Referee { Process proc; string refereeExecname; - bool isEnd = false; - vector<int> scores; + int numPlayers; + + string reftag; + struct Features { + // Referee supports current version of the protocol. + bool version2; + }; Features features; - vector<pair<int, string>> writeLinesList; void queryFeatures(); - void readScores(); - void readWriteLines(); public: + struct ReadEvent { + int player; + // call with the line read (no newline) + function<void(const string&)> callback; + }; + struct WriteEvent { + int player; + string line; + }; + struct GamelogEvent { + int player; + string line; + }; + struct EndEvent { + vector<int> scores; + }; + struct ErrorEvent { + int player; + string message; + }; + using Event = variant<ReadEvent, WriteEvent, GamelogEvent, EndEvent, ErrorEvent>; + Referee(const string_view execname, const vector<string> &players); ~Referee(); - // player: 0 = first player, 1 = second player - bool moveValid(int player, const string_view line); - vector<pair<int, string>> playerWriteLines(); - - bool gameEnded(); - optional<vector<int>> getScores(); + Event nextEvent(); // Kills the referee process void terminate(); - - const Features& getFeatures() const; }; /* @@ -56,32 +65,28 @@ Referee protocol: At startup, the referee should write a number of lines to stdout of the form 'feature <name>', indicating that the referee supports the feature given by <name>. The list of features should be ended with a line containing -'feature_end'. For supported features and their effect, see below. +'feature_end'. To use the version of the protocol described here, it is +mandatory to request 'feature version_2'. For supported features and their +effect, see below. The referee then receives a line containing the number of players in the game, followed by that number of lines containing the players' names. -Then, it repeatedly receives a line on stdin of the form '<player> <line>', -where <player> is the index of the player in the game (0 is first, 1 is second, -etc.) and <line> is the line as written out by the player. The referee should -then write a line to stdout that is equal to either 'valid', 'invalid', or -'valid end'. 'valid' and 'invalid' indicate a valid and invalid move, while -'valid end' indicates a valid move that ends the game. After writing the line -'valid end', the referee should write a line containing as many integers -(separated by spaces) as there are players, giving their scores at the end of -the game. +Then, the referee has the following commands available: +- 'read <player>': read a line from the player with index <player>; this line + is written directly to the referee afterwards. The first player is 0, the + second player is 1, etc. +- 'write <player> <line>': write a line to the player with index <player>. +- 'gamelog <player> <line>': write a line in the game log indicating that the + player with index <player> made the move given by <line>. +- 'end <score> <score> ...': signify that the game has ended. The final scores + of the players are given after 'end'. These scores should be given as a + space-separated list of integers, as many as there are players. +- 'error <player> <message>': indicate that the player with index <player> has + produced erroneous output. This ends the game. The <message> will be + presented to the user and written to the gamelog. FEATURES -The following features are supported: -- 'write_lines': After having received the list of players at the start of the - protocol, and after a move validity judgement, the referee should write lines - of the form '<player> <line>' to stdout, where <player> is the index of the - player on whose stdin to write <line>. The referee should signal the end of - this list by writing a line 'write_end' to stdout. This allows the referee to - write additional data to players' inputs after they have written their moves. - Note that 'valid end' is also a move validity judgement, and the player lines - should be written *before* writing the scores. -- 'no_last_move': Prevent the competition manager from writing the last player's - last move to stdin of the next player. Should probably be used in conjunction - with 'write_lines'. +Currently, the following features are supported: +- 'version_2': must be used by all referees. */ |