-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameS8.cpp
434 lines (396 loc) · 14.3 KB
/
gameS8.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
#include "gameS8.h"
#include "ui_gameS8.h"
gameS8::gameS8(QWidget* parent) :
QWidget(parent),
ui(new Ui::gameS8),
N(8), //确定棋盘阶数
cb(8) //建立棋盘
{
ui->setupUi(this);
setFixedSize(1280, 720);
pbtn[0][0] = ui->pBtn0;
pbtn[0][1] = ui->pBtn1;
pbtn[0][2] = ui->pBtn2;
pbtn[0][3] = ui->pBtn3;
pbtn[0][4] = ui->pBtn4;
pbtn[0][5] = ui->pBtn5;
pbtn[0][6] = ui->pBtn6;
pbtn[0][7] = ui->pBtn7;
pbtn[1][0] = ui->pBtn8;
pbtn[1][1] = ui->pBtn9;
pbtn[1][2] = ui->pBtn10;
pbtn[1][3] = ui->pBtn11;
pbtn[1][4] = ui->pBtn12;
pbtn[1][5] = ui->pBtn13;
pbtn[1][6] = ui->pBtn14;
pbtn[1][7] = ui->pBtn15;
pbtn[2][0] = ui->pBtn16;
pbtn[2][1] = ui->pBtn17;
pbtn[2][2] = ui->pBtn18;
pbtn[2][3] = ui->pBtn19;
pbtn[2][4] = ui->pBtn20;
pbtn[2][5] = ui->pBtn21;
pbtn[2][6] = ui->pBtn22;
pbtn[2][7] = ui->pBtn23;
pbtn[3][0] = ui->pBtn24;
pbtn[3][1] = ui->pBtn25;
pbtn[3][2] = ui->pBtn26;
pbtn[3][3] = ui->pBtn27;
pbtn[3][4] = ui->pBtn28;
pbtn[3][5] = ui->pBtn29;
pbtn[3][6] = ui->pBtn30;
pbtn[3][7] = ui->pBtn31;
pbtn[4][0] = ui->pBtn32;
pbtn[4][1] = ui->pBtn33;
pbtn[4][2] = ui->pBtn34;
pbtn[4][3] = ui->pBtn35;
pbtn[4][4] = ui->pBtn36;
pbtn[4][5] = ui->pBtn37;
pbtn[4][6] = ui->pBtn38;
pbtn[4][7] = ui->pBtn39;
pbtn[5][0] = ui->pBtn40;
pbtn[5][1] = ui->pBtn41;
pbtn[5][2] = ui->pBtn42;
pbtn[5][3] = ui->pBtn43;
pbtn[5][4] = ui->pBtn44;
pbtn[5][5] = ui->pBtn45;
pbtn[5][6] = ui->pBtn46;
pbtn[5][7] = ui->pBtn47;
pbtn[6][0] = ui->pBtn48;
pbtn[6][1] = ui->pBtn49;
pbtn[6][2] = ui->pBtn50;
pbtn[6][3] = ui->pBtn51;
pbtn[6][4] = ui->pBtn52;
pbtn[6][5] = ui->pBtn53;
pbtn[6][6] = ui->pBtn54;
pbtn[6][7] = ui->pBtn55;
pbtn[7][0] = ui->pBtn56;
pbtn[7][1] = ui->pBtn57;
pbtn[7][2] = ui->pBtn58;
pbtn[7][3] = ui->pBtn59;
pbtn[7][4] = ui->pBtn60;
pbtn[7][5] = ui->pBtn61;
pbtn[7][6] = ui->pBtn62;
pbtn[7][7] = ui->pBtn63;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
connect(pbtn[i][j], SIGNAL(clicked()), this, SLOT(hammer()));
connect(&tt, &TimeThread::addTime, this, &gameS8::updateTime);
reset();
}
gameS8::~gameS8()
{
delete ui;
}
void gameS8::setAccount(int i) //初始化界面
{
account = i;
reset();
update();
}
void gameS8::continueGame(int i, Game g) //继续之前的游戏
{
account = i;
reset();
lstscore = g.getScore();
attempts = g.getAttempts();
time = g.getCountup();
score = g.getScore();
cb = g.last;
gaming = true;
update();
tt.start();
ui->pBtnPause->setEnabled(true);
ui->pBtnEnd->setEnabled(true);
}
void gameS8::keyPressEvent(QKeyEvent* e)
{
if (dead) return;
if (ui->gbxCheckerboard->isEnabled()) return;
if (attempts && !gaming) return; //游戏暂停,阻止操作
//记录下每步操作
step.push_back(make_pair(cb.getBoard(), score));
ll t = -1;
switch(e->key()) {
case Qt::Key_A :
case Qt::Key_Left : t = cb.left(); break;
case Qt::Key_D :
case Qt::Key_Right : t = cb.right(); break;
case Qt::Key_W :
case Qt::Key_Up : t = cb.up(); break;
case Qt::Key_S :
case Qt::Key_Down : t = cb.down(); break;
default : step.pop_back(); return; //无效按键操作,把存下的棋盘删了
}
if (t != -1) {
attempts++;
score += t;
}
else {
step.pop_back();
return;
}
if (step.size() > MAX_STEP)
step.pop_front();
if (!gaming) { //第一次开始游戏
gaming = true;
tt.start();
ui->pBtnPause->setEnabled(true);
ui->pBtnEnd->setEnabled(true);
}
update(); //先更新一下棋盘
if (!cb.judge()) { //判断是否死亡,并询问是否需要复活
tt.terminate();
if (reviveNum && QMessageBox::question(this, tr("2048"), tr(("是否花费500金币删除所有最小数字\n\n一局内最多使用1次(" + intToString(2 - reviveNum) + "/1)").c_str()),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认复活
if (User::getCoins(account) < 500) { //金币不够
QMessageBox::critical(this, tr("2048"), tr("金币不足!\n请通过游戏获取金币 或 输入礼包兑换码兑换金币"));
dead = true;
gaming = false;
}
else { //复活并继续游戏
tt.start();
cb.delMin();
attempts++;
reviveNum--;
User::setCoins(account, User::getCoins(account) - 500);
}
}
else {
dead = true;
gaming = false;
}
update(); //第二次更新棋盘(复活 或 死亡)
}
if (dead) die();
}
void gameS8::closeEvent(QCloseEvent* event)
{
if (!attempts || dead) {
event->accept();
}
else {
tt.terminate();
if (QMessageBox::warning(this, tr("2048"), tr("是否退出游戏\n(退出游戏不会保存游戏的任何信息)"),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认退出
event->accept();
}
else {
event->ignore();
systemclose = true;
tt.start();
return;
}
}
reset();
account = -1;
emit returnHome(x(), y());
}
void gameS8::on_pBtnRetry_clicked() //重新开始
{
if (!attempts || dead) {
reset();
update();
}
else {
tt.terminate();
if (QMessageBox::warning(this, tr("2048"), tr("是否重新开始游戏\n(重新开始游戏不会保存游戏的任何信息)"),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认重新开始
reset();
update();
}
else tt.start();
}
}
void gameS8::on_pBtnRetry2_clicked() //重新开始(游戏失败后)
{
reset();
update();
}
void gameS8::on_pBtnPause_clicked() //暂停
{
if (gaming) { //正在游戏中,暂停
gaming = false;
tt.terminate();
ui->pBtnPause->setStyleSheet("QPushButton\n{\n border-image: url(:/interface/images/start.png);\n background-color: rgb(251, 248, 239);\n border-radius: 5px;\n}\n\nQPushButton:hover\n{\n background-color: rgb(219, 210, 200);\n}\n\nQPushButton:pressed\n{\n background-color: rgb(187, 173, 160);\n padding-left: 3px;\n padding-top: 3px;\n}");
}
else { //恢复游戏
gaming = true;
tt.start();
ui->pBtnPause->setStyleSheet("QPushButton\n{\n border-image: url(:/interface/images/pause.png);\n background-color: rgb(251, 248, 239);\n border-radius: 5px;\n}\n\nQPushButton:hover\n{\n background-color: rgb(219, 210, 200);\n}\n\nQPushButton:pressed\n{\n background-color: rgb(187, 173, 160);\n padding-left: 3px;\n padding-top: 3px;\n}");
}
update();
}
void gameS8::on_pBtnEnd_clicked() //结束游戏
{
tt.terminate();
if (QMessageBox::warning(this, tr("2048"), tr("是否结束本局游戏"),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认重新开始
dead = true;
gaming = false;
tt.terminate();
die();
}
else tt.start();
}
void gameS8::on_pBtnLast_clicked() //退回上一步
{
tt.terminate();
if (QMessageBox::question(this, tr("2048"), tr(("是否花费300金币退回上一步\n\n一局内最多使用3次(" + intToString(4 - lastNum) + "/3)").c_str()),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认退回上一步
if (User::getCoins(account) < 300) //金币不够
QMessageBox::critical(this, tr("2048"), tr("金币不足!\n请通过游戏获取金币 或 输入礼包兑换码兑换金币"));
else {
User::setCoins(account, User::getCoins(account) - 300);
cb.setBoard(step.back().first);
score = step.back().second;
step.pop_back();
attempts++;
lastNum--;
update();
}
}
tt.start();
}
void gameS8::on_pBtnHammer_clicked() //敲除一个方块
{
if (ui->gbxCheckerboard->isEnabled()) {
update();
return;
}
tt.terminate();
if (QMessageBox::question(this, tr("2048"), tr(("是否花费300金币敲除一个方块\n\n一局内最多使用3次(" + intToString(4 - hammerNum) + "/3)").c_str()),
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) == QMessageBox::Ok) { //确认退回上一步
if (User::getCoins(account) < 300) //金币不够
QMessageBox::critical(this, tr("2048"), tr("金币不足!\n请通过游戏获取金币 或 输入礼包兑换码兑换金币"));
else {
ui->gbxCheckerboard->setEnabled(true);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (cb.get(i,j) != 0)
pbtn[i][j]->setEnabled(true);
}
}
tt.start();
}
void gameS8::hammer() //敲除方块
{
QPushButton* obj = dynamic_cast<QPushButton*>(sender());
int r = -1, c = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (obj == pbtn[i][j])
r = i, c = j;
User::setCoins(account,User::getCoins(account) - 300);
attempts++;
step.push_back(make_pair(cb.getBoard(), score));
cb.set(r, c, 0);
hammerNum--;
update();
}
void gameS8::on_pBtnHome_clicked() //从游戏界面返回用户主界面
{
systemclose = false;
this->close();
}
void gameS8::on_pBtnHome2_clicked() //从游戏界面返回用户主界面(游戏失败后)
{
systemclose = false;
this->close();
}
void gameS8::reset()
{
lastNum = 3;
hammerNum = 3;
reviveNum = 1;
//重置游戏结束的界面
ui->gbxOver->hide();
ui->lblScore->setText("1234567890");
ui->lblRecord->setText("new record");
ui->lblAddCoins->setText("add coins");
//重置游戏界面
ui->lblAttempts->setText("attempts");
ui->lblAttempts->setToolTip("操作次数");
time.reset();
gaming = false;
tt.terminate();
ui->lblTime->setText("time");
ui->lblBest->setText("best");
ui->lblBest->setToolTip("最佳记录");
ui->lblCoins->setText("coins");
ui->lblCoins->setToolTip("金币数量");
ui->lblCur->setText("current");
ui->lblCur->setToolTip("");
ui->gbxCheckerboard->setEnabled(false);
ui->pBtnHammer->setEnabled(false);
ui->pBtnLast->setEnabled(false);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
pbtn[i][j]->setText("");
pbtn[i][j]->setFont(QFont("NumberOnly", sizeStyle[N - 3][0]));
pbtn[i][j]->setStyleSheet(QString::fromLocal8Bit(colorStyle[0].data()));
}
attempts = 0;
lstscore = score = 0;
cb.reset();
step.clear();
dead = false;
ui->pBtnPause->setEnabled(false);
ui->pBtnPause->setStyleSheet("QPushButton\n{\n border-image: url(:/interface/images/pause.png);\n background-color: rgb(251, 248, 239);\n border-radius: 5px;\n}\n\nQPushButton:hover\n{\n background-color: rgb(219, 210, 200);\n}\n\nQPushButton:pressed\n{\n background-color: rgb(187, 173, 160);\n padding-left: 3px;\n padding-top: 3px;\n}");
ui->pBtnEnd->setEnabled(false);
}
void gameS8::update()
{
ui->lblBest->setText(intToQString(User::getBest(account, N)));
ui->lblBest->setToolTip("最佳记录:" + longToQString(User::getBest(account, N)));
ui->lblCoins->setText(intToQString(User::getCoins(account)));
ui->lblCoins->setToolTip("金币数量:" + longToQString(User::getCoins(account)));
ui->lblAttempts->setText(intToQString((ll)attempts));
ui->lblAttempts->setToolTip("操作次数:" + intToQString(attempts));
ui->lblTime->setText(QString::fromLocal8Bit(time.get().data()));
ui->lblCur->setText(intToQString(score));
ui->lblCur->setToolTip("当前得分:" + longToQString(score));
ui->gbxCheckerboard->setEnabled(false);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
ll t = cb.get(i, j);
pbtn[i][j]->setEnabled(false);
pbtn[i][j]->setText(!t ? "" : numToQString(t));
//pbtn[i][j]->setFont(QFont("NumberOnly", sizeStyle[N - 3][t > 2147483648 ? 2 : (t > 131072 ? 131072 : t)]));
if (t > 2147483648) pbtn[i][j]->setFont(QFont("NumberOnly", sizeStyle[N - 3][2]));
else if (t >= 131072) pbtn[i][j]->setFont(QFont("NumberOnly", sizeStyle[N - 3][65536] - 1));
else pbtn[i][j]->setFont(QFont("NumberOnly", sizeStyle[N - 3][t]));
pbtn[i][j]->setStyleSheet(QString::fromLocal8Bit(colorStyle[t > 4294967296 ? -1 : t].data()));
}
if (!attempts || !lastNum || step.empty() || dead || !gaming) ui->pBtnLast->setEnabled(false); //如果未进行过任何操作,上一步技能禁用
else ui->pBtnLast->setEnabled(true);
if (!hammerNum || cb.have() <= 1 || dead || !gaming) ui->pBtnHammer->setEnabled(false); //如果场上只剩下一个数字,敲除方块按钮禁用
else ui->pBtnHammer->setEnabled(true);
}
void gameS8::die() //死亡结算
{
ui->gbxOver->show();
ui->lblScore->setText(longToQString(score));
int isRanked;
if ((isRanked = User::checkRank(N, score)) != -1) { //进入记录榜
if (score > User::getBest(account, N)) //刷新最高分记录
User::setBest(account, N, score);
ui->lblRecord->setText("排行榜 " + intToQString(isRanked + 1));
}
else if (score > User::getBest(account, N)) { //刷新最高分记录
User::setBest(account, N, score);
}
else {
ui->lblRecord->setText(longToQString(User::getBest(account, N)));
}
User::setCoins(account, User::getCoins(account) + cal(score) - cal(lstscore));
ui->lblAddCoins->setText("+ " + intToQString(cal(score)));
update();
ui->pBtnPause->setEnabled(false);
ui->pBtnEnd->setEnabled(false);
User::addHistory(account, Game(N, attempts, score, time, cb.getBoard()));
}
void gameS8::updateTime()
{
ui->lblTime->setText(QString::fromLocal8Bit((++time).get().data()));
}