-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylabel.cpp
542 lines (452 loc) · 12.1 KB
/
mylabel.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include "mylabel.h"
#include <vector>
#include <stack>
#include <utility>
myLabel::myLabel(QWidget* perant): QLabel(perant),judge(1)
{
// 设置 MyLabel 控件自动调整大小
setScaledContents(true);
player = new QSoundEffect(this);
}
myLabel::~myLabel()
{}
void myLabel::mousePressEvent(QMouseEvent *event)
{
playSound();
if(!connectToSQL(db))
{
return;
}
if(!createTable_r(username, db))
{
return;
}
int x = event->x() - 45;
int y = event->y() - 45;
qDebug() << x << y;
if(edgecheck(x, y))
{
s_col = qRound(x / static_cast<qreal>(gridSize));
s_row = qRound(y / static_cast<qreal>(gridSize));
qDebug() << "Grid position: " << s_row << ", " << s_col;
if(board[s_row][s_col] == 0)
{
if(judge == 1)
{
board[s_row][s_col] = blackstone;
isCaptured(s_row, s_col);
if(!isSafe(s_row, s_col))
{
board[s_row][s_col] = 0;
qDebug() << "you can't place there";
return;
}
judge = -1;
}
else
{
board[s_row][s_col] = whitestone;
isCaptured(s_row, s_col);
if(!isSafe(s_row, s_col))
{
board[s_row][s_col] = 0;
qDebug() << "you can't place there";
return;
}
judge = 1;
}
clicked = true;
insertIntoTable(s_row, s_col, board[s_row][s_col], c_id, 1, username, db);
}
update();
}
else
{
qDebug() << "out of edge!";
}
}
void myLabel::resizeEvent(QResizeEvent *event)
{
QLabel::resizeEvent(event);
}
//画棋盘
void myLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event); // 调用基类的 paintEvent 函数
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
int width = this->width() - space;
int height = this->height() - space;
painter.fillRect(rect(), QColorConstants::Svg::burlywood);
QPen pen(Qt::black); // 创建一个黑色画笔
pen.setWidth(2); // 设置画笔宽度为2像素
painter.setPen(pen); // 应用画笔设置
painter.drawRect(rect());
// 绘制水平线
for (int i = 0; i <= rows; i++)
{
painter.drawLine(space, i * gridSize, width, i * gridSize);
}
// 绘制垂直线
for (int j = 0; j <= cols; j++)
{
painter.drawLine(j * gridSize, space, j * gridSize, height);
}
// 绘制星位
painter.setBrush(Qt::black); // 设置画刷颜色为黑色
drawStarPoint(painter, 3, 3, gridSize); // 左上角
drawStarPoint(painter, 9, 3, gridSize); // 上边中间
drawStarPoint(painter, 15, 3, gridSize); // 右上角
drawStarPoint(painter, 3, 9, gridSize); // 左边中间
drawStarPoint(painter, 9, 9, gridSize); // 中心
drawStarPoint(painter, 15, 9, gridSize); // 右边中间
drawStarPoint(painter, 3, 15, gridSize); // 左下角
drawStarPoint(painter, 9, 15, gridSize); // 下边中间
drawStarPoint(painter, 15, 15, gridSize); // 右下角
if(clicked)
{
drawStones(s_row, s_col);
clicked = false;
}
// 根据棋盘状态绘制已下棋子
for (int i = 0; i < 19; ++i)
{
for (int j = 0; j < 19; ++j)
{
if (board[i][j] != 0)
{
drawStones(i, j);
}
}
}
}
//画星位
void myLabel::drawStarPoint(QPainter &painter, int row, int col, int gridSize)
{
int x = col * gridSize + space;
int y = row * gridSize + space;
int radius = gridSize / 15; // 星位的半径
painter.drawEllipse(QPoint(x, y), radius, radius);
}
//画棋子
void myLabel::drawStones(int row, int col)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
if(board[row][col] == blackstone)
{
painter.setBrush(Qt::black);
}
else
{
painter.setBrush(Qt::white);
}
int x = col * gridSize + 45;
int y = row * gridSize + 45;
int radius = gridSize / 2.5; // 棋子的半径
painter.drawEllipse(QPoint(x, y), radius, radius);
}
//边缘检查
bool myLabel::edgecheck(int x, int y)
{
int w = this->width() - 1.5 * space;
int h = this->height() - 1.5 * space;
if(x >= -space/2 && x <= w && y >= -space/2 && y <= h)
{
return true;
}
else
{
return false;
}
}
void myLabel::setUserID(int id, QString name)
{
c_id = id;
username = name;
qDebug() << c_id << " " << username;
}
void myLabel::setPass()
{
judge = -judge;
}
bool myLabel::isValid(int x, int y)
{
return x >= 0 && x < 19 && y >= 0 && y < 19;
}
int myLabel::countLiberty(int x, int y, std::vector<std::vector<bool>>& visited)
{
std::vector<std::pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
std::stack<std::pair<int, int>> s;
s.push({x, y});
int liberties = 0;
int stoneColor = board[x][y];
std::vector<std::pair<int, int>> connected;
while (!s.empty())
{
auto [cx, cy] = s.top();
s.pop();
if (!isValid(cx, cy) || visited[cx][cy])
{
continue;
}
visited[cx][cy] = true;
connected.push_back({cx, cy});
for (auto [dx, dy] : directions)
{
int nx = cx + dx;
int ny = cy + dy;
if (isValid(nx, ny))
{
if (board[nx][ny] == 0)
{
liberties++;
}
else if (board[nx][ny] == stoneColor && !visited[nx][ny])
{
s.push({nx, ny});
}
}
}
}
// 如果没有气,则标记为死子
if (liberties == 0)
{
playCaptureSound();
for (auto [cx, cy] : connected)
{
insertIntoTable(cx, cy, board[cx][cy], c_id, 0, username, db);
board[cx][cy] = 0;
}
}
return liberties;
}
bool myLabel::countLiberty_s(int x, int y, std::vector<std::vector<bool>>& visited)
{
std::vector<std::pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
std::stack<std::pair<int, int>> s;
s.push({x, y});
int stoneColor = board[x][y];
std::vector<std::pair<int, int>> connected;
while (!s.empty())
{
auto [cx, cy] = s.top();
s.pop();
if (!isValid(cx, cy) || visited[cx][cy])
{
continue;
}
visited[cx][cy] = true;
connected.push_back({cx, cy});
for (auto [dx, dy] : directions)
{
int nx = cx + dx;
int ny = cy + dy;
if (isValid(nx, ny))
{
if (board[nx][ny] == 0)
{
return true;
}
else if (board[nx][ny] == stoneColor && !visited[nx][ny])
{
s.push({nx, ny});
}
}
}
}
return false;
}
void myLabel::isCaptured(int x, int y)
{
std::vector<std::pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
std::vector<std::vector<bool>> visited(19, std::vector<bool>(19, false));
for( auto [dx, dy] : directions)
{
int nx = x + dx;
int ny = y + dy;
if (board[nx][ny] != 0 && isValid(nx, ny) && !visited[nx][ny] && board[nx][ny] != board[x][y])
{
countLiberty(nx, ny, visited);
}
}
}
bool myLabel::isSafe(int x, int y)
{
std::vector<std::vector<bool>> visited(19, std::vector<bool>(19, false));
if(!countLiberty_s(x, y, visited))
{
return false;
}
return true;
}
void myLabel::initialization()
{
for(int i = 0; i <19; i++)
{
for(int j = 0; j < 19; j++)
{
board[i][j] = 0;
}
}
judge = 1;
update();
}
void myLabel::setRegret()
{
if(board[s_row][s_col] != 0)
{
board[s_row][s_col] = 0;
judge = -judge;
insertIntoTable(s_row, s_col, board[s_row][s_col], c_id, 0, username, db);
}
update();
}
void myLabel::setFinish()
{
int black_t = 0;
int white_t = 0;
countTerritory(black_t, white_t);
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle("WINNER");
if(black_t - white_t >= 7)
{
msgBox.setText(QString("Winner: Black!\nBlack territory: %1").arg(black_t));
updateWinner(c_id, "black", username, db);
}
else
{
msgBox.setText(QString("Winner: White!\nWhite territory: %1").arg(white_t));
updateWinner(c_id, "white", username, db);
}
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
db.close();
}
void myLabel::countTerritory(int &black_t, int &white_t)
{
std::stack<std::pair<int, int>> s;
for(int i = 0; i < 19; i++)
{
int count = 0;
for(int j = 0; j <19; j++)
{
count++;
if(board[i][j] != 0)
{
if(s.empty())
{
if(board[i][j] == blackstone)
{
black_t = black_t + count;
count = 0;
}
else if(board[i][j] == whitestone)
{
white_t = white_t + count;
count = 0;
}
}
else
{
auto [cx, cy] = s.top();
if(board[i][j] == board[cx][cy])
{
if(board[i][j] == blackstone)
{
black_t = black_t + count;
count = 0;
}
else if(board[i][j] == whitestone)
{
white_t = white_t + count;
count = 0;
}
}
}
s.push({i, j});
}
}
}
}
void myLabel::loadReplayData(const QString &tableName, int c_id)
{
if (!connectToSQL(db))
{
return;
}
replayData.clear();
QSqlQuery query(db);
qDebug() << c_id;
query.prepare("SELECT x, y, color, alive FROM " + tableName + " WHERE c_id = :c_id ORDER BY id ASC");
query.bindValue(":c_id", c_id);
if (query.exec())
{
while (query.next())
{
QVector<int> move(4);
move[0] = query.value(0).toInt(); // x
move[1] = query.value(1).toInt(); // y
move[2] = query.value(2).toInt(); // color
move[3] = query.value(3).toInt(); // alive
replayData.append(move);
}
}
else
{
qDebug() << "Failed to load replay data: " << query.lastError();
}
currentStep = 0;
updateBoardToStep(currentStep);
}
void myLabel::updateBoardToStep(int step)
{
initialization(); // 重置棋盘
for (int i = 0; i <= step && i < replayData.size(); ++i)
{
int x = replayData[i][0];
int y = replayData[i][1];
int color = replayData[i][2];
if (replayData[i][3] == 0)
{
color = 0;
}
board[x][y] = color;
}
update(); // 更新显示
}
void myLabel::forward()
{
if (currentStep < replayData.size() - 1)
{
++currentStep;
updateBoardToStep(currentStep);
}
}
void myLabel::backward()
{
if (currentStep > 0)
{
--currentStep;
updateBoardToStep(currentStep);
}
}
void myLabel::setReplayData(const QVector<QVector<int>> &data)
{
replayData = data;
currentStep = 0;
updateBoardToStep(currentStep);
}
void myLabel::playSound()
{
player->setSource(QUrl::fromLocalFile("C:/Users/steph/Desktop/围棋落子.wav"));
player->setVolume(50);
player->play();
}
void myLabel::playCaptureSound()
{
player->setSource(QUrl::fromLocalFile("C:/Users/steph/Desktop/好.wav"));
player->setVolume(50);
player->play();
}