-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
86 lines (82 loc) · 2.89 KB
/
Copy pathtictactoe.cpp
File metadata and controls
86 lines (82 loc) · 2.89 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
#include <qpainter.h>
#include <qlist.h>
#include <cstdlib>
#include "tictactoe.h"
#define DATA(point) data.pos[point.y()][point.x()]
const QColor TicTacToe::playerColors[2] = {red, blue};
void TicTacToe::paintPoint(QPainter *painter, const QRect& rect, const int player)
{
int ds = rect.width() / 5;
if (player == O) {
painter->drawEllipse(rect);
painter->setPen(NoPen);
painter->setBrush(COLOR_BOARD);
painter->drawEllipse(QRect(rect.topLeft() + QPoint(ds, ds), rect.size() - QSize(ds, ds) * 2));
} else if (player == X) {
int x = rect.x(), y = rect.y(), s = rect.width();
ds = (int)((float)ds / 1.5);
QPointArray points;
// 3 points per line: Point 1 Point 2 Point 3
points.putPoints(0, 3, x + ds, y, x + s / 2, y + s / 2 - ds, x + s - ds, y);
points.putPoints(3, 3, x + s, y + ds, x + s / 2 + ds, y + s / 2, x + s, y + s - ds);
points.putPoints(6, 3, x + s - ds, y + s, x + s / 2, y + s / 2 + ds, x + ds, y + s);
points.putPoints(9, 3, x, y + s - ds, x + s / 2 - ds, y + s / 2, x, y + ds);
painter->drawPolygon(points);
} else
BoardGame::paintPoint(painter, rect, player);
}
QPoint TicTacToe::ai(const int player)
{
int l = winLength(), res[l][l];
for (int r = 0; r < l; r++)
for (int c = 0; c < l; c++) {
res[r][c] = -1;
if (data.pos[r][c] != -1)
continue;
res[r][c] = 0;
QPoint pos(c, r), direction(1, 0);
if (count(pos, direction, player) + count(pos, -direction, player) == l - 1)
return pos;
if (count(pos, direction, !player) + count(pos, -direction, !player) == l - 1)
res[r][c] += 100;
direction = QPoint(1, 1);
if (count(pos, direction, player) + count(pos, -direction, player) == l - 1)
return pos;
if (count(pos, direction, !player) + count(pos, -direction, !player) == l - 1)
res[r][c] += 100;
direction = QPoint(0, 1);
if (count(pos, direction, player) + count(pos, -direction, player) == l - 1)
return pos;
if (count(pos, direction, !player) + count(pos, -direction, !player) == l - 1)
res[r][c] += 100;
direction = QPoint(-1, 1);
if (count(pos, direction, player) + count(pos, -direction, player) == l - 1)
return pos;
if (count(pos, direction, !player) + count(pos, -direction, !player) == l - 1)
res[r][c] += 100;
/*if (r == size().height() / 2 && c == size().width() / 2)
res[r][c] += 50;*/
}
QPoint pos(-1, -1);
QList<QPoint> posList;
posList.setAutoDelete(true);
for (int r = 0; r < l; r++)
for (int c = 0; c < l; c++) {
if (res[r][c] == -1)
continue;
posList.append(new QPoint(c, r));
if (r == size().height() / 2 && c == size().width() / 2)
for (int i = 0; i < 5; i++)
posList.append(new QPoint(c, r));
if (pos.x() == -1) {
pos = QPoint(c, r);
continue;
}
if (res[pos.y()][pos.x()] > res[r][c])
continue;
pos = QPoint(c, r);
}
if (pos.x() == -1 || res[pos.y()][pos.x()] == 0)
pos = *posList.at(rand() % posList.count());
return pos;
}