-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.cpp
97 lines (76 loc) · 1.96 KB
/
play.cpp
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
#include "player.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::string;
#define MAX_ROUNDS 1000
int main(int argc, char **argv){
string board_name;
Player p1(1);
Player p2(2);
Player v(1);
int round = 1; // 1 or 2
v.init_board("boards/board_1.txt");
v.load_board("boards/board_1.txt");
while(true){
if(round > MAX_ROUNDS){
cout << "Reached max rounds";
break;
}
cout << "Playing as player 1" << endl;
board_name = "boards/board_" + std::to_string(round) + ".txt";
cout << "Reading board " << board_name << endl;
try{
p1.load_board(board_name);
std::this_thread::sleep_for (std::chrono::milliseconds(100));
p1.move();
board_name = "boards/board_" + std::to_string(++round) + ".txt";
p1.store_board(board_name);
if(p1.wins()){
cout << "Player 1 wins\n" << endl;
break;
}
}catch(player_exception e){
cout << e.msg << endl;
}
try{
v.load_board(board_name);
if(!v.valid_move()){
cout << "Player 1 loses for invalid move\n";
break;
}
}catch(player_exception e){
cout << e.msg << endl;
}
cout << "Playing as player 2" << endl;
board_name = "boards/board_" + std::to_string(round) + ".txt";
cout << "Reading board " << board_name << endl;
try{
p2.load_board(board_name);
std::this_thread::sleep_for (std::chrono::milliseconds(100));
p2.move();
board_name = "boards/board_" + std::to_string(++round) + ".txt";
p2.store_board(board_name);
if(p2.wins()){
cout << "Player 2 wins\n" << endl;
break;
}
}catch(player_exception e){
cout << e.msg << endl;
}
try{
v.load_board(board_name);
if(!v.valid_move()){
cout << "Player 2 loses for invalid move\n";
break;
}
}catch(player_exception e){
cout << e.msg << endl;
}
}
}