forked from dpsying/ChineseChess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
823 lines (751 loc) · 17.2 KB
/
mainwindow.cpp
File metadata and controls
823 lines (751 loc) · 17.2 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpainter.h"
#include "qevent.h"
#include "qmessagebox.h"
#define CHESS_ROWS 9
#define CHESS_COLUMES 8
#define RECT_WIDTH 94
#define RECT_HEIGHT 96
#define START_X (62-RECT_WIDTH/2) //62-96/2 棋盘左上定点像素值(62,76),棋盘的格子大小(96,96)
#define START_Y (76-RECT_HEIGHT/2) //76-96/2
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->mainToolBar->hide();
ui->menuBar->hide();
//resize((CHESS_COLUMES + 1)*RECT_WIDTH ,(CHESS_ROWS + 1)*RECT_HEIGHT);
QPixmap pixChessBoard(":/image/image/ChessBoard.png");
resize(pixChessBoard.size());
QPalette palette;
palette.setBrush( QPalette::Window,QBrush(pixChessBoard));
setPalette(palette);
m_ItemsImage = QPixmap(":/image/image/items.png");
m_nItemWidth = m_ItemsImage.width() / COLOR_MAX;
m_nItemHeight = m_ItemsImage.height() / ITEM_MAX;
NewGame();
startTimer(500);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent( QPaintEvent * )
{
QPainter painter(this);
for (int i = 0; i<m_items.size(); i++)
{
DrawItem(painter,m_items[i]);
}
update();
}
void MainWindow::DrawItem(QPainter& painter, Item item )
{
if (!item.m_bShow)
{
return;
}
QRect rcSrc(m_nItemWidth*item.m_color,m_nItemHeight*item.m_type,m_nItemWidth,m_nItemHeight);
QRect rcTarget( START_X + item.m_pt.x()*RECT_WIDTH,
START_Y + item.m_pt.y()*RECT_HEIGHT,
RECT_WIDTH,RECT_HEIGHT);
painter.drawPixmap(rcTarget,m_ItemsImage,rcSrc);
}
void MainWindow::InitItems()
{
m_items.clear();
Item item1(ITEM_JU,COLOR_BLACK,QPoint(0,0));
Item item2(ITEM_MA,COLOR_BLACK,QPoint(1,0));
Item item3(ITEM_XIANG,COLOR_BLACK,QPoint(2,0));
Item item4(ITEM_SHI,COLOR_BLACK,QPoint(3,0));
Item item5(ITEM_SHUAI,COLOR_BLACK,QPoint(4,0));
Item item6(ITEM_SHI,COLOR_BLACK,QPoint(5,0));
Item item7(ITEM_XIANG,COLOR_BLACK,QPoint(6,0));
Item item8(ITEM_MA,COLOR_BLACK,QPoint(7,0));
Item item9(ITEM_JU,COLOR_BLACK,QPoint(8,0));
Item item10(ITEM_PAO,COLOR_BLACK,QPoint(1,2));
Item item11(ITEM_PAO,COLOR_BLACK,QPoint(7,2));
Item item12(ITEM_BING,COLOR_BLACK,QPoint(0,3));
Item item13(ITEM_BING,COLOR_BLACK,QPoint(2,3));
Item item14(ITEM_BING,COLOR_BLACK,QPoint(4,3));
Item item15(ITEM_BING,COLOR_BLACK,QPoint(6,3));
Item item16(ITEM_BING,COLOR_BLACK,QPoint(8,3));
Item item_1(ITEM_JU,COLOR_RED,QPoint(0,9));
Item item_2(ITEM_MA,COLOR_RED,QPoint(1,9));
Item item_3(ITEM_XIANG,COLOR_RED,QPoint(2,9));
Item item_4(ITEM_SHI,COLOR_RED,QPoint(3,9));
Item item_5(ITEM_SHUAI,COLOR_RED,QPoint(4,9));
Item item_6(ITEM_SHI,COLOR_RED,QPoint(5,9));
Item item_7(ITEM_XIANG,COLOR_RED,QPoint(6,9));
Item item_8(ITEM_MA,COLOR_RED,QPoint(7,9));
Item item_9(ITEM_JU,COLOR_RED,QPoint(8,9));
Item item_10(ITEM_PAO,COLOR_RED,QPoint(1,7));
Item item_11(ITEM_PAO,COLOR_RED,QPoint(7,7));
Item item_12(ITEM_BING,COLOR_RED,QPoint(0,6));
Item item_13(ITEM_BING,COLOR_RED,QPoint(2,6));
Item item_14(ITEM_BING,COLOR_RED,QPoint(4,6));
Item item_15(ITEM_BING,COLOR_RED,QPoint(6,6));
Item item_16(ITEM_BING,COLOR_RED,QPoint(8,6));
m_items.push_back(item1);
m_items.push_back(item2);
m_items.push_back(item3);
m_items.push_back(item4);
m_items.push_back(item5);
m_items.push_back(item6);
m_items.push_back(item7);
m_items.push_back(item8);
m_items.push_back(item9);
m_items.push_back(item10);
m_items.push_back(item11);
m_items.push_back(item12);
m_items.push_back(item13);
m_items.push_back(item14);
m_items.push_back(item15);
m_items.push_back(item16);
m_items.push_back(item_1);
m_items.push_back(item_2);
m_items.push_back(item_3);
m_items.push_back(item_4);
m_items.push_back(item_5);
m_items.push_back(item_6);
m_items.push_back(item_7);
m_items.push_back(item_8);
m_items.push_back(item_9);
m_items.push_back(item_10);
m_items.push_back(item_11);
m_items.push_back(item_12);
m_items.push_back(item_13);
m_items.push_back(item_14);
m_items.push_back(item_15);
m_items.push_back(item_16);
}
void MainWindow::mousePressEvent(QMouseEvent * e)
{
QPoint pt;
pt.setX( (e->pos().x() - START_X ) / RECT_WIDTH);
pt.setY( (e->pos().y() - START_Y ) / RECT_HEIGHT);
if(m_bHasSelected)
{
if (pt == m_SelectedItem.m_pt)
{
//再次点击已经选择的棋子,什么也不做
return;
}
Item ClickedItem;
if (FindItemAtPoint(pt,ClickedItem))
{
//点击的同色的另外一个棋子,改选
if ( (m_bIsRedTurn && ClickedItem.m_color == COLOR_RED) ||
(!m_bIsRedTurn && ClickedItem.m_color != COLOR_RED))
{
SetItemShow(m_SelectedItem,true);
m_SelectedItem = ClickedItem;
return;
}
}
QVector<QPoint> moveArea;
GetItemCanMoveArea(m_SelectedItem,moveArea);
if (moveArea.contains(pt))
{
bool bDeleteSHUAI = false;
DeleteItemAtPoint(pt,bDeleteSHUAI);
ChangeItemPoint(m_SelectedItem.m_pt,pt);
if (bDeleteSHUAI)
{
QString str = m_bIsRedTurn?QStringLiteral("红方胜利!"):QStringLiteral("黑方胜利!");
QMessageBox::information(NULL, "GAME OVER ",str, QMessageBox::Yes , QMessageBox::Yes);
NewGame();
return ;
}
m_bHasSelected = false;
m_bIsRedTurn = !m_bIsRedTurn;
update();
return ;
}
else
{
return ;
}
}
else
{
Item ClickedItem;
if (FindItemAtPoint(pt,ClickedItem))
{
if ( (m_bIsRedTurn && ClickedItem.m_color == COLOR_RED) ||
(!m_bIsRedTurn && ClickedItem.m_color == COLOR_BLACK))
{
m_SelectedItem = ClickedItem;
m_bHasSelected = true;
return;
}
}
}
}
bool MainWindow::MoveItem(Item item,QPoint pt)
{
QVector<QPoint> moveArea;
GetItemCanMoveArea(item,moveArea);
if (moveArea.contains(pt))
{
bool bDeleteSHUAI = false;
DeleteItemAtPoint(pt,bDeleteSHUAI);
ChangeItemPoint(item.m_pt,pt);
if (bDeleteSHUAI)
{
QString str = m_bIsRedTurn?QStringLiteral("红方胜利!"):QStringLiteral("黑方胜利!");
QMessageBox::information(NULL, "GAME OVER ",str, QMessageBox::Yes , QMessageBox::Yes);
NewGame();
}
return true;
}
else
{
return false;
}
}
bool MainWindow::FindItemAtPoint(QPoint pt,Item& item)
{
for (int i = 0; i<m_items.size(); i++)
{
if (m_items[i].m_pt == pt)
{
item = m_items[i];
return true;
}
}
return false;
}
bool MainWindow::DeleteItemAtPoint(QPoint pt,bool& bDeleteSHUAI)
{
bool bDeleted = false;
for (int i = 0; i<m_items.size(); i++)
{
if (m_items[i].m_pt == pt)
{
bDeleteSHUAI = (m_items[i].m_type == ITEM_SHUAI);
m_items.erase(m_items.begin()+i);
bDeleted = true;
break;
}
}
return bDeleted;
}
void MainWindow::GetItemCanMoveArea(Item item,QVector<QPoint>& moveArea)
{
switch (item.m_type)
{
case ITEM_JU:
{
GetMoveArea_JU(item,moveArea);
break;
}
case ITEM_MA:
{
GetMoveArea_MA(item,moveArea);
break;
}
case ITEM_XIANG:
{
GetMoveArea_XIANG(item,moveArea);
break;
}
case ITEM_SHI:
{
GetMoveArea_SHI(item,moveArea);
break;
}
case ITEM_SHUAI:
{
GetMoveArea_SHUAI(item,moveArea);
break;
}
case ITEM_PAO:
{
GetMoveArea_PAO(item,moveArea);
break;
}
case ITEM_BING:
{
GetMoveArea_BING(item,moveArea);
break;
}
}
}
void MainWindow::ChangeItemPoint(QPoint ptItem,QPoint pt)
{
for (int i = 0; i<m_items.size(); i++)
{
if (m_items[i].m_pt == ptItem)
{
m_SelectedItem.m_pt = QPoint(-1,-1);
m_items[i].m_pt = pt;
m_items[i].m_bShow = true;
break;
}
}
}
void MainWindow::timerEvent( QTimerEvent * )
{
for (int i = 0; i<m_items.size(); i++)
{
if (m_items[i].m_pt == m_SelectedItem.m_pt)
{
m_items[i].m_bShow = !m_items[i].m_bShow;
}
}
}
void MainWindow::GetMoveArea_JU(Item item,QVector<QPoint>& moveArea)
{
//棋子“车”的计算可移动区域算法简介:
//1,向4个方向上依次遍历点,把空点都加入。
//当碰到棋子时停止,如果是同色棋子,不加入;是异色棋子,则加入(该处认为可走,因为可以吃子)
//左边
int x = item.m_pt.x() - 1;
while(x>=0)
{
QPoint ptNew = QPoint(x,item.m_pt.y());
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (item.m_color != item2.m_color)
{
moveArea.append(ptNew);
}
break;
}
moveArea.append(ptNew);
x--;
}
//右边
x = item.m_pt.x() + 1;
while(x<=8)
{
QPoint ptNew = QPoint(x,item.m_pt.y());
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (item.m_color != item2.m_color)
{
moveArea.append(ptNew);
}
break;
}
moveArea.append(ptNew);
x++;
}
//上边
int y = item.m_pt.y() - 1;
while(y>=0)
{
QPoint ptNew = QPoint(item.m_pt.x(),y);
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (item.m_color != item2.m_color)
{
moveArea.append(ptNew);
}
break;
}
moveArea.append(ptNew);
y--;
}
//下方
y = item.m_pt.y() + 1;
while(y<=9)
{
QPoint ptNew = QPoint(item.m_pt.x(),y);
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (item.m_color != item2.m_color)
{
moveArea.append(ptNew);
}
break;
}
moveArea.append(ptNew);
y++;
}
}
void MainWindow::GetMoveArea_MA( Item item,QVector<QPoint>& moveArea )
{
//棋子“马”的计算可移动区域算法简介:
//1,求出8个待选位置,8个位置的偏移是(-2,-1)(-2,1)(2,-1)(2,1)(1,-2)(1,2)(-1,-2)(-1,2)存在关系:|x|+|y|=3
//2,判断待选位置是否在棋盘内
//3,判断中间是否有卡位的棋子
//4,位置上是否已存在同色棋子
Item item2;
for (int i = -2; i<=2; i++)
{
for(int j = -2; j<=2; j++)
{
if (qAbs(i) + qAbs(j) == 3)
{
QPoint ptNew = item.m_pt + QPoint(i,j);
if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y()>=0 && ptNew.y() <= 9)
{
}
else
{
continue;
}
//求该方向行走路线的 卡位元素位置
QPoint ptDirect(0,0);
if (qAbs(i) > qAbs(j))
{
if (i>0)
{
ptDirect = QPoint(1,0);
}
else
{
ptDirect = QPoint(-1,0);
}
}
else
{
if (j>0)
{
ptDirect = QPoint(0,1);
}
else
{
ptDirect = QPoint(0,-1);
}
}
QPoint ptHit = item.m_pt + ptDirect; //马的卡位元素位置
if (FindItemAtPoint(ptHit,item2))
{
//卡位
continue;
}
if (FindItemAtPoint(ptNew ,item2) && item.m_color == item2.m_color)
{
//有本组item
continue;
}
moveArea.append(ptNew);
}
}
}
}
void MainWindow::GetMoveArea_XIANG( Item item,QVector<QPoint>& moveArea )
{
//棋子“相”的计算可移动区域算法简介:
//1,求出4个待选位置
//2,判断是否在该“相”对应的半边棋盘内
//3,判断中间是否有卡位的棋子
//4,位置上是否已存在同色棋子
QVector<QPoint> directions;
directions.push_back(QPoint(-2,-2));
directions.push_back(QPoint(2,-2));
directions.push_back(QPoint(2,2));
directions.push_back(QPoint(-2,2));
for (int i = 0; i<directions.size(); i++)
{
QPoint ptNew = item.m_pt + directions[i]; //移动后的点
if (item.m_pt.y() <= 4) //上方的
{
if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y()>=0 && ptNew.y() <= 4) //移动后需仍在上半棋盘
{
}
else
{
continue;
}
}
else
{
if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y()>=5 && ptNew.y() <= 9) //移动后需仍在下半棋盘
{
}
else
{
continue;
}
}
QPoint ptHit = item.m_pt + QPoint(directions[i].x()/2,directions[i].y()/2); //卡位点在中点
Item item2;
if (FindItemAtPoint(ptHit,item2))
{
//存在卡位棋子
continue;
}
if (FindItemAtPoint(ptNew ,item2) && item.m_color == item2.m_color)
{
//终点是同色的棋子
continue;
}
moveArea.append(ptNew);
}
}
void MainWindow::GetMoveArea_SHI( Item item,QVector<QPoint>& moveArea )
{
//移动算法与相类似
//4个待选的位置偏移
QVector<QPoint> directions;
directions.push_back(QPoint(-1,-1));
directions.push_back(QPoint(1,-1));
directions.push_back(QPoint(1,1));
directions.push_back(QPoint(-1,1));
for (int i = 0; i<directions.size(); i++)
{
QPoint ptNew = item.m_pt + directions[i]; //移动后的点
if (item.m_pt.y() <= 4) //上方的
{
if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y()>=0 && ptNew.y() <= 2) //移动后在上棋盘的(3,0)~(5,2)田字内
{
}
else
{
continue;
}
}
else
{
if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y()>=7 && ptNew.y() <= 9) //移动后需仍在下半棋盘
{
}
else
{
continue;
}
}
//QPoint ptHit = item.m_pt + QPoint(directions[i].x()/2,directions[i].y()/2); //卡位点在中点
Item item2;
//if (FindItemAtPoint(ptHit,item2))
//{
// //存在卡位的棋子
// continue;
//}
if (FindItemAtPoint(ptNew ,item2) && item.m_color == item2.m_color)
{
//终点是同色的棋子
continue;
}
moveArea.append(ptNew);
}
}
void MainWindow::GetMoveArea_SHUAI( Item item,QVector<QPoint>& moveArea )
{
QVector<QPoint> directions;
directions.push_back(QPoint(-1,0));
directions.push_back(QPoint(1,0));
directions.push_back(QPoint(0,1));
directions.push_back(QPoint(0,-1));
for (int i = 0; i<directions.size(); i++)
{
QPoint ptNew = item.m_pt + directions[i]; //移动后的点
if (item.m_pt.y() <= 4) //上方的
{
if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y()>=0 && ptNew.y() <= 2) //移动后在上棋盘的(3,0)~(5,2)田字内
{
}
else
{
continue;
}
}
else
{
if (ptNew.x() >= 3 && ptNew.x() <= 5 && ptNew.y()>=7 && ptNew.y() <= 9) //移动后需仍在下半棋盘
{
}
else
{
continue;
}
}
Item item2;
if (FindItemAtPoint(ptNew ,item2) && item.m_color == item2.m_color)
{
//终点是同色的棋子
continue;
}
moveArea.append(ptNew);
}
}
void MainWindow::GetMoveArea_PAO( Item item,QVector<QPoint>& moveArea )
{
//炮的可移动区域分为两部分:第一部分同“车”,第二部分为直线上隔一个棋子后的异色棋子
//QVector<QPoint> juMoveArea;
//GetMoveArea_JU(item,moveArea);
//左边
bool bFind = false;
int x = item.m_pt.x() - 1;
while(x>=0)
{
QPoint ptNew = QPoint(x,item.m_pt.y());
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (bFind)
{
//如果之前找到过一个点,那么这是第2个点
if(item.m_color != item2.m_color)
{
//如果这第2个点是异色的,那么是可以吃的
moveArea.append(ptNew);
}
break;
}
bFind = true;
x--;
continue;
}
if (!bFind)
{
moveArea.append(ptNew);
}
x--;
}
//右边
bFind = false;
x = item.m_pt.x() + 1;
while(x<=8)
{
QPoint ptNew = QPoint(x,item.m_pt.y());
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (bFind)
{
//如果之前找到过一个点,那么这是第2个点
if(item.m_color != item2.m_color)
{
//如果这第2个点是异色的,那么是可以吃的
moveArea.append(ptNew);
}
break;
}
bFind = true;
x++;
continue;
}
if (!bFind)
{
moveArea.append(ptNew);
}
x++;
}
//上边
bFind = false;
int y = item.m_pt.y() - 1;
while(y>=0)
{
QPoint ptNew = QPoint(item.m_pt.x(),y);
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (bFind)
{
//如果之前找到过一个点,那么这是第2个点
if(item.m_color != item2.m_color)
{
//如果这第2个点是异色的,那么是可以吃的
moveArea.append(ptNew);
}
break;
}
bFind = true;
y--;
continue;
}
if (!bFind)
{
moveArea.append(ptNew);
}
y--;
}
//下方
bFind = false;
y = item.m_pt.y() + 1;
while(y<=9)
{
QPoint ptNew = QPoint(item.m_pt.x(),y);
Item item2;
if (FindItemAtPoint(ptNew ,item2))
{
if (bFind)
{
//如果之前找到过一个点,那么这是第2个点
if(item.m_color != item2.m_color)
{
//如果这第2个点是异色的,那么是可以吃的
moveArea.append(ptNew);
}
break;
}
bFind = true;
y++;
continue;
}
if (!bFind)
{
moveArea.append(ptNew);
}
y++;
}
}
void MainWindow::GetMoveArea_BING( Item item,QVector<QPoint>& moveArea )
{
QVector<QPoint> directions;
if (item.m_color == COLOR_RED)
{
directions.push_back(QPoint(0,-1)); //红色上移
if (item.m_pt.y() < 5 ) //红色在下方,y<5就是已经过河,可以横向移动
{
directions.push_back(QPoint(-1,0));
directions.push_back(QPoint(1,0));
}
}
else
{
directions.push_back(QPoint(0,1)); //黑色下移
if (item.m_pt.y() >= 5 ) //黑色在上方,y>=5就是已经过河,可以横向移动
{
directions.push_back(QPoint(-1,0));
directions.push_back(QPoint(1,0));
}
}
for (int i = 0; i<directions.size(); i++)
{
QPoint ptNew = item.m_pt + directions[i]; //移动后的点
if (ptNew.x() >= 0 && ptNew.x() <= 8 && ptNew.y()>=0 && ptNew.y() <= 9) //移动后需仍在棋盘
{
}
else
{
continue;
}
Item item2;
if (FindItemAtPoint(ptNew ,item2) && item.m_color == item2.m_color)
{
//终点是同色的棋子
continue;
}
moveArea.append(ptNew);
}
}
void MainWindow::NewGame()
{
InitItems();
m_SelectedItem.m_pt = QPoint(-1,-1);
m_bIsRedTurn = true;
m_bHasSelected = false;
}
void MainWindow::SetItemShow( Item item,bool bShow )
{
for (int i = 0; i<m_items.size(); i++)
{
if (m_items[i].m_pt == item.m_pt)
{
m_items[i].m_bShow = bShow;
break;
}
}
}