-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
1009 lines (882 loc) · 31.8 KB
/
game.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "game.h"
#include "king.h"
#include "button.h"
#include "queen.h"
#include "rook.h"
#include "bishop.h"
#include "knight.h"
#include <QTimer>
#include <QTextEdit>
#include <QDebug>
#include <QtMath>
#include <QGraphicsProxyWidget>
#define WINDOW_WIDTH 1300
#define WINDOW_HEIGHT 700
#define BUTTON_WIDTH 250
#define BUTTON_HEIGHT 86
#define BUTTON_MARGIN 10
Game::Game()
{
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
setWindowTitle("Chess");
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setScene(scene);
setFixedSize(WINDOW_WIDTH+1, WINDOW_HEIGHT+2);
}
Game::~Game()
{
delete movesHistoryDisplay;
delete board;
delete scrollArea;
delete scrollableHistoryWindow;
delete movesHistoryDisplay;
delete checkDisplay;
delete restartButton;
delete undoButton;
delete surrenderButton;
delete drawButton;
delete panel;
delete turnImage;
for(auto item : alivePieces)
delete item;
for(auto item : whiteDead)
delete item;
for(auto item : blackDead)
delete item;
}
//用图片提示现在是谁在走棋
void Game::setTurn(const QString & turn)
{
m_turn = turn;
if(turn == "WHITE"){
turnImage->setPixmap(QPixmap(":/moves/images/Moves/whiteMove.png"));
}
else{
turnImage->setPixmap(QPixmap(":/moves/images/Moves/blackMove.png"));
}
}
QString Game::getTurn()
{
return m_turn;
}
void Game::changeTurn()
{
if(getTurn() == "WHITE"){
setTurn("BLACK");
}
else{
setTurn("WHITE");;
}
}
void Game::placeInDeadPlace(ChessPiece *piece)
{
if(!moveValidationCheck)
resetMoveCounter();
if(piece->side() == "WHITE") {
whiteDead.append(piece);
displayDeadWhite();
} else {
blackDead.append(piece);
displayDeadBlack();
}
alivePieces.removeAll(piece);
}
//判断胜负(包括平局)
void Game::gameover(const QString & winner)
{
disableMouseEventsWhenPanelIsShown();
drawPanel(0, 0, width(), height(), Qt::black, 0.90);
auto gameOverText = new QGraphicsPixmapItem();
gameOverText->setPixmap(QPixmap(":/winners/images/Winners/" + winner + "Won.png"));
gameOverText->setPos(width()/2.0-gameOverText->boundingRect().width()/2.0, 220);
scene->addItem(gameOverText);
auto playAgain = new Button(QString(":/buttons/images/Buttons/restartButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
playAgain->setPos(width()/2.0 - BUTTON_WIDTH - BUTTON_MARGIN, height()/2.0 + 50);
connect(playAgain, SIGNAL(clicked()), this, SLOT(restart()));
scene->addItem(playAgain);
auto quit = new Button(QString(":/buttons/images/Buttons/quitButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
quit->setPos(width()/2.0 + BUTTON_MARGIN, height()/2.0 + 50);
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
scene->addItem(quit);
}
//摆放被吃的白/黑子
void Game::displayDeadWhite()
{
int shift = 80;
int j = 0;
int k = 0;
for(int i = 0, n = whiteDead.size(); i < n; i++) {
if(j == 4) {
k++;
j = 0;
}
whiteDead[i]->setPos(9 * board->fieldAt(0,0)->width() + shift * j ++, scene->height() - shift*(k+1));
}
}
void Game::displayDeadBlack()
{
int shift = 80;
int j = 0;
int k = 0;
for(int i = 0, n = blackDead.size(); i < n; i++) {
if(j == 4) {
k++;
j = 0;
}
blackDead[i]->setPos(9 * board->fieldAt(0,0)->width() + shift * j++, shift*k);
}
}
//游戏主界面
void Game::displayMainMenu()
{
auto background = new QGraphicsPixmapItem();
background->setPixmap(QPixmap(":/background/images/background.png"));
scene->addItem(background);
auto playButton = new Button(QString(":/buttons/images/Buttons/playButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
int playButtonXPos = int(this->width()/2.0 - playButton->boundingRect().width()/2.0 - 200);
int playButtonYPos = 450;
playButton->setPos(playButtonXPos, playButtonYPos);
connect(playButton,SIGNAL(clicked()),this,SLOT(start()));
scene->addItem(playButton);
auto quitButton = new Button(QString(":/buttons/images/Buttons/quitButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
int quitButtonXPos = int(this->width()/2.0 - quitButton->boundingRect().width()/2.0 + 200);
int quitButtonYPos = 450;
quitButton->setPos(quitButtonXPos, quitButtonYPos);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
}
void Game::checkForCheck()
{
//判断是否有将军
//对角移动、左、右、上、下(在车范围内)
//是否在对方控制范围内
bool isBlackChecked = pawnAttack(blackKing)
|| horizontalAttack(blackKing)
|| verticalAttack(blackKing)
|| diagonalAttack(blackKing)
|| knightAttack(blackKing);
if(isBlackChecked) {
blackKing->setChecked(true);
if(!moveValidationCheck) {
blackKing->currentField()->setBrush(Qt::lightGray);
}
} else {
blackKing->setChecked(false);
if(!moveValidationCheck)
blackKing->currentField()->resetOriginalColor();
}
bool whiteIsItChecked = pawnAttack(whiteKing)
|| horizontalAttack(whiteKing)
|| verticalAttack(whiteKing)
|| diagonalAttack(whiteKing)
|| knightAttack(whiteKing);
if(whiteIsItChecked) {
whiteKing->setChecked(true);
if(!moveValidationCheck)
whiteKing->currentField()->setBrush(Qt::lightGray);
} else {
whiteKing->setChecked(false);
if(!moveValidationCheck)
whiteKing->currentField()->resetOriginalColor();
}
if(whiteIsItChecked || isBlackChecked) {
checkDisplay->setVisible(true);
} else {
checkDisplay->setVisible(false);
}
}
void Game::checkForCheckmateOrStalemate()
{
bool whiteIsChecked = whiteKing->checked();
bool blackIsChecked = blackKing->checked();
if(numOfMovesWithoutEating == 50)
gameover("none");
//只剩两个王
if(alivePieces.size() == 2) {
gameover("none");
}
//有王,且车/象在攻击王
if(alivePieces.size() == 3) {
if(std::any_of(alivePieces.begin(), alivePieces.end(), [](ChessPiece * a){return a->type() == "KNIGHT" || a->type() == "BISHOP";}))
gameover("none");
}
//有王,且两车攻击王
if(alivePieces.size() == 4) {
if(std::count_if(alivePieces.begin(), alivePieces.end(), [](ChessPiece * a) {return a->type() == "KNIGHT" && a->side() == "WHITE";}) == 2
|| std::count_if(alivePieces.begin(), alivePieces.end(), [](ChessPiece * a) {return a->type() == "KNIGHT" && a->side() == "BLACK";}) == 2)
gameover("none");
}
//迭代未被吃的子,如果都不能动
//根据是否有将军判断死局
bool areTherePossibleMoves = false;
moveValidationCheck = false;
auto currentAlivePieces = alivePieces;
for(auto item : currentAlivePieces) {
if(item->side() == getTurn()) {
item->generatePossibleMoves();
item->decolorAvailableFields();
if(!item->possibleMoves().empty()) {
areTherePossibleMoves = true;
break;
}
}
}
if(!areTherePossibleMoves) {
if(getTurn() == "WHITE" && whiteIsChecked) {
gameover("black");
} else if(getTurn() == "BLACK" && blackIsChecked) {
gameover("white");
} else {
gameover("none");
}
}
}
void Game::disableMouseEventsWhenPanelIsShown()
{
//主窗口启动时关闭鼠标事件
undoButton->setEnabled(false);
restartButton->setEnabled(false);
surrenderButton->setEnabled(false);
drawButton->setEnabled(false);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
board->fieldAt(i, j)->setEnabled(false);
}
}
for(auto item : alivePieces)
item->setEnabled(false);
for(auto item : whiteDead)
item->setEnabled(false);
for(auto item : blackDead)
item->setEnabled(false);
}
void Game::enableMouseEventsAfterClosingPanel()
{
//在子窗口关闭后重启鼠标事件(子窗口弹出时,不能移动棋子)
undoButton->setEnabled(true);
restartButton->setEnabled(true);
surrenderButton->setEnabled(true);
drawButton->setEnabled(true);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
board->fieldAt(i, j)->setEnabled(true);
}
}
for(auto item : alivePieces)
item->setEnabled(true);
for(auto item : whiteDead)
item->setEnabled(true);
for(auto item : blackDead)
item->setEnabled(true);
}
void Game::updateHistory(const QString & move)
{
QTextCursor cursor = movesHistoryDisplay->textCursor();
if(getTurn() == "WHITE") {
cursor.movePosition(QTextCursor::End);
cursor.insertText(move + "\n");
movesHistoryDisplay->setTextCursor(cursor);
timePanelB->stop();
timePanelW->start();
} else {
cursor.movePosition(QTextCursor::End);
cursor.insertText(move + "\t");
movesHistoryDisplay->setTextCursor(cursor);
timePanelW->stop();
timePanelB->start();
}
}
void Game::updateMoveCounter()
{
numOfMovesWithoutEating++;
}
void Game::resetMoveCounter()
{
numOfMovesWithoutEating = 0;
}
bool Game::isWhiteKingChecked()
{
return whiteKing->checked();
}
bool Game::isBlackKingChecked()
{
return blackKing->checked();
}
//兵攻击王
bool Game::pawnAttack(King *a)
{
bool isItChecked = false;
int row = a->currentField()->row();
int col = a->currentField()->column();
if(a->side() == "BLACK") {
if(row + 1 < 8
&& col - 1 >= 0
&& !board->fieldAt(row+1, col-1)->isItFree()
&& board->fieldAt(row+1, col-1)->currentPiece->side() =="WHITE"
&& board->fieldAt(row+1, col-1)->currentPiece->type() == "PAWN")
isItChecked = true;
if(row + 1 < 8
&& col + 1 < 8
&& !board->fieldAt(row+1, col+1)->isItFree()
&& board->fieldAt(row+1, col+1)->currentPiece->side() == "WHITE"
&& board->fieldAt(row+1, col+1)->currentPiece->type() == "PAWN")
isItChecked = true;
} else {
if(row - 1 >= 0
&& col - 1 >= 0
&& !board->fieldAt(row-1, col-1)->isItFree()
&& board->fieldAt(row-1, col-1)->currentPiece->side() == "BLACK"
&& board->fieldAt(row-1, col-1)->currentPiece->type() == "PAWN")
isItChecked = true;
if(row - 1 >= 0
&& col + 1 < 8
&& !board->fieldAt(row-1, col+1)->isItFree()
&& board->fieldAt(row-1, col+1)->currentPiece->side() == "BLACK"
&& board->fieldAt(row-1, col+1)->currentPiece->type() == "PAWN")
isItChecked = true;
}
return isItChecked;
}
//横斩
bool Game::horizontalAttack(King *a)
{
bool isItChecked = false;
int row = a->currentField()->row();
int col = a->currentField()->column();
for(int i = col + 1; i < 8; i++) {
if(!board->fieldAt(row, i)->isItFree()
&& board->fieldAt(row, i)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(row, i)->isItFree()
&& board->fieldAt(row, i)->currentPiece->side() != a->side()) {
if(board->fieldAt(row, i)->currentPiece->type() == "QUEEN"
|| board->fieldAt(row, i)->currentPiece->type() == "ROOK") {
isItChecked = true;
break;
} else {
break;
}
}
}
for(int i = col - 1; i >= 0; i--) {
if(!board->fieldAt(row, i)->isItFree()
&& board->fieldAt(row, i)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(row, i)->isItFree()
&& board->fieldAt(row, i)->currentPiece->side() != a->side()) {
if(board->fieldAt(row, i)->currentPiece->type() == "QUEEN"
|| board->fieldAt(row, i)->currentPiece->type() == "ROOK") {
isItChecked = true;
break;
} else {
break;
}
}
}
return isItChecked;
}
//竖斩
bool Game::verticalAttack(King *a)
{
bool isItChecked = false;
int row = a->currentField()->row();
int col = a->currentField()->column();
for(int i = row + 1; i < 8; i++) {
if(!board->fieldAt(i, col)->isItFree()
&& board->fieldAt(i, col)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, col)->isItFree()
&& board->fieldAt(i, col)->currentPiece->side() != a->side()) {
if((board->fieldAt(i, col)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, col)->currentPiece->type() == "ROOK")) {
isItChecked = true;
break;
} else {
break;
}
}
}
for(int i = row - 1; i >= 0; i--) {
if(!board->fieldAt(i, col)->isItFree()
&& board->fieldAt(i, col)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, col)->isItFree()
&& board->fieldAt(i, col)->currentPiece->side() != a->side()) {
if(board->fieldAt(i, col)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, col)->currentPiece->type() == "ROOK") {
isItChecked = true;
break;
} else {
break;
}
}
}
return isItChecked;
}
//对角斩
bool Game::diagonalAttack(King *a)
{
bool isItChecked = false;
int row = a->currentField()->row();
int col = a->currentField()->column();
for(int i = row + 1, j = col + 1; i < 8 && j < 8; i++, j++) {
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()) {
if(board->fieldAt(i, j)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, j)->currentPiece->type() == "BISHOP") {
isItChecked = true;
break;
} else {
break;
}
}
}
for(int i = row + 1, j = col - 1; i < 8 && j >= 0; i++, j--) {
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()) {
if(board->fieldAt(i, j)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, j)->currentPiece->type() == "BISHOP") {
isItChecked = true;
break;
} else {
break;
}
}
}
for(int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()) {
if(board->fieldAt(i, j)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, j)->currentPiece->type() == "BISHOP") {
isItChecked = true;
break;
} else {
break;
}
}
}
for(int i = row - 1, j = col + 1; i >= 0 && j < 8; i--, j++) {
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() == a->side())
break;
if(!board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()) {
if(board->fieldAt(i, j)->currentPiece->type() == "QUEEN"
|| board->fieldAt(i, j)->currentPiece->type() == "BISHOP") {
isItChecked = true;
break;
} else {
break;
}
}
}
return isItChecked;
}
//车攻击
bool Game::knightAttack(King *a)
{
bool isItChecked = false;
int row = a->currentField()->row();
int col = a->currentField()->column();
//可能的车攻击范围
int i = row - 2;
int j = col + 1;
if(i >= 0
&& j < 8
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row - 2;
j = col - 1;
if(i >= 0
&& j >= 0
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row - 1;
j = col + 2;
if(i >= 0
&& j < 8
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row - 1;
j = col - 2;
if(i >= 0
&& j >= 0
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row + 1;
j = col - 2;
if(i < 8
&& j >= 0
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row + 1;
j = col + 2;
if(i < 8
&& j < 8
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row + 2;
j = col - 1;
if(i < 8
&& j >= 0
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
i = row + 2;
j = col + 1;
if(i < 8
&& j < 8
&& !board->fieldAt(i, j)->isItFree()
&& board->fieldAt(i, j)->currentPiece->side() != a->side()
&& board->fieldAt(i, j)->currentPiece->type() == "KNIGHT") {
isItChecked = true;
}
return isItChecked;
}
//兵升变
void Game::displayPromotion(const QString & side)
{
panel = drawPanel(0, 0, width(), height(), Qt::black, 0.90);
scene->addItem(panel);
disableMouseEventsWhenPanelIsShown();
auto pickPromotionText = new QGraphicsPixmapItem();
pickPromotionText->setPixmap(QPixmap(":/moves/images/pickPromotion.png"));
pickPromotionText->setPos(width()/2.0-pickPromotionText->boundingRect().width()/2.0, 220);
pickPromotionText->setParentItem(panel);
scene->addItem(pickPromotionText);
promotionPieces.append(new Queen(side, true));
scene->addItem(promotionPieces[0]);
promotionPieces[0]->setPos(width()/2.0 - 175, height()/2.0 + 50);
promotionPieces.append(new Rook(side, true));
scene->addItem(promotionPieces[1]);
promotionPieces[1]->setPos(width()/2.0 - 175 + 90, height()/2.0 + 50);
promotionPieces.append(new Bishop(side, true));
scene->addItem(promotionPieces[2]);
promotionPieces[2]->setPos(width()/2.0 - 175 + 180, height()/2.0 + 50);
promotionPieces.append(new Knight(side, true));
scene->addItem(promotionPieces[3]);
promotionPieces[3]->setPos(width()/2.0 - 175 + 270, height()/2.0 + 50);
}
bool Game::isAlive(ChessPiece *piece)
{
if(piece->side() == "WHITE" && whiteDead.contains(piece))
return false;
if(piece->side() == "BLACK" && blackDead.contains(piece))
return false;
return true;
}
bool Game::nextToKing(Field *field, const QString &kingSide)
{
King * currentKing = nullptr;
if(kingSide == "WHITE")
currentKing = whiteKing;
else
currentKing = blackKing;
if(field->row() >= 0
&& field->row() <= 7
&& field->column() >= 0
&& field->column() <= 7
&& qFabs(currentKing->currentField()->row() - field->row()) <= 1
&& qFabs(currentKing->currentField()->column() - field->column()) <= 1)
return true;
return false;
}
QGraphicsRectItem *Game::drawPanel(int x, int y, int width, int height, const QColor & color, double opacity)
{
//游戏结束、逼和等子窗口的背景
auto panel = new QGraphicsRectItem(x, y, width, height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
panel->setBrush(brush);
panel->setOpacity(opacity);
scene->addItem(panel);
//放按钮
auto panel2 = new QGraphicsPixmapItem();
panel2->setPixmap(QPixmap(":/textures/images/Textures/panelBackground.png").scaled(600, 400));
panel2->setPos(x+this->width()/2.0-300, y+this->height()/2.0-200);
panel2->setOpacity(1);
scene->addItem(panel2);
panel2->setParentItem(panel);
return panel;
}
void Game::previousState()
{
if(!moves.isEmpty()) { // move是否为空串
//回放,但不增加走子记录(只恢复)
if(!moveValidationCheck) {
QTextCursor cursor = movesHistoryDisplay->textCursor();
cursor.movePosition(QTextCursor::End);
if(getTurn() == "BLACK") {
cursor.movePosition(QTextCursor::End);
cursor.select(QTextCursor::LineUnderCursor);
cursor.removeSelectedText();
movesHistoryDisplay->setTextCursor(cursor);
} else {
QString newText = movesHistoryDisplay->toPlainText();
newText.truncate(newText.lastIndexOf('\t'));
movesHistoryDisplay->clear();
movesHistoryDisplay->setText(newText + '\t');
}
}
GameMove * lastMove = moves.last();
//落子
setTurn(lastMove->m_movedPiece->side());
lastMove->m_from->placePiece(lastMove->m_movedPiece);
lastMove->m_movedPiece->setFirstMove(lastMove->m_wasItFirstMove);
if(!moveValidationCheck) {
if(selectedPiece) {
selectedPiece->decolorAvailableFields();
selectedPiece->currentField()->resetOriginalColor();
selectedPiece = nullptr;
}
}
//重置 'to' field
lastMove->m_to->setIsItFree(true);
lastMove->m_to->currentPiece = nullptr;
//在走步之前被将
checkDisplay->setVisible(lastMove->m_wasMovedInCheck);
if(lastMove->m_wasMovedInCheck) {
if(lastMove->m_movedPiece->side() == "WHITE") {
whiteKing->currentField()->setBrush(Qt::lightGray);
} else {
blackKing->currentField()->setBrush(Qt::lightGray);
}
} else if(!lastMove->m_wasMovedInCheck) {
if(lastMove->m_movedPiece->side() == "WHITE") {
whiteKing->currentField()->resetOriginalColor();
} else {
blackKing->currentField()->resetOriginalColor();
}
}
//在落子后吃了对方的子
if(lastMove->m_hasEatenSomething) {
if(lastMove->m_deadPiece->side() == "WHITE") {
whiteDead.removeAll(lastMove->m_deadPiece);
} else {
blackDead.removeAll(lastMove->m_deadPiece);
}
alivePieces.push_back(lastMove->m_deadPiece);
lastMove->m_deadPiece->setFirstMove(lastMove->m_eatenPieceFirstMove);
lastMove->m_deadPieceField->placePiece(lastMove->m_deadPiece);
lastMove->m_deadPiece->setPos(lastMove->m_deadPieceField->x(), lastMove->m_deadPieceField->y());
}
if(lastMove->m_wasOpponentInCheck) {
if(lastMove->m_movedPiece->side() == "WHITE") {
blackKing->currentField()->resetOriginalColor();
blackKing->setChecked(false);
} else {
whiteKing->currentField()->resetOriginalColor();
whiteKing->setChecked(false);
}
}
if(moveValidationCheck) {
if(lastMove->m_movedPiece->type() == "KING") {
King * p = dynamic_cast<King*>(lastMove->m_movedPiece);
if(p->checked())
p->setChecked(false);
}
}
//取消小易位
if(lastMove->m_smallCastling) {
int row = lastMove->m_movedPiece->currentField()->row();
board->fieldAt(row, 5)->setIsItFree(true);
board->fieldAt(row, 6)->setIsItFree(true);
}
//取消长易位
if(lastMove->m_largeCastling) {
int row = lastMove->m_movedPiece->currentField()->row();
board->fieldAt(row, 1)->setIsItFree(true);
board->fieldAt(row, 2)->setIsItFree(true);
board->fieldAt(row, 3)->setIsItFree(true);
}
//取消兵升变
if(lastMove->m_movedPiece->type() == "PAWN"
&& (lastMove->m_to->row() == 7
|| lastMove->m_to->row() == 0)
&& !moveValidationCheck){
alivePieces.removeAll(lastMove->promotionPiece);
if(lastMove->m_movedPiece->side() == "WHITE")
whiteDead.removeAll(lastMove->m_movedPiece);
else {
blackDead.removeAll(lastMove->m_movedPiece);
}
scene->removeItem(lastMove->promotionPiece);
promotionPieces.clear();
}
moves.removeLast();
}
}
//投子认负
void Game::surrender()
{
QString winner = "none";
if(getTurn() == "WHITE") {
winner = "black";
} else {
winner = "white";
}
gameover(winner);
}
void Game::draw()
{
disableMouseEventsWhenPanelIsShown();
panel = drawPanel(0, 0, width(), height(), Qt::black, 0.90);
auto askForDrawText = new QGraphicsPixmapItem();
askForDrawText->setPixmap(QPixmap(":/moves/images/Moves/offeredDraw.png"));
askForDrawText->setPos(width()/2.0-askForDrawText->boundingRect().width()/2.0, 220);
askForDrawText->setParentItem(panel);
scene->addItem(askForDrawText);
auto acceptDraw = new Button(QString(":/buttons/images/Buttons/acceptButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
acceptDraw->setPos(width()/2.0 - 260, height()/2.0 + 50);
connect(acceptDraw, SIGNAL(clicked()), this, SLOT(acceptDraw()));
acceptDraw->setParentItem(panel);
scene->addItem(acceptDraw);
auto declineDraw = new Button(QString(":/buttons/images/Buttons/declineButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
declineDraw->setPos(width()/2.0 + 10, height()/2.0 + 50);
connect(declineDraw, SIGNAL(clicked()), this, SLOT(declineDraw()));
declineDraw->setParentItem(panel);
scene->addItem(declineDraw);
}
//同意和棋申请,平局
void Game::acceptDraw()
{
scene->removeItem(panel);
gameover("none");
}
//取消
void Game::declineDraw()
{
enableMouseEventsAfterClosingPanel();
scene->removeItem(panel);
delete panel;
}
//倒计时和回放的配合
void Game::onUndo()
{
auto size = moves.size();
if(size > 0){
if(getTurn() == "WHITE") {
timePanelW->stop();
timePanelB->start();
} else {
timePanelB->stop();
timePanelW->start();
}
}
}
//开始,初始化所有控件
void Game::start()
{
scene->clear();
moveValidationCheck = false;
enpassantAvailable = false;
numOfMovesWithoutEating = 0;
undoButton = new Button(QString(":/buttons/images/Buttons/undoButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
undoButton->setPos(width() - BUTTON_WIDTH - BUTTON_MARGIN, BUTTON_MARGIN);
scene->addItem(undoButton);
connect(undoButton, SIGNAL(clicked()), this, SLOT(onUndo()));
connect(undoButton, SIGNAL(clicked()), this, SLOT(previousState()));
restartButton = new Button(QString(":/buttons/images/Buttons/restartButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
restartButton->setPos(width() - BUTTON_WIDTH - BUTTON_MARGIN, 2 * BUTTON_MARGIN + BUTTON_HEIGHT);
scene->addItem(restartButton);
connect(restartButton, SIGNAL(clicked()), this, SLOT(restart()));
surrenderButton = new Button(QString(":/buttons/images/Buttons/surrenderButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
surrenderButton->setPos(width() - BUTTON_WIDTH - BUTTON_MARGIN, 3 * BUTTON_MARGIN + 2 * BUTTON_HEIGHT);
scene->addItem(surrenderButton);
connect(surrenderButton, SIGNAL(clicked()), this, SLOT(surrender()));
drawButton = new Button(QString(":/buttons/images/Buttons/drawButton.png"), BUTTON_WIDTH, BUTTON_HEIGHT);
drawButton->setPos(width() - BUTTON_WIDTH - BUTTON_MARGIN, 4 * BUTTON_MARGIN + 3 * BUTTON_HEIGHT);
scene->addItem(drawButton);
connect(drawButton, SIGNAL(clicked()), this, SLOT(draw()));
checkDisplay = new QGraphicsPixmapItem();
checkDisplay->setPixmap(QPixmap(":/moves/images/Moves/check.png"));
checkDisplay->setPos(WINDOW_WIDTH / 2 - checkDisplay->boundingRect().width(), WINDOW_HEIGHT - checkDisplay->boundingRect().height() + 1);
checkDisplay->setVisible(false);
scene->addItem(checkDisplay);
auto movesBackground = new QGraphicsPixmapItem();
movesBackground->setPixmap(QPixmap(":/background/images/movesBackground.png"));
movesBackground->setPos(width() - BUTTON_WIDTH - BUTTON_MARGIN, 5 * BUTTON_MARGIN + 4 * BUTTON_HEIGHT);
scene->addItem(movesBackground);
scrollArea = new QScrollArea();
movesHistoryDisplay = new QTextEdit();
movesHistoryDisplay->setReadOnly(true);
movesHistoryDisplay->setFixedWidth(190);
movesHistoryDisplay->setFixedHeight(230);
movesHistoryDisplay->setFrameShape(QFrame::NoFrame);
scrollArea->setFrameShape(QFrame::NoFrame);
scrollArea->setWidget(movesHistoryDisplay);
scrollArea->setFixedWidth(190);
scrollableHistoryWindow = scene->addWidget(scrollArea);
scrollableHistoryWindow->setPos(width() - scrollArea->width() - 14, 5 * BUTTON_MARGIN + 4 * BUTTON_HEIGHT + 66);
board = new Chessboard();
board->drawFields();
board->drawChessPieces();
selectedPiece = nullptr;
whiteKing = dynamic_cast<King*>(board->fieldAt(7, 4)->currentPiece);
blackKing = dynamic_cast<King*>(board->fieldAt(0, 4)->currentPiece);
turnImage = new QGraphicsPixmapItem();
turnImage->setPixmap(QPixmap(":/moves/images/Moves/whiteMove.png"));
turnImage->setPos(2, WINDOW_HEIGHT - turnImage->boundingRect().height());
scene->addItem(turnImage);
timePanelW = new TimePanel;
timePanelW->setCountDown(true);
scene->addItem(timePanelW);
timePanelW->setPos(0,150);
timePanelW->setRole("white");
timePanelDuration = new TimePanel;
scene->addItem(timePanelDuration);
timePanelDuration->setPos(0,250);
timePanelDuration->setRole("游戏时长");
timePanelDuration->start();
timePanelB = new TimePanel;
timePanelB->setCountDown(true);
scene->addItem(timePanelB);
timePanelB->setPos(0,350);
timePanelB->setRole("black");
setTurn("WHITE");
timePanelW->start();
}
//重玩
void Game::restart()
{