summaryrefslogtreecommitdiff
path: root/referee.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'referee.cpp')
-rw-r--r--referee.cpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/referee.cpp b/referee.cpp
new file mode 100644
index 0000000..dad469c
--- /dev/null
+++ b/referee.cpp
@@ -0,0 +1,141 @@
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <string>
+//#define NDEBUG
+#include <cassert>
+#include <sys/time.h>
+#include <unistd.h>
+#include "common.h"
+
+using namespace std;
+
+int main(int argc,char **argv){
+ struct timeval tv;
+ gettimeofday(&tv,NULL);
+ srand(tv.tv_sec*1000000+tv.tv_usec);
+
+ bool uselogfile=argc!=1;
+ ofstream logfile,htmlfile;
+ if(uselogfile){
+ logfile.open(argv[1]);
+ if(!logfile){
+ cerr<<argv[0]<<": could not open logfile '"<<(argc==1?"/dev/null":argv[1])<<"'!"<<endl;
+ uselogfile=false;
+ } else {
+ logfile<<"Game log: "<<argv[1]<<endl;
+ string htmlfname=argv[1];
+ htmlfname+=".log";
+ htmlfile.open(htmlfname);
+ if(!htmlfile){
+ logfile.close();
+ uselogfile=false;
+ }
+ }
+ }
+ int i;
+ Board bd;
+ Move lastmove[NPLAYERS];
+ bool disqual[NPLAYERS],lost[NPLAYERS];
+ memset(disqual,0,NPLAYERS*sizeof(bool));
+ memset(lost,0,NPLAYERS*sizeof(bool));
+ string status[NPLAYERS];
+ int turnidx;
+ string line;
+ int win=-1;
+ cout<<"I lock"<<endl;
+ getline(cin,line);
+ assert(line=="lock_ok");
+ for(i=0;i<NPLAYERS;i++){
+ cout<<i+1<<' '<<(char)('A'+i)<<endl;
+ }
+ cout<<"I unlock"<<endl;
+ for(turnidx=0;;turnidx++){
+ const int turnmodn=turnidx%NPLAYERS;
+ if(lost[turnmodn]){
+ lastmove[turnmodn].x=-1;
+ lastmove[turnmodn].y=-1;
+ continue;
+ }
+ if(disqual[turnmodn]){
+ lastmove[turnmodn]=randommove(bd,turnmodn);
+ } else {
+ cout<<"I lock"<<endl;
+ getline(cin,line);
+ assert(line=="lock_ok");
+ cout<<turnmodn+1;
+ for(i=turnmodn+1;i%NPLAYERS!=turnmodn;i++){
+ cout<<' '<<lastmove[i%NPLAYERS].x<<' '<<lastmove[i%NPLAYERS].y;
+ }
+ cout<<endl;
+ cout<<"I listen "<<turnmodn+1<<endl;
+ getline(cin,line);
+ if(line.compare(0,16,"<caiaio:_player_")==0){
+ disqual[turnmodn]=true;
+ status[turnmodn]+=line.substr(15);
+ lastmove[turnmodn]=randommove(bd,turnmodn);
+ cout<<turnmodn+1<<" Quit"<<endl;
+ if(uselogfile)logfile<<turnidx<<" - P"<<turnmodn+1<<": "<<status[turnmodn]<<endl;
+ } else {
+ int x,y;
+ stringstream ss(line);
+ ss>>x>>y;
+ if(!bd.putq(x,y,turnmodn)){
+ disqual[turnmodn]=true;
+ status[turnmodn]+="_invalid_move";
+ lastmove[turnmodn]=randommove(bd,turnmodn);
+ cout<<turnmodn+1<<" Quit"<<endl;
+ if(uselogfile)logfile<<"- P"<<turnmodn+1<<": "<<status[turnmodn]<<" ("<<x<<' '<<y<<')'<<endl;
+ } else {
+ lastmove[turnmodn].x=x;
+ lastmove[turnmodn].y=y;
+ }
+ }
+ cout<<"I unlock"<<endl;
+ }
+ bd.put(lastmove[turnmodn].idx(),turnmodn);
+ if(uselogfile){
+ logfile<<turnidx<<" P"<<turnmodn+1<<": "<<lastmove[turnmodn].x<<' '<<lastmove[turnmodn].y<<endl;
+ htmlfile<<turnidx<<':'<<endl;
+ bd.print(htmlfile);
+ htmlfile<<endl;
+ }
+ if(turnidx>=NPLAYERS){
+ win=bd.checkwin();
+ if(win!=-1){
+ status[win]+="_won";
+ break;
+ }
+ if(bd.ballcount(turnmodn)==0){
+ lost[turnmodn]=true;
+ status[turnmodn]+="_lost";
+ }
+ }
+ for(i=0;i<NPLAYERS;i++)if(!disqual[i]&&!lost[i])break;
+ if(i==NPLAYERS)break;
+ }
+ if(uselogfile)logfile<<endl;
+ cout<<"I lock"<<endl;
+ getline(cin,line);
+ assert(line=="lock_ok");
+ for(i=0;i<NPLAYERS;i++){
+ cout<<"I request_time "<<i+1<<endl;
+ getline(cin,line);
+ if(uselogfile)logfile<<"player "<<i+1<<": "<<line<<endl;
+ if(!lost[i]&&!disqual[i])cout<<i+1<<" Quit"<<endl;
+ }
+ if(uselogfile)logfile.close();
+ cout<<"I unlock"<<endl;
+ cout<<'M';
+ for(i=0;i<NPLAYERS;i++){
+ cout<<' ';
+ if(disqual[i])cout<<'0';
+ else if(i==win)cout<<'3';
+ else cout<<'1';
+ }
+ for(i=0;i<NPLAYERS;i++){
+ if(status[i].size()==0)cout<<" --";
+ else cout<<' '<<status[i];
+ }
+ cout<<endl;
+}