-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifier.cpp
More file actions
194 lines (156 loc) · 3.77 KB
/
verifier.cpp
File metadata and controls
194 lines (156 loc) · 3.77 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Verifier, do not modify
#include <iostream>
#include <vector>
#include <fstream>
const int SIZE = 10;
std::vector< std::vector< char > > board;
void displayboard(std::ostream& o)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
o << board[i][j] << " ";
}
o << std::endl;
}
o << "end" << std::endl;
};
bool Draw()
{
// all squares are full
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] == '_')
{
return false;
}
}
}
return true;
}
bool check_position(int i, int j)
{
// there must be a middle position
if (board[i][j] != 'X') return false;
if ( (i+1 < SIZE) && (board[i+1][j] == 'X') &&
(i+2 < SIZE) && (board[i+2][j] == 'X') &&
(i-1 >= 0) && (board[i-1][j] == 'X') &&
(i-2 >= 0) && (board[i-2][j] == 'X')
)
{
return true;
};
if ( (j+1 < SIZE) && (board[i][j+1] == 'X') &&
(j+2 < SIZE) && (board[i][j+2] == 'X') &&
(j-1 >= 0) && (board[i][j-1] == 'X') &&
(j-2 >= 0) && (board[i][j-2] == 'X')
)
{
return true;
};
if ( (i+1 < SIZE) && (j+1 < SIZE) && (board[i+1][j+1] == 'X') &&
(i+2 < SIZE) && (j+2 < SIZE) && (board[i+2][j+2] == 'X') &&
(i-1 >= 0) && (j-1 >= 0) && (board[i-1][j-1] == 'X') &&
(i-2 >= 0) && (j-2 >= 0) && (board[i-2][j-2] == 'X')
)
{
return true;
};
if ( (i+1 < SIZE) && (j-1 >= 0) && (board[i+1][j-1] == 'X') &&
(i+2 < SIZE) && (j-2 >= 0) && (board[i+2][j-2] == 'X') &&
(i-1 >= 0) && (j+1 < SIZE) && (board[i-1][j+1] == 'X') &&
(i-2 >= 0) && (j+2 < SIZE) && (board[i-2][j+2] == 'X')
)
{
return true;
};
return false;
}
// decide if anybody won
bool Xwon()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (check_position(i,j))
{
return true;
}
}
}
return false;
}
// decide if anybody won
void flipboard()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] == 'X')
{
board[i][j] = 'O';
}
else if (board[i][j] == 'O')
{
board[i][j] = 'X';
}
}
}
}
int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cout << "Use with 3 arguments: move infile outfile" << std::endl;
return SIZE;
}
int move = atoi(argv[1]);
std::ifstream in(argv[2]);
std::ofstream out(argv[3]);
// Load the board position
board.resize(SIZE);
for (int i = 0; i < SIZE; i++)
{
board[i].resize(SIZE);
for (int j = 0; j < SIZE; j++)
{
in >> board[i][j];
std::ws(in);
}
};
// make the move
if (board[move/SIZE][move % SIZE] == '_')
{
board[move/SIZE][move % SIZE] = 'X';
}
else
{
// he who makes an invalid move loses
std::cout << "0" << std::endl;
return 0;
}
// decide if anybody won
if (Xwon())
{
std::cout << "2" << std::endl;
return 0;
}
if (Draw())
{
std::cout << "1" << std::endl;
return 0;
}
// flip the board for the next player
flipboard();
// print it
displayboard(out);
in.close();
out.close();
std::cout << "5" << std::endl;
return 0;
}