aboutsummaryrefslogtreecommitdiff
path: root/viewcompetition.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'viewcompetition.cpp')
-rw-r--r--viewcompetition.cpp125
1 files changed, 60 insertions, 65 deletions
diff --git a/viewcompetition.cpp b/viewcompetition.cpp
index 40ba715..3f15c29 100644
--- a/viewcompetition.cpp
+++ b/viewcompetition.cpp
@@ -1,111 +1,106 @@
#include <iostream>
#include <fstream>
-#include <limits>
-#include <cstdint>
-#include <cassert>
-#ifdef _WIN32
-#include <windows.h>
-#else
-#include <unistd.h>
-#endif
-
-#define SYMBOLFOR(p) ((p)==P1?'X':(p)==P2?'O':(p)==PNONE?'.':'?')
-
-#define PNONE (0)
-#define P1 (1)
-#define P2 (2)
+#include <vector>
+#include <sstream>
using namespace std;
-uint8_t board[81]={PNONE};
-
-void dosleep(int ms){
-#ifdef _WIN32
- Sleep(ms);
-#else
- usleep(ms*1e3);
-#endif
-}
-
-void printboard(void){
- int x,y;
- cout<<"+-------+-------+-------+"<<endl;
- for(y=0;y<9;y++){
- cout<<"| ";
- for(x=0;x<9;x++){
- cout<<SYMBOLFOR(board[9*y+x])<<' ';
- if(x%3==2)cout<<"| ";
- }
- cout<<endl;
- if(y%3==2)cout<<"+-------+-------+-------+"<<endl;
+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=\"&larr;\">\n\
+<input type=\"button\" id=\"nextmovebtn\" onclick=\"nextmove()\" value=\"&rarr;\"><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){
- cout<<"Pass the file name of the competition log as a command-line parameter."<<endl;
+ cerr<<"Pass the file name of the competition log as a command-line parameter."<<endl;
return 1;
} else if(argc>2){
- cout<<"Multiple command-line arguments were passed."<<endl;
+ cerr<<"Multiple command-line arguments were passed."<<endl;
return 1;
}
ifstream in(argv[1]);
if(!in.good()){
- cout<<"Cannot open file '"<<argv[1]<<"'."<<endl;
+ cerr<<"Cannot open file '"<<argv[1]<<"'."<<endl;
return 1;
}
string p1name,p2name;
- int player,x,y;
+ 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<<"\x1B[2J\x1B[1;1HCompetition between:\n"
- "P1 (X): "<<p1name<<"\n"
- "P2 (O): "<<p2name<<endl;
+ cout<<'"'<<p1name<<"\",\""<<p2name<<"\"";
+ cout<<html_string[2];
if(!in.good()){
- cout<<"Error in log file format."<<endl;
+ cerr<<"Error in log file format."<<endl;
return 1;
}
- cout<<"\x1B[4;27HPress enter to play next move"<<flush;
- cout<<"\x1B[4;1H"<<flush;
- printboard();
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.get();
- x=in.get()-'0';
- in.get(); //" "
- y=in.get()-'0';
+ in>>mv.neudir>>mv.from>>mv.dir;
in.ignore(numeric_limits<streamsize>::max(),'\n');
- board[9*y+x]=player;
- cout<<"\x1B["<<(5+y/3+y)<<';'<<(3+x/3*2+x*2)<<'H'<<SYMBOLFOR(player)
- <<"\x1B[5;27H"<<SYMBOLFOR(player)<<": "<<x<<' '<<y
- <<"\x1B[7;27H"<<SYMBOLFOR(haswonsmall(0))<<SYMBOLFOR(haswonsmall(1))<<SYMBOLFOR(haswonsmall(2))
- <<"\x1B[8;27H"<<SYMBOLFOR(haswonsmall(3))<<SYMBOLFOR(haswonsmall(4))<<SYMBOLFOR(haswonsmall(5))
- <<"\x1B[9;27H"<<SYMBOLFOR(haswonsmall(6))<<SYMBOLFOR(haswonsmall(7))<<SYMBOLFOR(haswonsmall(8))<<flush;
- c=cin.get();
- if(c!='\n')cin.ignore(numeric_limits<streamsize>::max(),'\n');
+ cout<<mv.json()<<',';
} else if(c==' '){
- cout<<"\x1B[17;1HThe victor is player "<<player<<"!"<<endl;
+ cout<<html_string[3];
+ cout<<player;
+ cout<<html_string[4]<<flush;
in.close();
- dosleep(1500);
return 0;
} else {
- cout<<"\x1B[17;1HError in log file format."<<endl;
+ cerr<<"Error in log file format."<<endl;
return 1;
}
} else if(c=='T'){
- cout<<"\x1B[17;1HThe game resulted in a tie."<<endl;
+ cout<<html_string[3];
+ cout<<-1;
+ cout<<html_string[4]<<flush;
in.close();
- dosleep(1500);
return 0;
}
}
- cout<<"\x1B[17;1HLog file ended prematurely."<<endl;
+ cerr<<"Log file ended prematurely."<<endl;
return 1;
}