forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10111 Find the Winning Move.cpp
165 lines (138 loc) · 4.24 KB
/
10111 Find the Winning Move.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
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
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int GridSize = 4;
const int NumElements = GridSize * GridSize;
const int UNFORCED = -1;
enum Owner {UNOWNED, X, O};
struct Board {
Owner grid[NumElements];
bool xTurn;
const Owner& operator[](int x) const
{
return grid[x];
}
Owner& operator[](int x)
{
return grid[x];
}
bool operator<(const Board& other) const {
for (int i = 0; i < NumElements; ++i)
if (grid[i] != other.grid[i])
return grid[i] < other.grid[i];
return false;
}
};
// This doesn't need to be cleared at all
map<Board, int> oForcedToLosePlay;
Owner Winner(const Board& board)
{
// Horizontal
for (int start = 0; start < NumElements; start += GridSize)
{
if (board[start] == UNOWNED)
continue;
bool same = true;
for (int i = 1; i < GridSize; ++i)
if (board[start + i] != board[start])
same = false;
if (same)
return board[start];
}
// Vertical
for (int start = 0; start < GridSize; ++start)
{
if (board[start] == UNOWNED)
continue;
bool same = true;
for (int i = GridSize; i < NumElements; i += GridSize)
if (board[start + i] != board[start])
same = false;
if (same)
return board[start];
}
// One diagonal
if (board[0] != UNOWNED && board[0] == board[5] && board[5] == board[10] && board[10] == board[15])
return board[0];
if (board[3] != UNOWNED && board[3] == board[6] && board[6] == board[9] && board[9] == board[12])
return board[3];
return UNOWNED;
}
int OForcedToLose(Board& board)
{
if (oForcedToLosePlay.find(board) == oForcedToLosePlay.end())
{
int forcedToLosePlayCurrent = UNFORCED;
// Check if there is a winner
Owner winner = Winner(board);
if (winner != UNOWNED)
{
forcedToLosePlayCurrent = (winner == X ? 0 : UNFORCED);
}
else
{
bool allForced = true;
for (int i = 0; i < NumElements; ++i)
{
if (board.grid[i] == UNOWNED)
{
board.grid[i] = (board.xTurn ? X : O);
board.xTurn = !board.xTurn;
int winPlay = OForcedToLose(board);
allForced &= (winPlay != UNFORCED);
board.grid[i] = UNOWNED;
board.xTurn = !board.xTurn;
if (board.xTurn && winPlay != UNFORCED)
{
forcedToLosePlayCurrent = i;
break;
}
if (!board.xTurn && !allForced)
break;
}
}
// o is only forced if all of the moves would result in it being forced
if (allForced && !board.xTurn)
forcedToLosePlayCurrent = 0;
}
return oForcedToLosePlay[board] = forcedToLosePlayCurrent;
}
return oForcedToLosePlay[board];
}
int main()
{
char id;
Board board;
// Since all will have X be first
board.xTurn = true;
while (cin >> id, id != '$')
{
for (int y = 0; y < GridSize; ++y)
{
string line;
cin >> line;
for (int x = 0; x < GridSize; ++x)
{
int index = y * GridSize + x;
switch (line[x])
{
case '.':
board.grid[index] = UNOWNED;
break;
case 'o':
board.grid[index] = O;
break;
case 'x':
board.grid[index] = X;
break;
}
}
}
int forcedPos = OForcedToLose(board);
if (forcedPos == UNFORCED)
cout << "#####\n";
else
cout << "(" << (forcedPos / GridSize) << ',' << (forcedPos % GridSize) << ")\n";
}
}