-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
300 lines (239 loc) · 7.37 KB
/
MainWindow.cpp
File metadata and controls
300 lines (239 loc) · 7.37 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Fichier contenant la fen�tre principale. C'est elle qui affiche le jeu et qui re�oit les demandes de l'utilisateur.
* \file MainWindow.cpp
* \author Erreur-404 et Mo-LK
* \date 19 avril 2022
* Cr�� le 13 avril 2022
*/
#include "MainWindow.hpp"
namespace view
{
MainWindow::MainWindow() :
sourceTile_(nullptr),
checkedTile_(nullptr),
controller_(new control::Controller()),
playerTurnDisplayer_(nullptr),
gameoverMessage_(nullptr),
positionSelectionWindow_(nullptr)
{
QObject::connect(controller_, &control::Controller::invalidMove,
this, &view::MainWindow::invalidMove);
QObject::connect(controller_, &control::Controller::notifyCheck,
this, &view::MainWindow::assignCheckTile);
QObject::connect(controller_, &control::Controller::notifyGameover,
this, &view::MainWindow::gameover);
QObject::connect(controller_, &control::Controller::notifyPieceAdded,
this, &view::MainWindow::addPiece);
}
MainWindow::~MainWindow()
{
for (auto tile : tiles_) {
delete tile;
}
delete controller_;
}
void MainWindow::buttonClicked()
{
const QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender());
if (sourceTile_ != nullptr) {
const int destinationX = sender->x() / caseWidth;
const int destinationY = sender->y() / caseHeight;
setSourceTileDefaultColor();
movePiece(destinationX, destinationY);
}
else {
const int sourceX = sender->x() / caseWidth;
const int sourceY = sender->y() / caseHeight;
if (controller_->isValidTile(sourceX, sourceY)) {
sourceTile_ = tiles_[sourceX * chessboardHeigth + sourceY];
sourceTile_->setSelected();
}
}
}
void MainWindow::invalidMove(int x, int y)
{
Tile* wantedDestination = tiles_[x * chessboardHeigth + y];
wantedDestination->setInvalid();
}
void MainWindow::assignCheckTile(int x, int y)
{
checkedTile_ = tiles_[x * chessboardHeigth + y];
}
void MainWindow::gameover()
{
gameoverMessage_ = new QLabel("GAME OVER", this);
gameoverMessage_->setGeometry(
gameoverMessageX,
gameoverMessageY,
gameoverMessageWidth,
gameoverMessageHeight
);
gameoverMessage_->setFont(QFont("Helvetica", gameoverMessageSize));
gameoverMessage_->setStyleSheet("QLabel {color: red}");
gameoverMessage_->show();
}
void MainWindow::selectStartupPosition()
{
QPushButton* button = qobject_cast<QPushButton*>(QObject::sender());
std::string startupPosition = button->whatsThis().toStdString();
int positionNumber = stoi(startupPosition.substr(startupPositionStringLength, 1));
displayPrompt(positionNumber);
}
void MainWindow::displayPositionSelectionWindow()
{
positionSelectionWindow_ = new PositionSelectionWindow;
positionSelectionWindow_->createWindow(*this);
positionSelectionWindow_->setAttribute(Qt::WA_DeleteOnClose);
positionSelectionWindow_->show();
}
void MainWindow::addPiece(int x, int y, std::string type, std::string pieceColor)
{
std::string filename = "img/" + pieceColor + "_" + type + ".svg";
QIcon icon(QString::fromStdString(filename));
tiles_[(x * chessboardHeigth) + y]->setImage(icon);
}
void MainWindow::setUi()
{
initializeMainWindow();
initializeBoard();
displayPositionSelectionWindow();
}
void MainWindow::initializeMainWindow()
{
setWindowTitle("Chess");
const int width = margin * 2 + chessboardWidth * caseWidth;
const int height = margin * 2 + chessboardHeigth * caseHeight;
setFixedSize(width, height);
initializePlayerTurnDisplayer();
initializeMenuBar();
show();
}
void MainWindow::initializeBoard()
{
QWidget* board = new QWidget(this);
const int width = chessboardWidth * caseWidth;
const int height = chessboardHeigth * caseHeight;
board->setGeometry(margin, margin, width, height);
for (int i : iter::range(chessboardWidth)) {
for (int j : iter::range(chessboardHeigth)) {
Tile* tile = new Tile("", board, i, j);
QObject::connect(tile, &QPushButton::pressed,
this, &MainWindow::buttonClicked);
tiles_.push_back(tile);
}
}
board->show();
}
void MainWindow::initializePlayerTurnDisplayer()
{
playerTurnDisplayer_ = new QLabel(this);
int width = playerTurnDisplayerWidth;
int height = playerTurnDisplayerHeight;
int x = playerTurnDisplayerX;
int y = playerTurnDisplayerY;
playerTurnDisplayer_->setFixedHeight(height);
playerTurnDisplayer_->setFixedWidth(width);
playerTurnDisplayer_->setScaledContents(true);
playerTurnDisplayer_->move(x, y);
QPixmap colorTurn("img/white.png");
colorTurn.scaled(width, height);
playerTurnDisplayer_->setPixmap(colorTurn);
}
void MainWindow::initializeMenuBar()
{
QMenu* playMenu = menuBar()->addMenu("Play");
QAction* positionSelection = playMenu->addAction("New Game");
QObject::connect(positionSelection, &QAction::triggered,
this, &MainWindow::displayPositionSelectionWindow);
playMenu->addSeparator();
QAction* closeAction = playMenu->addAction("Close");
QObject::connect(closeAction, &QAction::triggered,
this, &MainWindow::close);
}
void MainWindow::displayPrompt(int positionNumber)
{
QMessageBox prompt;
prompt.setWindowTitle("Confirm your selection");
prompt.setText("A preset has been selected.");
prompt.setInformativeText("Do you wish to start the game?");
QPushButton* yes = prompt.addButton(tr("Yes"), QMessageBox::ActionRole);
prompt.addButton(tr("Cancel"), QMessageBox::ActionRole);
prompt.exec();
if (prompt.clickedButton() == yes) {
positionSelectionWindow_->close();
startGame(positionNumber);
}
}
void MainWindow::startGame(int piecesPositionNumber)
{
clear();
controller_->startGame(piecesPositionNumber);
}
void MainWindow::clear()
{
clearBoard();
switchColor(model::Color::white);
delete gameoverMessage_;
}
void MainWindow::clearBoard()
{
sourceTile_ = nullptr;
checkedTile_ = nullptr;
for (Tile* tile : tiles_) {
tile->setImage(QIcon());
tile->setDefaultColor();
}
}
void MainWindow::switchColor(const model::Color& color)
{
QPixmap colorTurn;
if (color == model::Color::white)
colorTurn = QPixmap("img/white.png");
else
colorTurn = QPixmap("img/black.png");
const int width = playerTurnDisplayerWidth;
const int height = playerTurnDisplayerHeight;
colorTurn.scaled(width, height);
playerTurnDisplayer_->setPixmap(colorTurn);
}
void MainWindow::moveIcon(Tile* destinationTile)
{
QIcon icon = sourceTile_->icon();
sourceTile_->setImage(QIcon());
destinationTile->setImage(icon);
}
void MainWindow::setSourceTileDefaultColor()
{
const bool checkedTileIsSelected = (sourceTile_ == checkedTile_);
if (checkedTileIsSelected)
checkedTile_->setCheck();
else
sourceTile_->setDefaultColor();
}
void MainWindow::setCheckedTileColor()
{
if (!checkedTile_->isCheck()) {
checkedTile_->setCheck();
}
else {
checkedTile_->setDefaultColor();
checkedTile_ = nullptr;
}
}
void MainWindow::movePiece(int destinationX, int destinationY)
{
auto [sourceX, sourceY] = sourceTile_->getPosition();
int destinationTileNumber = destinationX * chessboardHeigth + destinationY;
Tile* destinationTile = tiles_[destinationTileNumber];
bool isValidMove = controller_->move(sourceX, sourceY, destinationX, destinationY);
if (isValidMove) {
switchColor(controller_->getColor());
moveIcon(destinationTile);
const bool kingIsChecked = checkedTile_ != nullptr;
if (kingIsChecked) {
setCheckedTileColor();
}
}
sourceTile_ = nullptr;
}
}