summaryrefslogtreecommitdiff
path: root/referee.cpp
blob: dad469c0363f7a7f3431db5e7ecb474692ca102d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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;
}