-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessgame.cpp
More file actions
277 lines (248 loc) · 10.4 KB
/
Copy pathchessgame.cpp
File metadata and controls
277 lines (248 loc) · 10.4 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
#include "chessgame.h"
#include "ui_chessgame.h"
#include "king.h"
#include "rook.h"
#include "knight.h"
#include "bishop.h"
#include "queen.h"
#include "mainpawn.h"
#include <vector>
#include <string>
#include <QDebug>
#include <cmath>
ChessGame::ChessGame(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ChessGame)
{
ui->setupUi(this);
QPushButton *numButtons[64];
for(int column=0; column<8; ++column){
for(int row=0; row<8; ++row) {
QString butName = "field_" + QString::number(column) + QString::number(row) ;
numButtons[(row+1)*(column+1)] = ChessGame::findChild<QPushButton *>(butName);
numButtons[(row+1)*(column+1)]->setText("");
numButtons[(row+1)*(column+1)]->setDisabled(true);
connect(numButtons[(row+1)*(column+1)], SIGNAL(released()), this,
SLOT(FieldPressed()));
}
}
ChessGame::SetBoard();
ui->turn->setText(QString::fromStdString(ChessGame::turn));
UndisableButtons();
}
ChessGame::~ChessGame()
{
delete ui;
}
/*!
* \brief ChessGame::UndisableButtons undisable white or black buttons depends on turn
*/
void ChessGame::UndisableButtons(){
string color = ChessGame::turn;
for(int row=0; row <8; row++){
for(int col=0; col<8; col++){
QString field = "field_" + QString::number(row) + QString::number(col);
QPushButton *button = ChessGame::findChild<QPushButton *>(field);
if(ChessGame::ChessBoard[row][col]->getColor() == color){
button->setDisabled(false);
}
}
}
}
/*!
* \brief ChessGame::DisableAllButtons Disable all buttons on the chess board
*/
void ChessGame::DisableAllButtons(){
for(int row=0; row <8; row++){
for(int col=0; col<8; col++){
QString field = "field_" + QString::number(row) + QString::number(col);
QPushButton *button = ChessGame::findChild<QPushButton *>(field);
button->setDisabled(true);
}
}
}
/*!
* \brief ChessGame::UndisablePossibleMoves Undisable buttons on which selected object can move
*/
void ChessGame::UndisablePossibleMoves(){
for(int row=0; row <8; row++){
for(int col=0; col<8; col++){
if(ChessGame::possibleMoves[row][col] == true){
QString field = "field_" + QString::number(row) + QString::number(col);
QPushButton *button = ChessGame::findChild<QPushButton *>(field);
button->setDisabled(false);
}
}
}
}
/*!
* \brief ChessGame::FieldPressed When button is pressed this function checks what has to be done to move on this object
*/
void ChessGame::FieldPressed(){
QPushButton *button = (QPushButton *)sender();
QString butName = button->objectName();
int row = butName.split("_")[1][0].digitValue();
int col = butName.split("_")[1][1].digitValue();
if(ChessGame::selectedPosition[0] >= 0){
SetSelectedProposition(row, col);
TryToMovePawn();
} else {
SetSelectedPosition(row, col);
CheckPossibleMoves(ChessGame::ChessBoard[row][col]);
UndisablePossibleMoves();
}
}
void ChessGame::SetSelectedPosition(int row, int col){
ChessGame::selectedPosition[0] = row;
ChessGame::selectedPosition[1] = col;
}
void ChessGame::CheckPossibleMoves(Pawn* pawn){
ChessGame::possibleMoves = pawn->checkPossibleMove(ChessGame::selectedPosition, ChessGame::ChessBoard);
}
void ChessGame::SetSelectedProposition(int row, int col){
ChessGame::propositionPosition[0] = row;
ChessGame::propositionPosition[1] = col;
}
/*!
* \brief ChessGame::TryToMovePawn Make move of object on chess board, and checks if is winner
*/
void ChessGame::TryToMovePawn(){
bool isRightPosition = CheckPosition();
if(isRightPosition){
if(ChessGame::ChessBoard[ChessGame::selectedPosition[0]][ChessGame::selectedPosition[1]]->getName() == "Pion"){
ChessGame::ChessBoard[ChessGame::selectedPosition[0]][ChessGame::selectedPosition[1]]->setFirstMove(false);
}
MovePawn();
if(!ChessGame::isWinner){
ChangeTurn();
}
}
ResetPosition();
DisableAllButtons();
UndisableButtons();
}
/*!
* \brief ChessGame::ChangeTurn After move position of object is changed turn
*/
void ChessGame::ChangeTurn(){
if(ChessGame::turn == "white"){
ChessGame::turn = "black";
} else {
ChessGame::turn = "white";
}
ui->turn->setText(QString::fromStdString(ChessGame::turn));
}
/*!
* \brief ChessGame::MovePawn if it is possible, move pawn and reset last position of object
*/
void ChessGame::MovePawn(){
CheckLoss();
Pawn* pawnToMove = ChessGame::ChessBoard[ChessGame::selectedPosition[0]][ChessGame::selectedPosition[1]];
ChessGame::ChessBoard[ChessGame::propositionPosition[0]][ChessGame::propositionPosition[1]] = pawnToMove;
QString field = "field_" + QString::number(ChessGame::propositionPosition[0]) + QString::number(ChessGame::propositionPosition[1]);
QPushButton *button = ChessGame::findChild<QPushButton *>(field);
button->setText(QString::fromStdString(pawnToMove->getName()));
AddIcon(pawnToMove->getIconName(), button);
Pawn* nullPawn = new Pawn();
ChessGame::ChessBoard[ChessGame::selectedPosition[0]][ChessGame::selectedPosition[1]] = nullPawn;
QString fieldNull = "field_" + QString::number(ChessGame::selectedPosition[0]) + QString::number(ChessGame::selectedPosition[1]);
QPushButton *nullButton = ChessGame::findChild<QPushButton *>(fieldNull);
nullButton->setText(QString::fromStdString(nullPawn->getName()));
nullButton->setIcon(QIcon());
}
/*!
* \brief ChessGame::CheckLoss Check if is winner or looser
*/
void ChessGame::CheckLoss(){
if(ChessGame::ChessBoard[ChessGame::propositionPosition[0]][ChessGame::propositionPosition[1]]->getName() == "Król"){
string lostMessage = ChessGame::ChessBoard[ChessGame::propositionPosition[0]][ChessGame::propositionPosition[1]]->getColor() + " lost";
ui->turn->setText(QString::fromStdString(lostMessage));
ChessGame::isWinner = true;
}
}
void ChessGame::ResetPosition(){
ChessGame::selectedPosition[0] = -1;
ChessGame::selectedPosition[1] = -1;
}
bool ChessGame::CheckPosition(){
bool isRightPosition = ChessGame::possibleMoves[ChessGame::propositionPosition[0]][ChessGame::propositionPosition[1]];
return (isRightPosition);
}
/*!
* \brief ChessGame::SetBoard Add all pawn on chess board
*/
void ChessGame::SetBoard(){
for(int row=0; row<8; row++){
for(int col=0; col<8; col++){
Pawn* nullPawn = new Pawn();
ChessGame::ChessBoard[row][col] = nullPawn;
}
}
Rook *leftWhiteRook = new Rook;
ChessGame::SetPawn(leftWhiteRook, leftWhiteRook->getleftWhiteStartColumn(), leftWhiteRook->getleftWhiteStartRow(), "white");
Rook *rightWhiteRook= new Rook;
ChessGame::SetPawn(rightWhiteRook, rightWhiteRook->getrightWhiteStartColumn(), rightWhiteRook->getrightWhiteStartRow(), "white");
Rook *leftBlackRook = new Rook;
ChessGame::SetPawn(leftBlackRook, leftBlackRook->getleftBlackStartColumn(), leftBlackRook->getleftBlackStartRow(), "black");
Rook *rightBlackRook = new Rook;
ChessGame::SetPawn(rightBlackRook, rightBlackRook->getrightBlackStartColumn(), rightBlackRook->getrightBlackStartRow(), "black");
Knight *leftWhiteKnight = new Knight;
ChessGame::SetPawn(leftWhiteKnight, leftWhiteKnight->getleftWhiteStartColumn(), leftWhiteKnight->getleftWhiteStartRow(), "white");
Knight *rightWhiteKnight = new Knight;
ChessGame::SetPawn(rightWhiteKnight, rightWhiteKnight->getrightWhiteStartColumn(), rightWhiteKnight->getrightWhiteStartRow(), "white");
Knight *leftBlackKnight = new Knight;
ChessGame::SetPawn(leftBlackKnight, leftBlackKnight->getleftBlackStartColumn(), leftBlackKnight->getleftBlackStartRow(), "black");
Knight *rightBlackKnight = new Knight;
ChessGame::SetPawn(rightBlackKnight, rightBlackKnight->getrightBlackStartColumn(), rightBlackKnight->getrightBlackStartRow(), "black");
Bishop *leftWhiteBishop = new Bishop;
ChessGame::SetPawn(leftWhiteBishop, leftWhiteBishop->getleftWhiteStartColumn(), leftWhiteBishop->getleftWhiteStartRow(), "white");
Bishop *rightWhiteBishop = new Bishop;
ChessGame::SetPawn(rightWhiteBishop, rightWhiteBishop->getrightWhiteStartColumn(), rightWhiteBishop->getrightWhiteStartRow(), "white");
Bishop *leftBlackBishop = new Bishop;
ChessGame::SetPawn(leftBlackBishop, leftBlackBishop->getleftBlackStartColumn(), leftBlackBishop->getleftBlackStartRow(), "black");
Bishop *rightBlackBishop = new Bishop;
ChessGame::SetPawn(rightBlackBishop, rightBlackBishop->getrightBlackStartColumn(), rightBlackBishop->getrightBlackStartRow(), "black");
King *whiteKing = new King;
ChessGame::SetPawn(whiteKing, whiteKing->getwhiteStartColumn(), whiteKing->getwhiteStartRow(), "white");
King *blackKing = new King;
ChessGame::SetPawn(blackKing, blackKing->getblackStartColumn(), blackKing->getblackStartRow(), "black");
Queen *whiteQueen = new Queen;
ChessGame::SetPawn(whiteQueen, whiteQueen->getwhiteStartColumn(), whiteQueen->getwhiteStartRow(), "white");
Queen *blackQueen = new Queen;
ChessGame::SetPawn(blackQueen, blackQueen->getblackStartColumn(), blackQueen->getblackStartRow(), "black");
for(int i=0; i<8; i++){
MainPawn *blackMainPawn = new MainPawn;
ChessGame::SetPawn(blackMainPawn, i, 1, "black");
MainPawn *whiteMainPawn = new MainPawn;
ChessGame::SetPawn(whiteMainPawn, i, 6, "white");
}
}
/*!
* \brief ChessGame::SetPawn Set individual pawn
* \param pawn Class which is inherited of Pawn abstraction
* \param column Current column
* \param row Current row
* \param color What color of pawn
*/
void ChessGame::SetPawn(Pawn* pawn, int column, int row, string color){
pawn->setColor(color);
ChessGame::ChessBoard[row][column] = pawn;
QString field = "field_" + QString::number(row) + QString::number(column);
QPushButton *button = ChessGame::findChild<QPushButton *>(field);
button->setText(QString::fromStdString(pawn->getName()));
AddIcon(pawn->getIconName(), button);
}
/*!
* \brief ChessGame::AddIcon Add icon to button of pawn
* \param url Url to image of icon
* \param button To which button has to add icon
*/
void ChessGame::AddIcon(QString url, QPushButton* button){
QIcon icon;
QPixmap qpm;
if(qpm.load(":/images/"+url+".png"))
{
icon.addPixmap(qpm);
button->setIcon(icon);
}
}