-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboardgame.cpp
More file actions
245 lines (227 loc) · 6.57 KB
/
Copy pathboardgame.cpp
File metadata and controls
245 lines (227 loc) · 6.57 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
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
#include <qpainter.h>
#include <qfont.h>
#include "boardgame.h"
#define DATA(point) data.pos[(point).y()][(point).x()]
const QColor BoardGame::playerColors[2] = {black, white};
void BoardGame::refresh(QPixmap *cache)
{
if (gameName() == BoardGame::gameName()) {
QPainter painter(cache);
painter.setPen(NoPen);
painter.setBrush(QBrush(white));
painter.drawRect(cache->rect());
painter.setPen(QPen(COLOR_LINE, 10));
painter.setBrush(QBrush(COLOR_BOARD));
painter.drawEllipse(cache->rect());
QFont f("noah", 24);
painter.setFont(f);
painter.drawText(cache->rect(), AlignCenter, tr("Board Games!"));
return;
}
/* generalRefresh(cache);
}
void BoardGame::generalRefresh(QPixmap *cache)
{*/
QPainter painter(cache);
drawBackground(cache, &painter);
switch (boardType()) {
case CenterLine:
drawBoardLines(cache, &painter);
case NoLine:
break;
case EdgeLine:
drawEdgeLines(cache, &painter);
}
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++)
drawPoint(cache, &painter, QPoint(c, r), data.pos[r][c]);
if (data.last.x() != -1)
drawLast(cache, &painter, data.last, DATA(data.last));
}
void BoardGame::drawBackground(QPixmap *cache, QPainter *painter)
{
painter->setPen(NoPen);
painter->setBrush(QBrush(COLOR_BOARD));
painter->drawRect(cache->rect());
}
void BoardGame::drawBoardLines(QPixmap *cache, QPainter *painter)
{
float w = cache->width(), h = cache->height();
float dw, dh;
float us;
if (square()) {
float s = w < h ? w : h;
dw = (w - s) / 2;
dh = (h - s) / 2;
us = (s / size().width());
if (boardType() != CenterLine)
us = (int)us;
w = s;
h = s;
} else {
us = ((w / size().width() < h / size().height() ? w / size().width() : h / size().height()));
if (boardType() != CenterLine)
us = (int)us;
dw = (w - us * size().width()) / 2;
dh = (h - us * size().height()) / 2;
w = us * size().width();
h = us * size().height();
}
painter->setPen(COLOR_LINE);
painter->setBrush(NoBrush);
for (int r = 0; r < size().height(); r++)
painter->drawLine((int)(dw + us / 2), (int)(dh + us / 2 + us * r), \
(int)(dw + us / 2 + w - us), (int)(dh + us / 2 + us * r));
for (int c = 0; c < size().height(); c++)
painter->drawLine((int)(dw + us / 2 + us * c), (int)(dh + us / 2), \
(int)(dw + us / 2 + us * c), (int)(dh + us / 2 + h - us));
}
void BoardGame::drawEdgeLines(QPixmap *cache, QPainter *painter)
{
painter->setPen(COLOR_LINE);
painter->setBrush(NoBrush);
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++)
painter->drawRect(pointRect(cache->size(), QPoint(c, r)));
}
QRect BoardGame::pointRect(const QSize& disp, const QPoint& point) const
{
float w = disp.width(), h = disp.height();
float dw, dh;
float us;
if (square()) {
float s = w < h ? w : h;
dw = (w - s) / 2;
dh = (h - s) / 2;
us = (s / size().width());
if (boardType() != CenterLine)
us = (int)us;
w = s;
h = s;
} else {
us = ((w / size().width() < h / size().height() ? w / size().width() : h / size().height()));
if (boardType() != CenterLine)
us = (int)us;
dw = (w - us * size().width()) / 2;
dh = (h - us * size().height()) / 2;
w = us * size().width();
h = us * size().height();
}
int r = point.y(), c = point.x();
return QRect((int)(dw + us * c), (int)(dh + us * r), (int)us, (int)us);
}
QPoint BoardGame::findPoint(const QSize& disp, const QPoint& pos)
{
float w = disp.width(), h = disp.height();
float dw, dh;
float us;
if (square()) {
float s = w < h ? w : h;
dw = (w - s) / 2;
dh = (h - s) / 2;
us = (s / size().width());
if (boardType() != CenterLine)
us = (int)us;
w = s;
h = s;
} else {
us = ((w / size().width() < h / size().height() ? w / size().width() : h / size().height()));
if (boardType() != CenterLine)
us = (int)us;
dw = (w - us * size().width()) / 2;
dh = (h - us * size().height()) / 2;
w = us * size().width();
h = us * size().height();
}
QPoint p((int)(pos.x() - dw), (int)(pos.y() - dh));
if (p.x() < 0 || p.y() < 0)
return QPoint(-1, -1);
p = QPoint((int)(p.x() / us), (int)(p.y() / us));
if (p.x() >= size().width() || p.y() >= size().height())
return QPoint(-1, -1);
return p;
}
void BoardGame::drawPoint(QPixmap *cache, QPainter *painter, const QPoint& point, const int player)
{
if (player == -1)
return;
painter->setPen(NoPen);
painter->setBrush(QBrush(playerColor(player)));
paintPoint(painter, pointRect(cache->size(), point), player);
}
void BoardGame::drawLast(QPixmap *cache, QPainter *painter, const QPoint& point, const int player)
{
painter->setPen(QPen(playerColor(player), 0, DashLine));
painter->setBrush(NoBrush);
painter->drawRect(pointRect(cache->size(), point));
}
void BoardGame::drawHover(QPixmap *hover, QPainter *painter, const int player)
{
drawBackground(hover, painter);
painter->setPen(NoPen);
painter->setBrush(QBrush(playerColor(player), Dense4Pattern));
paintPoint(painter, hover->rect(), player);
}
void BoardGame::hoverPixmap(QPixmap *cache, QPixmap *hover, const int player)
{
QRect rect = pointRect(cache->size(), QPoint(0, 0));
hover->resize(rect.size());
QPainter painter(hover);
drawHover(hover, &painter, player);
if (boardType() != EdgeLine)
return;
painter.setPen(COLOR_LINE);
painter.setBrush(NoBrush);
painter.drawRect(hover->rect());
}
void BoardGame::reset(void)
{
data.last = QPoint(-1, -1);
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++)
data.pos[r][c] = -1;
}
int BoardGame::count(const QPoint& point, const QPoint& direction, const int player) const
{
int c;
QPoint p(point);
for (c = 0; pointValid(p) && DATA(p) == player; p += direction, c++);
return c;
}
int BoardGame::win(void)
{
bool space = false;
for (int r = 0; r < size().height(); r++)
for (int c = 0; c < size().width(); c++) {
if (data.pos[r][c] == -1) {
space = true;
continue;
}
if (count(QPoint(c, r), QPoint(1, 0), data.pos[r][c]) >= winLength())
return data.pos[r][c];
if (count(QPoint(c, r), QPoint(1, 1), data.pos[r][c]) >= winLength())
return data.pos[r][c];
if (count(QPoint(c, r), QPoint(0, 1), data.pos[r][c]) >= winLength())
return data.pos[r][c];
if (count(QPoint(c, r), QPoint(-1, 1), data.pos[r][c]) >= winLength())
return data.pos[r][c];
}
if (space)
return -1;
return -2;
}
bool BoardGame::place(const QPoint& point, const int player)
{
if (DATA(point) != -1)
return false;
DATA(point) = player;
data.last = point;
return true;
}
QString BoardGame::winMessage(const int player, const QString msg) const
{
if (player == -2)
return msg;
else
return tr("Player %1: %2 won!").arg(player + 1).arg(msg);
}