-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsolePlayer.cpp
More file actions
75 lines (66 loc) · 1.46 KB
/
consolePlayer.cpp
File metadata and controls
75 lines (66 loc) · 1.46 KB
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
#include<bits/stdc++.h>
using namespace std;
struct Cell {
int x, y;
Cell():x(0), y(0) { }
Cell(int x, int y):x(x), y(y) {}
};
const int DIM = 8;
char me;
string gr[DIM][DIM];
bool readFile() {
ifstream in;
in.open("shared_file.txt");
char now;
in >> now;
if (now!=me) {
in.close();
return false;
}
cout << "reading" << endl;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
string str;
in >> str;
cout << str << " ";
gr[i][j] = str;
}
cout << endl;
}
in.close();
cout << "read done" << endl;
return true;
}
void writeFile(Cell mv) {
cout << "move: " << mv.x << " " << mv.y << "\n";
ofstream out;
out.open("shared_file.txt");
out << 0 << "\n";
out << mv.x << " " << mv.y << "\n";
out.close();
}
bool isValidCell(const Cell& p) {
return 0 <= p.x && p.x < DIM && 0 <= p.y && p.y < DIM;
}
int main(int argc, char* argv[])
{
me = argv[1][0];
char other;
if (me=='R') other = 'G';
else other = 'R';
while (true) {
if (readFile()==false) continue;
cout << "Input x y: ";
Cell c;
while (true) {
cin >> c.x >> c.y;
if (isValidCell(c)==false || gr[c.x][c.y][0]==other) {
cout << "Invalid move!!" << endl;
} else {
break;
}
}
writeFile(c);
}
return 0;
}