-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
287 lines (241 loc) · 7.76 KB
/
MainWindow.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Reference: https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-3-tic-tac-toe-ai-finding-optimal-move/ */
#include <QMessageBox>
#include <algorithm>
#include "MainWindow.h"
MainWindow::MainWindow(QMainWindow* parent)
: QMainWindow(parent), ui{std::make_unique<Ui::MainWindow>()}
{
ui->setupUi(this);
// Build Icons X and O from Ui::MainWindow form properties
QVariant o (QObject::property("O_icon"));
QVariant x (QObject::property("X_icon"));
o_icon = o.value<QIcon>();
x_icon = x.value<QIcon>();
// Statusbar
ui->statusbar->addPermanentWidget(&lab);
//this array will be used to map buttons to coordinates
btn_tbl = {nullptr, ui->pushButton_1, ui->pushButton_2, ui->pushButton_3,
ui->pushButton_4, ui->pushButton_5, ui->pushButton_6,
ui->pushButton_7, ui->pushButton_8, ui->pushButton_9};
}
bool MainWindow::check_win()
{
int score = evaluate();
if (score == -10) {
++win;
block_cells();
QMessageBox::information(this, "Congratulations", "You Win!!!");
return true;
}
else if (score == +10) {
++loose;
block_cells();
QMessageBox::warning(this, "Ouch!", "You Loose!!");
return true;
}
else if (!has_moves_left()) {
++draw;
block_cells();
QMessageBox::warning(this, "Ouch!", "DRAW!!!");
return true;
}
return false;
}
void MainWindow::buttonClicked()
{
// Your turn
auto button = static_cast<QPushButton*>(sender());
button->setEnabled(false);
button->setIcon(o_icon);
int number = button->property("number").toInt();
auto coord = get_coordinates(number);
board[coord.first][coord.second] = 'o';
if (check_win()) {
lab.setText("Win:"+QString::number(win)+
" Loose:"+QString::number(loose)+
" Draw:"+QString::number(draw));
return;
}
// Computer turn
auto best = find_best_move();
board[best.first][best.second] = 'x';
auto cell = best.first*3 + best.second + 1; // convert (x,y) coord to btn number
auto button2 = btn_tbl[cell];
button2->setEnabled(false);
button2->setIcon(x_icon);
if (check_win()) {
lab.setText("Win:"+QString::number(win)+
" Loose:"+QString::number(loose)+
" Draw:"+QString::number(draw));
}
}
inline void MainWindow::block_cells()
{
for (auto it=btn_tbl.begin()+1; it != btn_tbl.end(); ++it) {
(*it)->setEnabled(false);
}
}
inline std::pair<int,int> MainWindow::get_coordinates(int val)
{ // returns (row, col) coordinate of given cell
if (val < 1 || val > 9) throw std::runtime_error("");
return std::pair<int,int> ((val-1)/3, (val-1)%3);
}
inline bool MainWindow::has_moves_left()
{
for (auto& row: board)
for (auto& cell: row)
if (cell == ' ')
return true;
return false;
}
// This is the evaluation function
int MainWindow::evaluate()
{
// Checking for Rows for X or O victory.
for (auto& row: board) {
if (std::all_of(row.begin(), row.end(), [&row](auto& a){return a==*row.begin();})){
if (*row.begin() == 'x') {
return +10;
}
else if (*row.begin() == 'o') {
return -10;
}
}
}
// Checking for Columns for X or O victory.
for (decltype(board)::size_type col = 0; col<board.size(); ++col)
{
if (board[0][col]==board[1][col] &&
board[1][col]==board[2][col])
{
if (board[0][col]=='x')
return +10;
else if (board[0][col]=='o')
return -10;
}
}
// Checking for Diagonals for X or O victory.
if (board[0][0]==board[1][1] && board[1][1]==board[2][2])
{
if (board[0][0]=='x')
return +10;
else if (board[0][0]=='o')
return -10;
}
if (board[0][2]==board[1][1] && board[1][1]==board[2][0])
{
if (board[0][2]=='x')
return +10;
else if (board[0][2]=='o')
return -10;
}
// Else if none of them have won then return 0
return 0;
}
// This is the minimax function. It considers all
// the possible ways the game can go and returns
// the value of the board
int MainWindow::minimax(int depth, bool isMax)
{
int score = evaluate();
// If Maximizer has won the game return his/her
// evaluated score minus depth (to make it smarter)
if (score == 10)
return score - depth;
// If Minimizer has won the game return his/her
// evaluated score plus depth (to make it smarter)
if (score == -10)
return score + depth;
// If there are no more moves and no winner then
// it is a tie
if (!has_moves_left())
return 0;
// If this maximizer's move
if (isMax)
{
int best = -1000;
// Traverse all cells
for (decltype(board)::size_type i = 0; i<board.size(); ++i) {
for (decltype(i) j = 0; j<board[0].size(); ++j) {
// Check if cell is empty
if (board[i][j]==' ') {
// Make the move
board[i][j] = 'x';
// Call minimax recursively and choose
// the maximum value
best = std::max(best, minimax(depth+1, !isMax));
// Undo the move
board[i][j] = ' ';
}
}
}
return best;
}
// If this minimizer's move
else
{
int best = 1000;
// Traverse all cells
for (decltype(board)::size_type i = 0; i<board.size(); ++i) {
for (decltype(i) j = 0; j<board[0].size(); ++j) {
// Check if cell is empty
if (board[i][j]==' ') {
// Make the move
board[i][j] = 'o';
// Call minimax recursively and choose
// the minimum value
best = std::min(best, minimax(depth+1, !isMax));
// Undo the move
board[i][j] = ' ';
}
}
}
return best;
}
}
std::pair<int,int> MainWindow::find_best_move()
{
int bestVal = -1000, row = -1, col = -1;
// Traverse all cells, evalutae minimax function for
// all empty cells. And return the cell with optimal
// value.
for (decltype(board)::size_type i = 0; i<board.size(); ++i) {
for (decltype(i) j = 0; j<board[0].size(); ++j) {
// Check if celll is empty
if (board[i][j]==' ') {
// Make the move
board[i][j] = 'x';
// compute evaluation function for this
// move.
int moveVal = minimax(0, false);
// Undo the move
board[i][j] = ' ';
// If the value of the current move is
// more than the best value, then update
// best/
if (moveVal > bestVal) {
row = i;
col = j;
bestVal = moveVal;
}
}
}
}
return std::pair<int,int>(row, col);
}
void MainWindow::newgame()
{
for (auto& row: board)
for (auto& cell: row)
cell = ' ';
auto icon = QIcon();
for (auto it=btn_tbl.begin()+1; it != btn_tbl.end(); ++it) {
(*it)->setEnabled(true);
(*it)->setIcon(icon);
}
}
void MainWindow::about()
{
QMessageBox::information(this, "About",
"TicTacToe version 0.1\nAuthor: Fernando B. Giannasi\nmar/2018");
}