-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
296 lines (245 loc) · 9.17 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "SchaakStuk.h"
#include <QMessageBox>
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
scene = new ChessBoard;
g = new Game;
beurt = wit;
focused = nullptr;
QGraphicsView *view = new QGraphicsView(scene);
setCentralWidget(view);
connect(scene,SIGNAL(clicked(int,int)),this,SLOT(clicked(int,int)));
createActions();
createMenus();
newGame();
}
void MainWindow::clicked(int r, int k) {
update();
SchaakStuk* selected = g->getPiece(r, k);
if(selected != nullptr && selected->getKleur() != beurt) {
QMessageBox box1;
box1.setText(QString("Dit zijn jouw stukken niet! >:("));
box1.exec();
return;
}
scene->setTileSelect(r,k,true);
pair<vector<SchaakStuk*>, vector<SchaakStuk*>> allpcs = {g->witte_stukken(), g->zwarte_stukken()};
vector<SchaakStuk*> enemies;
vector<SchaakStuk*> allies;
if (beurt == wit){
enemies = allpcs.second;
allies = allpcs.first;
}
else if (beurt == zwart) {
enemies = allpcs.first;
allies = allpcs.second;
}
if (display_kills->isChecked()) {
for(auto ally :allies) {
for (auto move : ally->geldige_zetten(g)) {
for (SchaakStuk *enemy : enemies) {
if (g->findPiece(enemy) == move) {
scene->setPieceThreat(g->findPiece(enemy).first, g->findPiece(enemy).second, true);
}
}
}
}
}
if (display_threats->isChecked()) {
for(auto enemy : enemies){
for(auto move : enemy->geldige_zetten(g)){
for(auto ally : allies){
if (g->findPiece(ally) == move){
scene->setPieceThreat(g->findPiece(ally).first, g->findPiece(ally).second, true);
}
}
}
}
}
if(selected != nullptr) {
if (display_moves->isChecked()) {
for (auto move: selected->geldige_zetten(g)) {
int x = move.first;
int y = move.second;
for (auto enemy : enemies) {
for (auto enemymove : enemy->geldige_zetten(g)) {
if (enemymove == move) { scene->setTileThreat(x, y, true); }
else { scene->setTileFocus(x, y, true); }
}
}
}
}
}
if(focused != nullptr){
if(g->move(focused, r, k)) {
if (beurt == wit) { beurt = zwart; }
else { beurt = wit; }
focused = nullptr;
update();
}
}
if (selected != nullptr){
focused = selected;
}
}
void MainWindow::newGame(){
//for()
g->setStartBord();
update();
}
void MainWindow::save() {
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save game"), "",
tr("Chess File (*.chs);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QDataStream out(&file);
out << QString("Rb") << QString("Hb") << QString("Bb") << QString("Qb") << QString("Kb") << QString("Bb") << QString("Hb") << QString("Rb");
for (int i=0;i<8;i++) {
out << QString("Pb");
}
for (int r=3;r<7;r++) {
for (int k=0;k<8;k++) {
out << QString(".");
}
}
for (int i=0;i<8;i++) {
out << QString("Pw");
}
out << QString("Rw") << QString("Hw") << QString("Bw") << QString("Qw") << QString("Kw") << QString("Bw") << QString("Hw") << QString("Rw");
}
}
void MainWindow::open() {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Load game"), "",
tr("Chess File (*.chs);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
try {
QDataStream in(&file);
QString debugstring;
for (int r=0;r<8;r++) {
for (int k=0;k<8;k++) {
QString piece;
in >> piece;
debugstring += "\t" + piece;
if (in.status()!=QDataStream::Ok) {
throw QString("Error reading file "+fileName);
}
}
debugstring += "\n";
}
QMessageBox::information(this, tr("Debug"),
debugstring);
} catch (QString& Q) {
QMessageBox::information(this, tr("Error reading file"),
Q);
}
}
update();
}
void MainWindow::undo() {
QMessageBox box;
box.setText(QString("Je hebt undo gekozen"));
box.exec();
}
void MainWindow::redo() {}
void MainWindow::visualizationChange() {
QMessageBox box;
QString visstring = QString(display_moves->isChecked()?"T":"F")+(display_kills->isChecked()?"T":"F")+(display_threats->isChecked()?"T":"F");
box.setText(QString("Visualization changed : ")+visstring);
box.exec();
}
// Update de inhoud van de grafische weergave van het schaakbord (scene)
// en maak het consistent met de game state in variabele g.
void MainWindow::update() {
scene->clearBoard();
scene->removeAllMarking();
for (int r = 0 ; r < 8 ; r++){
for (int k = 0 ; k < 8 ; k++){
if(g->getPiece(r, k) != nullptr){
scene->setItem(r, k, g->getPiece(r,k)->piece());
}
}
}
}
////////////////
void MainWindow::createActions() {
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Start a new game"));
connect(newAct, &QAction::triggered, this, &MainWindow::newGame);
openAct = new QAction(tr("&Open"), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Read game from disk"));
connect(openAct, &QAction::triggered, this, &MainWindow::open);
saveAct = new QAction(tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save game to disk"));
connect(saveAct, &QAction::triggered, this, &MainWindow::save);
exitAct = new QAction(tr("&Exit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Abandon game"));
connect(exitAct, &QAction::triggered, this, &MainWindow::on_actionExit_triggered);
undoAct = new QAction(tr("&Undo"), this);
undoAct->setShortcuts(QKeySequence::Undo);
undoAct->setStatusTip(tr("Undo last move"));
connect(undoAct, &QAction::triggered, this, &MainWindow::undo);
redoAct = new QAction(tr("&redo"), this);
redoAct->setShortcuts(QKeySequence::Redo);
redoAct->setStatusTip(tr("Redo last undone move"));
connect(redoAct, &QAction::triggered, this, &MainWindow::redo);
display_moves= new QAction(tr("&valid moves"), this);
display_moves->setStatusTip(tr("Show valid moves"));
display_moves->setCheckable(true);
display_moves->setChecked(true);
connect(display_moves, &QAction::triggered, this, &MainWindow::visualizationChange);
display_kills= new QAction(tr("threathed &enemy"), this);
display_kills->setStatusTip(tr("Highlight threathened pieces (enemy)"));
display_kills->setCheckable(true);
display_kills->setChecked(true);
connect(display_kills, &QAction::triggered, this, &MainWindow::visualizationChange);
display_threats= new QAction(tr("threathed &player"), this);
display_threats->setStatusTip(tr("Highlight threathened pieces (player)"));
display_threats->setCheckable(true);
display_threats->setChecked(true);
connect(display_threats, &QAction::triggered, this, &MainWindow::visualizationChange);
}
void MainWindow::createMenus() {
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(exitAct);
gameMenu = menuBar()->addMenu(tr("&Game"));
gameMenu->addAction(undoAct);
gameMenu->addAction(redoAct);
visualizeMenu = menuBar()->addMenu(tr("&Visualize"));
visualizeMenu->addAction(display_moves);
visualizeMenu->addAction(display_kills);
visualizeMenu->addAction(display_threats);
}
void MainWindow::on_actionExit_triggered() {
if (QMessageBox::Yes == QMessageBox::question(this,
tr("Spel verlaten"),
tr("Bent u zeker dat u het spel wil verlaten?\nNiet opgeslagen wijzigingen gaan verloren.")))
{
QApplication::quit();
}
}