-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathchessbox.cpp
More file actions
173 lines (147 loc) · 4.73 KB
/
chessbox.cpp
File metadata and controls
173 lines (147 loc) · 4.73 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
#include "chessbox.h"
#include "game.h"
#include <QDebug>
#include "king.h"
extern Game *game;
ChessBox::ChessBox(QGraphicsItem *parent):QGraphicsRectItem(parent)
{
//making the Square CHess Box
setRect(0,0,100,100);
brush.setStyle(Qt::SolidPattern);
setZValue(-1);
setHasChessPiece(false);
setChessPieceColor("NONE");
currentPiece = NULL;
}
ChessBox::~ChessBox()
{
delete this;
}
void ChessBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// qDebug() << getChessPieceColor();
//Deselecting counter part of ChessPiece
if(currentPiece == game->pieceToMove && currentPiece){
currentPiece->mousePressEvent(event);
return;
}
//if selected
if(game->pieceToMove){
//if same team
if(this->getChessPieceColor() == game->pieceToMove->getSide())
return;
//removing the eaten piece
QList <ChessBox *> movLoc = game->pieceToMove->moveLocation();
//TO make sure the selected box is in move zone
int check = 0;
for(size_t i = 0, n = movLoc.size(); i < n;i++) {
if(movLoc[i] == this) {
check++;
}
}
// if not prsent return
if(check == 0)
return;
//change the color back to normal
game->pieceToMove->decolor();
//make the first move false applicable for pawn only
game->pieceToMove->firstMove = false;
//this is to eat or consume the enemy present inn the movable region
if(this->getHasChessPiece()){
this->currentPiece->setIsPlaced(false);
this->currentPiece->setCurrentBox(NULL);
game->placeInDeadPlace(this->currentPiece);
}
//changing the new stat and resetting the previous left region
game->pieceToMove->getCurrentBox()->setHasChessPiece(false);
game->pieceToMove->getCurrentBox()->currentPiece = NULL;
game->pieceToMove->getCurrentBox()->resetOriginalColor();
placePiece(game->pieceToMove);
game->pieceToMove = NULL;
//changing turn
game->changeTurn();
checkForCheck();
}
//Selecting couterpart of the chessPiece
else if(this->getHasChessPiece())
{
this->currentPiece->mousePressEvent(event);
}
}
void ChessBox::setColor(QColor color)
{
brush.setColor(color);
setBrush(color);
}
void ChessBox::placePiece(ChessPiece *piece)
{
piece->setPos(x()+50- piece->pixmap().width()/2 ,y()+50-piece->pixmap().width()/2);
piece->setCurrentBox(this);
setHasChessPiece(true,piece);
currentPiece = piece;
}
void ChessBox::resetOriginalColor()
{
setColor(originalColor);
}
void ChessBox::setOriginalColor(QColor value)
{
originalColor = value;
setColor(originalColor);
}
bool ChessBox::getHasChessPiece()
{
return hasChessPiece;
}
void ChessBox::setHasChessPiece(bool value, ChessPiece *piece)
{
hasChessPiece = value;
if(value)
setChessPieceColor(piece->getSide());
else
setChessPieceColor("NONE");
}
void ChessBox::checkForCheck()
{
int c = 0;
QList <ChessPiece *> pList = game->alivePiece;
for(size_t i = 0,n=pList.size(); i < n; i++ ) {
King * p = dynamic_cast<King *> (pList[i]);
if(p){
continue;
}
pList[i]->moves();
pList[i]->decolor();
QList <ChessBox *> bList = pList[i]->moveLocation();
for(size_t j = 0,n = bList.size(); j < n; j ++) {
King * p = dynamic_cast<King *> (bList[j]->currentPiece);
if(p) {
if(p->getSide() == pList[i]->getSide())
continue;
bList[j]->setColor(Qt::blue);
pList[i]->getCurrentBox()->setColor(Qt::darkRed);
if(!game->check->isVisible())
game->check->setVisible(true);
else{
bList[j]->resetOriginalColor();
pList[i]->getCurrentBox()->resetOriginalColor();
game->gameOver();
}
c++;
}
}
}
if(!c){
game->check->setVisible(false);
for(size_t i = 0,n=pList.size(); i < n; i++ )
pList[i]->getCurrentBox()->resetOriginalColor();
}
}
QString ChessBox::getChessPieceColor()
{
return chessPieceColor;
}
void ChessBox::setChessPieceColor(QString value)
{
chessPieceColor = value;
}