-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
367 lines (327 loc) · 11.8 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
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
#include "mainwindow.h"
#include <QApplication>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QEvent>
#include <qt_windows.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, game_mode(PVP)
, black_player_id(H1_Player)
{
qRegisterMetaType<Point>("Point");
this->initWindow();
this->initButton();
this->initConnections();
if(game_mode == PVE) {
this->resetPVEGame();
this->startPVEGame();
}
else if(game_mode == PVP) {
this->resetPVPGame();
this->startPVPGame();
}
}
MainWindow::~MainWindow()
{
}
/*----------------------------------------init---------------------------------*/
void MainWindow::initWindow() {
//set font
QFont font("Century Gothic", 8);
QApplication::setFont(font);
//set Window
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
this->setFixedSize(Window_Width, Window_Height);
this->setWindowTitle(QString("人工智能带带我"));
this->move(Window_Width / 2, Window_Height / 8);
this->setMouseTracking(true);
//set animation
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(250);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
}
void MainWindow::initButton() {
//init button
for(int i = 0; i < Button_Ctrl_num; i++) {
button_ctrl[i].setParent(this);
button_ctrl[i].setGeometry(Button_Ctrl_start_X + i * Button_Ctrl_D, Button_Ctrl_start_Y, Button_Ctrl_Width, Button_Ctrl_Height);
//button_ctrl[i].setFlat(true);
button_ctrl[i].setStyleSheet(QString("QPushButton{border:2px solid #000000"));
}
for(int i = 0; i < Button_Menu_num; i++) {
button_menu[i].setParent(this);
button_menu[i].setGeometry(Button_Menu_start_X + i * Button_Menu_D, Button_Menu_start_Y, Button_Menu_Width, Button_Menu_Height);
button_menu[i].setStyleSheet(QString("QPushButton{border:2px solid #000000"));
button_menu[i].setFlat(true);
}
//set button content
button_menu[Min_Button].setText("-");
button_menu[Close_Button].setText("x");
button_ctrl[New_Button].setText("新的一局");
button_ctrl[Mode_Button].setText("切换模式");
button_ctrl[Change_Button].setText("切换先手");
button_ctrl[Retract_Button].setText("悔棋");
for(int i = 0; i < 3; i++) {
message_text[i].setParent(this);
message_text[i].setGeometry(Button_Ctrl_start_X + (i + 1) * Button_Ctrl_D, Button_Ctrl_start_Y + 50, Button_Ctrl_Width + 50, Button_Ctrl_Height);
}
message_text[2].setText(QString("当前步数:%1").arg(gobang_cnt));
}
void MainWindow::initConnections() {
//set connections of menu buttons
connect(button_menu + Min_Button, &PushButton::clicked, this, &MainWindow::showMinimized);
connect(button_menu + Close_Button, &PushButton::clicked, this, &MainWindow::close);
//set connections of ctrl buttons
connect(button_ctrl + New_Button, &PushButton::clicked,
[&]() {
if(game_mode == PVE) {
if(now_player_id != AI_Player) {
this->resetPVEGame();
this->startPVEGame();
}
}
else if(game_mode == PVP) {
this->resetPVPGame();
this->startPVPGame();
}
});
connect(button_ctrl + Mode_Button, &PushButton::clicked,
[&]() {
if(now_player_id != AI_Player) {
if(game_mode == PVE) {
game_mode = PVP;
black_player_id = H1_Player;
this->resetPVPGame();
this->startPVPGame();
}
else if(game_mode == PVP) {
game_mode = PVE;
black_player_id = AI_Player;
this->resetPVEGame();
this->startPVEGame();
}
}
});
connect(button_ctrl + Change_Button, &PushButton::clicked,
[&]() {
if(game_mode == PVE) {
if(now_player_id == H1_Player) {
if(black_player_id == AI_Player) {
black_player_id = H1_Player;
}
else {
black_player_id = AI_Player;
}
this->resetPVEGame();
this->startPVEGame();
}
}
else if(game_mode == PVP) {
this->resetPVPGame();
this->startPVPGame();
}
});
connect(button_ctrl + Retract_Button, &PushButton::clicked,
[&]() {
if(game_mode == PVE) {
if(now_player_id != AI_Player)
this->retractPVEGame();
}
else if(game_mode == PVP) {
this->retractPVPGame();
}
});
}
/*----------------------------------------init---------------------------------*/
/*----------------------------------------game---------------------------------*/
void MainWindow::startPVEGame() {
message_text[0].setText(QString("当前模式:%1").arg(QString("人机")));
now_player_id = black_player_id;
gobang_board.setBlackPlayerID(black_player_id);
if(black_player_id == AI_Player) {
ai.set_id(true);
const Point &next_move = ai.ai_run();
this->aiMove(next_move);
message_text[1].setText(QString("当前先手:%1").arg(QString("电脑")));
}
else if(black_player_id == H1_Player) {
ai.set_id(false);
message_text[1].setText(QString("当前先手:%1").arg(QString("人")));
}
}
void MainWindow::startPVPGame() {
message_text[0].setText(QString("当前模式:%1").arg(QString("人人")));
message_text[1].setText(QString("当前先手:%1").arg(QString("人")));
now_player_id = black_player_id;
gobang_board.setBlackPlayerID(black_player_id);
}
void MainWindow::resetPVEGame() {
gobang_cnt = 0;
mouse_cursor = {init_flag, init_flag};
gobang_board.initboard();
ai.init();
ai.set_cnt(0);
this->update();
}
void MainWindow::resetPVPGame() {
gobang_cnt = 0;
mouse_cursor.x = mouse_cursor.y = init_flag;
gobang_board.initboard();
ai.init();
this->update();
}
void MainWindow::retractPVEGame() {
if(gobang_cnt >= 3) {
const Point& last_human_move = record_moves[gobang_cnt - 2];
const Point& last_ai_move = record_moves[gobang_cnt - 1];
ai.takePiece(last_human_move.x, last_human_move.y);
ai.takePiece(last_ai_move.x, last_ai_move.y);
gobang_board.takePiece(last_human_move.x, last_human_move.y);
gobang_board.takePiece(last_ai_move.x, last_ai_move.y);
this->update();
gobang_cnt -= 2;
ai.set_cnt(gobang_cnt);
}
else {
this->resetPVEGame();
this->startPVEGame();
}
}
void MainWindow::retractPVPGame() {
if(gobang_cnt > 2) {
const Point& last_H1_player_move = record_moves[gobang_cnt - 2];
const Point& last_H2_player_move = record_moves[gobang_cnt - 1];
gobang_board.takePiece(last_H1_player_move.x, last_H1_player_move.y);
gobang_board.takePiece(last_H2_player_move.x, last_H2_player_move.y);
gobang_cnt -= 2;
this->update();
}
else {
this->resetPVPGame();
this->startPVPGame();
}
}
void MainWindow::PVERound() {
this->PVEPutPiece(mouse_cursor, H1_Player);
this->update();
now_player_id = AI_Player;
this->repaint();
if(gobang_board.win(mouse_cursor, H1_Player)) {
QMessageBox::information(this, "", "玩家胜利啦!");
this->resetPVEGame();
this->startPVEGame();
}
else {
const Point &next_move = ai.ai_run();
this->aiMove(next_move);
}
}
void MainWindow::PVPRound() {
this->PVPPutPiece(mouse_cursor, now_player_id);
this->update();
if(now_player_id == H1_Player) {
if(gobang_board.win(mouse_cursor, now_player_id)) {
QMessageBox::information(this, "", "黑子赢");
this->resetPVPGame();
this->startPVPGame();
}
else
now_player_id = H2_Player;
}
else if(now_player_id == H2_Player) {
if(gobang_board.win(mouse_cursor, now_player_id)) {
QMessageBox::information(this, "", "白子赢");
this->resetPVPGame();
this->startPVPGame();
}
else
now_player_id = H1_Player;
}
}
void MainWindow::PVEPutPiece(const Point &next_move, const int id) {
if(gobang_board.isAvaliable(next_move)) {
gobang_board.putPiece(next_move.x, next_move.y, id);
ai.putPiece(next_move.x, next_move.y, id);
record_moves[gobang_cnt++] = next_move;
ai.set_cnt(gobang_cnt);
}
}
void MainWindow::PVPPutPiece(const Point &next_move, const int id) {
if(gobang_board.isAvaliable(next_move)) {
gobang_board.putPiece(next_move.x, next_move.y, id);
record_moves[gobang_cnt++] = next_move;
}
}
void MainWindow::aiMove(const Point& next_move) {
this->PVEPutPiece(next_move, AI_Player);
this->update();
if(gobang_board.win(next_move, AI_Player)) {
QMessageBox::information(this, "", "AI胜利啦!");
this->resetPVEGame();
this->startPVEGame();
}
else
now_player_id = H1_Player;
}
/*----------------------------------------game---------------------------------*/
/*-----------------------------------------ui----------------------------------*/
void MainWindow::paintEvent(QPaintEvent *event) {
DrawPainter painter(this);
painter.init();
painter.DrawBoard();
//draw all chess pieces
painter.DrawPieces(gobang_board);
//draw last move
if(gobang_cnt && gobang_board.isInside(record_moves[gobang_cnt - 1]))
painter.DrawLastMove(record_moves[gobang_cnt - 1], now_player_id == black_player_id);
message_text[2].setText(QString("当前步数:%1").arg(gobang_cnt));
}
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
int x = event->x(), y = event->y();
if(x >= Board_start_X && x <= Board_end_X && y >= Board_start_Y && y <= Board_end_Y) {
Point near_pos((x - Board_start_X) / Grid_Width, (y - Board_start_Y) / Grid_Width);
this->findNearPos(near_pos, x, y);
this->update();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
if(game_mode == PVE && gobang_board.isAvaliable(mouse_cursor) && this->now_player_id == H1_Player)
PVERound();
else if(game_mode == PVP && gobang_board.isAvaliable(mouse_cursor))
PVPRound();
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if(ReleaseCapture() && event->y() <= Button_Menu_D) {
QWidget *pWindow = this->window();
if(pWindow->isTopLevel())
SendMessage(HWND(pWindow->winId()),WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
void MainWindow::closeEvent(QCloseEvent *event) {
}
/*-----------------------------------------ui----------------------------------*/
/*-------------------------------------ui-function-----------------------------*/
int MainWindow::Distance(int x0, int y0, int x1, int y1) {
int dx = x0 - x1, dy = y0 -y1;
return (int)sqrt(dx * dx + dy * dy);
}
void MainWindow::findNearPos(const Point &near_pos, int x, int y) {
const int dx[] = {0, 1, 0, 1};
const int dy[] = {0, 0, 1, 1};
int x0 = Board_start_X + Grid_Width * near_pos.x, y0 = Board_start_Y + Grid_Width * near_pos.y;
mouse_cursor.x = mouse_cursor.y = init_flag;
int min_dis = Window_Height * Window_Height;
for(int i = 0; i < 4; i++) {
int xi = x0 + dx[i] * Grid_Width, yi = y0 + dy[i] * Grid_Width;
int now_dis = Distance(x, y, xi, yi);
if(Detect_Radius >= now_dis && min_dis > now_dis) {
mouse_cursor.x = near_pos.x + dx[i];
mouse_cursor.y = near_pos.y + dy[i];
min_dis = now_dis;
}
}
}
/*-------------------------------------ui-function-----------------------------*/