-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchesspiece.cpp
97 lines (83 loc) · 2.06 KB
/
chesspiece.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
#include "chesspiece.h"
#include "game.h"
#include <QDebug>
#include "king.h"
extern Game *game;
ChessPiece::ChessPiece(QString team, QGraphicsItem *parent):QGraphicsPixmapItem(parent)
{
side = team;
isPlaced = true;
firstMove = true;
}
void ChessPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//Deselect
if(this == game->pieceToMove){
game->pieceToMove->getCurrentBox()->resetOriginalColor();
game->pieceToMove->decolor();
game->pieceToMove = NULL;
return;
}
//if it is already consumed or not the respective color's turn
if((!getIsPlaced() )|| ( (game->getTurn() != this->getSide())&& (!game->pieceToMove)) )
return;
//selecting
if(!game->pieceToMove){
game->pieceToMove = this;
game->pieceToMove->getCurrentBox()->setColor(Qt::red);
game->pieceToMove->moves();
}
//Consuming counterPart of the CHessBox
else if(this->getSide() != game->pieceToMove->getSide()){
this->getCurrentBox()->mousePressEvent(event);
}
}
void ChessPiece::setCurrentBox(ChessBox *box)
{
currentBox = box;
}
ChessBox *ChessPiece::getCurrentBox()
{
return currentBox;
}
QString ChessPiece::getSide()
{
return side;
}
void ChessPiece::setSide( QString value)
{
side = value;
}
bool ChessPiece::getIsPlaced()
{
return isPlaced;
}
void ChessPiece::setIsPlaced(bool value)
{
isPlaced = value;
}
QList<ChessBox *> ChessPiece::moveLocation()
{
return location;
}
void ChessPiece::decolor()
{
for(size_t i = 0, n = location.size(); i < n;i++) {
location[i]->resetOriginalColor();
}
}
bool ChessPiece::boxSetting(ChessBox *box)
{
if(box->getHasChessPiece()) {
King *q = dynamic_cast<King*>(location.last()->currentPiece);
if(q){
box->setColor(Qt::blue);
}
else
box->setColor(Qt::red);
return true;
}
else
location.last()->setColor(Qt::darkRed);
return false;
}