-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
241 lines (208 loc) · 7.26 KB
/
board.cpp
File metadata and controls
241 lines (208 loc) · 7.26 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
#include "board.h"
#include "point.h"
#include "dice.h"
#include "spot.h"
#include "diceanimation.h"
#include "doubledice.h"
#include "mainwindow.h"
#include <QtGui>
#include <QString>
#include <sstream>
#include <iostream>
#include <fstream>
#include <QIODevice>
#include <QDataStream>
#include <QLabel>
#include <QPalette>
#include <QBrush>
#include <QColor>
#include <QDebug>
#include <QFileDialog>
#include <QDir>
const unsigned int Board::noOfPoints = 24;
const unsigned int Board::noOfFinishPlaces = 4;
const unsigned int Board::noOfCheckers = 30;
const unsigned int Board::width = 934;
const unsigned int Board::height = 568;
Board::Board(QWidget * parent) : QWidget(parent) {
setFixedSize(width, height);
// FIXME: call changestyle at startup instead of this and after that
// the changestyle at constructors can be deleted from all of the classes
QPalette myPalette = palette();
myPalette.setBrush(QPalette::Window, QBrush(QPixmap(":/images/backgrounda.png")));
setPalette(myPalette);
setAutoFillBackground(true);
/* POINTS */
double loosen = 1.06;
// at the bottom
for (unsigned int i=0, line = 0; i<noOfPoints/2; ++i) {
if ((i%2)!=0) {
points[i] = new Point(Point::WHITE,Point::DOWN, this);
} else
points[i] = new Point(Point::BLACK,Point::DOWN, this);
if (i == noOfPoints*1/4) line = 50;
points[i]->move(loosen*i*points[i]->scalex + line + 110, height/2 + 10);
connect(points[i], SIGNAL(killedChecker(Checker *)), this, SLOT(placeKilledChecker(Checker *)));
}
// on top
for (unsigned int i=noOfPoints/2, j = 0, line = 0; i<noOfPoints; ++i, ++j) {
if ((i%2)==0) {
points[i] = new Point(Point::WHITE, Point::UP, this);
} else points[i] = new Point(Point::BLACK, Point::UP, this);
if (i == noOfPoints*3/4) line = 50;
points[i]->move(loosen*j*points[i]->scalex + line + 110, 15);
connect(points[i], SIGNAL(killedChecker(Checker *)), this, SLOT(placeKilledChecker(Checker *)));
}
/* FINISH PLACE */
points[24] = new Point(Point::BLACK, Point::END, this);
points[24]->move(19, 11);
points[25] = new Point(Point::WHITE, Point::END, this);
points[25]->move(19, 294);
points[26] = new Point(Point::WHITE, Point::END, this);
points[26]->move(860, 15);
points[27] = new Point(Point::BLACK, Point::END, this);
points[27]->move(860, 294);
/* FINISH PLACE END*/
/* POINTS END */
createCheckers();
/* DICE */
dice1 = new Dice(6, Dice::TOP, this);
dice2 = new Dice(6, Dice::BOTTOM, this);
diceanimation = new DiceAnimation(this);
diceanimation->addDice(dice1);
diceanimation->addDice(dice2);
doubledice = new DoubleDice(this);
doubledice->setNumber(2);
/* DICE */
resetCheckers();
stylecode = 'a';
changeStyle(stylecode);
}
void Board::createCheckers() {
for (unsigned int i=0; i<Board::noOfCheckers; ++i){
if (i<=Board::noOfCheckers/2-1)
checkers[i] = new Checker(Checker::WHITE, this);
else
checkers[i] = new Checker(Checker::BLACK, this);
}
}
void Board::deleteCheckers(unsigned int num) {
for (unsigned int i=0; i<num; ++i) {
delete checkers[i];
}
}
void Board::resetCheckers() {
/* CHECKERS */
for (int i=0; i<5; ++i) points[0]->addChecker(checkers[i]);
for (int i=5; i<7; ++i) points[11]->addChecker(checkers[i]);
for (int i=7; i<10; ++i) points[16]->addChecker(checkers[i]);
for (int i=10; i<15; ++i) points[18]->addChecker(checkers[i]);
for (int i=15; i<18; ++i) points[4]->addChecker(checkers[i]);
for (int i=18; i<23; ++i) points[6]->addChecker(checkers[i]);
for (int i=23; i<28; ++i) points[12]->addChecker(checkers[i]);
for (int i=28; i<30; ++i) points[23]->addChecker(checkers[i]);
/* CHECKERS END */
}
void Board::clearPoints() {
for (unsigned int i=0; i<noOfPoints + noOfFinishPlaces; ++i) {
points[i]->clearCheckers();
}
}
void Board::placeKilledChecker(Checker * checkerHit) {
if (checkerHit->getMyColor() == Checker::WHITE)
points[25]->addChecker(checkerHit);
else
points[24]->addChecker(checkerHit);
}
bool Board::validDoublediceNum(const int num) {
return (num == 2 || num == 4 || num == 8 || num == 16 || num == 32 || num == 64);
}
bool Board::loadFromFile(const QString filename, Board* &retBoard) {
bool error = false;
retBoard = new Board;
std::ifstream openedfile;
openedfile.open(filename.toUtf8().constData());
if (openedfile.is_open()) {
std::string doubledicestr;
int doubledicenum;
bool ok;
openedfile >> doubledicestr;
doubledicenum = QString(doubledicestr.c_str()).toInt(&ok);
int checker_count = 0;
if ((ok) && (validDoublediceNum(doubledicenum))) {
retBoard->doubledice->setNumber(doubledicenum);
int nextChecker = 0;
retBoard->clearPoints();
retBoard->deleteCheckers(noOfCheckers);
int error_at = 0;
std::string nextPointstr;
openedfile >> nextPointstr;
int i = 1;
while ((i <= 28) && (!error)) {
error_at += 1;
unsigned int nextPoint;
unsigned int nextColor;
unsigned int nextCount;
std::string nextColorstr;
std::string nextCountstr;
nextPoint = QString(nextPointstr.c_str()).toInt(&ok);
if ((ok) && (nextPoint <= noOfPoints + noOfFinishPlaces)){
openedfile >> nextColorstr;
nextColor = QString(nextColorstr.c_str()).toInt(&ok);
if ((ok) && (nextColor < 3)){
openedfile >> nextCountstr;
nextCount = QString(nextCountstr.c_str()).toInt(&ok);
if ((ok) && (nextCount < noOfCheckers)){
for (unsigned int j=0; j < nextCount; j++) {
retBoard->checkers[nextChecker] = new Checker(nextColor, retBoard);
checker_count++;
retBoard->points[nextPoint]->addChecker(retBoard->checkers[nextChecker]);
++nextChecker;
}
} else error = true;
} else error = true;
} else error = true;
openedfile >> nextPointstr;
i++;
}
openedfile.close();
// if (error) {
// QMessageBox::warning(this, "Error", "There is an error in the file at line " + QString::number(error_at));
// clearPoints();
// deleteCheckers(checker_count);
// createCheckers();
// resetCheckers();
// }
} else error = true;// QMessageBox::warning(this, "Error", "The first line is wrong in the file.");
openedfile.close();
} else error = true;// QMessageBox::warning(this, "File not found", "Can't find this file.");
return !error;
}
void Board::saveToFile(QString filename) const {
std::ofstream savedfile;
savedfile.open(filename.toUtf8().constData());
savedfile << doubledice->getNumber() << "\n";
for (unsigned int i=0; i< noOfPoints + noOfFinishPlaces; ++i) {
savedfile << i << " " << points[i]->checkersColor() << " " << points[i]->getCheckersNo() << "\n";
}
savedfile.close();
}
void Board::changeStyle(const char codeletter) {
stylecode = codeletter;
QPalette myPalette = palette();
myPalette.setBrush(QPalette::Window,
QBrush(QPixmap(QString(":/images/background")
+ codeletter
+ QString(".png"))));
setPalette(myPalette);
setAutoFillBackground(true);
for (unsigned int i=0; i<noOfPoints+4; ++i) {
points[i]->changeStyle(codeletter);
}
dice1->changeStyle(codeletter);
dice2->changeStyle(codeletter);
doubledice->changeStyle(codeletter);
for (unsigned int i=0; i<Board::noOfCheckers; ++i) {
checkers[i]->changeStyle(codeletter);
}
}