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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <limits>
using namespace std;
struct Move{
int neudir,from,dir;
string str(void){
stringstream ss;
ss<<neudir<<' '<<from<<' '<<dir;
return ss.str();
}
string json(void){
stringstream ss;
ss<<'['<<neudir<<','<<from<<','<<dir<<']';
return ss.str();
}
};
const char *html_string[5]={
"<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<meta charset=\"utf-8\">\n\
<title>Game</title>\n\
<script>\n\
var S=" , ";\n\
var PLAYERS=[" , "];\n\
var MOVES=[" , "null];MOVES.pop();\n\
var WINNER=" , ";\n\
</script>\n\
<script src=\"../viewcompetition.js\"></script>\n\
<link rel=\"stylesheet\" type=\"text/css\" href=\"../viewcompetition.css\">\n\
</head>\n\
<body>\n\
<h1 id=\"header\"></h1>\n\
<table id=\"bgtab\"><tbody id=\"bgtbody\"></tbody></table><br>\n\
<input type=\"button\" id=\"prevmovebtn\" onclick=\"prevmove()\" value=\"←\">\n\
<input type=\"button\" id=\"nextmovebtn\" onclick=\"nextmove()\" value=\"→\"><br>\n\
<div id=\"movelist\" style=\"margin-top:30px\"></div>\n\
<div id=\"status\" style=\"margin-top:50px\"></div>\n\
</body>\n\
</html>\n"};
int main(int argc,char **argv){
if(argc==1){
cerr<<"Pass the file name of the competition log as a command-line parameter."<<endl;
return 1;
} else if(argc>2){
cerr<<"Multiple command-line arguments were passed."<<endl;
return 1;
}
ifstream in(argv[1]);
if(!in.good()){
cerr<<"Cannot open file '"<<argv[1]<<"'."<<endl;
return 1;
}
string p1name,p2name;
int player;
char c;
Move mv;
cout<<html_string[0];
cout<<5;
cout<<html_string[1];
in.get(); in.get(); in.get(); in.get(); //"P1: "
getline(in,p1name);
in.get(); in.get(); in.get(); in.get(); //"P2: "
getline(in,p2name);
cout<<'"'<<p1name<<"\",\""<<p2name<<"\"";
cout<<html_string[2];
if(!in.good()){
cerr<<"Error in log file format."<<endl;
return 1;
}
while(in.good()){
c=in.get(); //"P" for a move line or a win line, "T" for a tie line
if(c=='P'){
player=in.get()-'0';
c=in.get(); //":" for a move line, " " for a win line
if(c==':'){
in>>mv.neudir>>mv.from>>mv.dir;
in.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<mv.json()<<',';
} else if(c==' '){
cout<<html_string[3];
cout<<player;
cout<<html_string[4]<<flush;
in.close();
return 0;
} else {
cerr<<"Error in log file format."<<endl;
return 1;
}
} else if(c=='T'){
cout<<html_string[3];
cout<<-1;
cout<<html_string[4]<<flush;
in.close();
return 0;
}
}
cerr<<"Log file ended prematurely."<<endl;
return 1;
}
|