-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathking.cpp
More file actions
110 lines (98 loc) · 4.08 KB
/
king.cpp
File metadata and controls
110 lines (98 loc) · 4.08 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
#include "king.h"
#include "game.h"
#include "pawn.h"
extern Game *game;
King::King(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
{
setImage();
}
void King::setImage()
{
if(side == "WHITE")
setPixmap(QPixmap(":/images/king1.png"));
else
setPixmap(QPixmap(":/images/king.png"));
}
void King::moves()
{
location.clear();
int row = this->getCurrentBox()->rowLoc;
int col = this->getCurrentBox()->colLoc;
QString team = this->getSide();
if(col > 0 && row > 0 && !(game->collection[row-1][col-1]->getChessPieceColor() == team)) {//up left
location.append(game->collection[row-1][col-1]);
game->collection[row-1][col-1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(col < 7 && row > 0 && !(game->collection[row-1][col+1]->getChessPieceColor() == team)) { // up right
location.append(game->collection[row-1][col+1]);
game->collection[row-1][col+1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(row>0 && !(game->collection[row-1][col]->getChessPieceColor() == team)) {//up
location.append(game->collection[row-1][col]);
game->collection[row-1][col]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(row<7 && !(game->collection[row+1][col]->getChessPieceColor() == team)) {//down
location.append(game->collection[row+1][col]);
game->collection[row+1][col]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(col>0 && !(game->collection[row][col-1]->getChessPieceColor() == team)) {// left
location.append(game->collection[row][col-1]);
game->collection[row][col-1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(col<7 && !(game->collection[row][col+1]->getChessPieceColor() == team)) {//right
location.append(game->collection[row][col+1]);
game->collection[row][col+1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(col > 0 && row < 7 && !(game->collection[row+1][col-1]->getChessPieceColor() == team)) {//down left
location.append(game->collection[row+1][col-1]);
game->collection[row+1][col-1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
if(col < 7 && row < 7 && !(game->collection[row+1][col+1]->getChessPieceColor() == team)) {//down right
location.append(game->collection[row+1][col+1]);
game->collection[row+1][col+1]->setColor(Qt::darkRed);
if(location.last()->getHasChessPiece()){
location.last()->setColor(Qt::red);
}
}
findUnSafeLocation();
}
void King::findUnSafeLocation() {
QList <ChessPiece *> pList = game->alivePiece;
for(size_t i = 0,n = pList.size(); i < n; i++) {
if(pList[i]->getSide() != this->getSide())
{
QList <ChessBox *> bList = pList[i]->moveLocation();
for(size_t j = 0, n = bList.size(); j < n; j++) {
Pawn *c = dynamic_cast<Pawn *> (pList[i]) ;
if(c)
continue;
for(size_t k = 0, n = location.size(); k < n; k++) {
if(bList[j] == location [k]) {
location[k]->setColor(Qt::blue);
}
}
}
}
}
}