-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
233 lines (200 loc) · 5.71 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
#include "game.h"
#include "button.h"
#include <QPixmap>
#include "king.h"
#include <QDebug>
Game::Game(QWidget *parent ):QGraphicsView(parent)
{
//Making the Scene
gameScene = new QGraphicsScene();
gameScene->setSceneRect(0,0,1400,900);
//Making the view
setFixedSize(1400,900);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setScene(gameScene);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::black);
setBackgroundBrush(brush);
pieceToMove = NULL;
//display turn
turnDisplay = new QGraphicsTextItem();
turnDisplay->setPos(width()/2-100,10);
turnDisplay->setZValue(1);
turnDisplay->setDefaultTextColor(Qt::white);
turnDisplay->setFont(QFont("",18));
turnDisplay->setPlainText("Turn : WHITE");
//display Check
check = new QGraphicsTextItem();
check->setPos(width()/2-100,860);
check->setZValue(4);
check->setDefaultTextColor(Qt::red);
check->setFont(QFont("",18));
check->setPlainText("CHECK!");
check->setVisible(false);
setTurn("WHITE");
}
void Game::drawChessBoard()
{
chess = new ChessBoard();
drawDeadHolder(0,0,Qt::lightGray);
drawDeadHolder(1100,0,Qt::lightGray);
chess->drawBoxes(width()/2-400,50);
}
void Game::displayDeadWhite()
{
int SHIFT = 50;
int j = 0;
int k = 0;
for(size_t i = 0,n = whiteDead.size(); i<n; i++) {
if(j == 4){
k++;
j = 0;
}
whiteDead[i]->setPos(40+SHIFT*j++,100+SHIFT*2*k);
}
}
void Game::displayDeadBlack()
{
int SHIFT = 50;
int j = 0;
int k = 0;
for(size_t i = 0,n = blackDead.size(); i<n; i++) {
if(j == 4){
k++;
j = 0;
}
blackDead[i]->setPos(1140+SHIFT*j++,100+SHIFT*2*k);
}
}
void Game::placeInDeadPlace(ChessPiece *piece)
{
if(piece->getSide() == "WHITE") {
whiteDead.append(piece);
King *g = dynamic_cast <King *>(piece);
if(g){
check->setPlainText("Black Won");
gameOver();
}
displayDeadWhite();
}
else{
blackDead.append(piece);
King *g = dynamic_cast <King *>(piece);
if(g){
check->setPlainText("White Won");
gameOver();
}
displayDeadBlack();
}
alivePiece.removeAll(piece);
}
void Game::addToScene(QGraphicsItem *item)
{
gameScene->addItem(item);
}
void Game::removeFromScene(QGraphicsItem *item)
{
gameScene->removeItem(item);
}
QString Game::getTurn()
{
return turn;
}
void Game::setTurn(QString value)
{
turn = value;
}
void Game::changeTurn()
{
if(getTurn() == "WHITE")
setTurn("BLACK");
else
setTurn("WHITE");
turnDisplay->setPlainText("Turn : " + getTurn());
}
void Game::start()
{
for(size_t i =0, n = listG.size(); i < n; i++)
removeFromScene(listG[i]);
addToScene(turnDisplay);
QGraphicsTextItem* whitePiece = new QGraphicsTextItem();
whitePiece->setPos(70,10);
whitePiece->setZValue(1);
whitePiece->setDefaultTextColor(Qt::white);
whitePiece->setFont(QFont("",14));
whitePiece->setPlainText("WHITE PIECE");
addToScene(whitePiece);
QGraphicsTextItem *blackPiece = new QGraphicsTextItem();
blackPiece->setPos(1170,10);
blackPiece->setZValue(1);
blackPiece->setDefaultTextColor(Qt::black);
blackPiece->setFont(QFont("",14));
blackPiece->setPlainText("BLACK PIECE");
addToScene(blackPiece);
addToScene(check);
chess->addChessPiece();
}
void Game::drawDeadHolder(int x, int y,QColor color)
{
deadHolder = new QGraphicsRectItem(x,y,300,900);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
deadHolder->setBrush(brush);
addToScene(deadHolder);
}
void Game::displayMainMenu()
{
QGraphicsPixmapItem *p = new QGraphicsPixmapItem();
p->setPixmap(QPixmap(":/images/king1.png"));
p->setPos(420,170);
addToScene(p);
listG.append(p);
QGraphicsPixmapItem *p1 = new QGraphicsPixmapItem();
p1->setPixmap(QPixmap(":/images/king.png"));
p1->setPos(920,170);
addToScene(p1);
listG.append(p1);
//Create the title
QGraphicsTextItem *titleText = new QGraphicsTextItem("Chess Pro");
QFont titleFont("arial" , 50);
titleText->setFont( titleFont);
int xPos = width()/2 - titleText->boundingRect().width()/2;
int yPos = 150;
titleText->setPos(xPos,yPos);
addToScene(titleText);
listG.append(titleText);
//create Button
Button * playButton = new Button("Play 2 v 2");
int pxPos = width()/2 - playButton->boundingRect().width()/2;
int pyPos = 300;
playButton->setPos(pxPos,pyPos);
connect(playButton,SIGNAL(clicked()) , this , SLOT(start()));
addToScene(playButton);
listG.append(playButton);
//Create Quit Button
Button * quitButton = new Button("Quit");
int qxPos = width()/2 - quitButton->boundingRect().width()/2;
int qyPos = 375;
quitButton->setPos(qxPos,qyPos);
connect(quitButton, SIGNAL(clicked()),this,SLOT(close()));
addToScene(quitButton);
drawChessBoard();
listG.append(quitButton);
}
void Game::gameOver()
{
//removeAll();
setTurn("WHITE");
alivePiece.clear();
chess->reset();
}
void Game::removeAll(){
QList<QGraphicsItem*> itemsList = gameScene->items();
for(size_t i = 0, n = itemsList.size();i<n;i++) {
if(itemsList[i]!=check)
removeFromScene(itemsList[i]);
}
}