-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.cpp
More file actions
235 lines (215 loc) · 5.24 KB
/
field.cpp
File metadata and controls
235 lines (215 loc) · 5.24 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
#include <QPushButton>
#include <QStyle>
#include <QLabel>
#include <QMouseEvent>
#include "field.h"
ClickableLabel::ClickableLabel(QWidget* parent)
: QLabel(parent)
{
}
ClickableLabel::~ClickableLabel()
{
}
/**
* @brief react on the left mouse click
*/
void ClickableLabel::mousePressEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton && active)
{
emit clicked(index);
}
}
/**
* @brief sets correct index
* @param i Index that should be set
*/
void ClickableLabel::indexChange(int i)
{
index = i;
}
/**
* @brief Constructor
*/
field::field(Color fColor, int coord)
:fieldColor(fColor), coordinate(coord), beat(false)
{
}
/**
* @brief Copy constructor
*/
field::field(const field& obj)
{
fieldColor = obj.fieldColor;
coordinate = obj.coordinate;
beat = obj.beat;
moves = obj.moves;
beats = obj.beats;
figure = new Figure;
if(obj.figure)
{
figure->setKing(obj.figure->isKing());
figure->setColor(obj.figure->getColor());
figure->setBeat(obj.figure->getBeat());
}
else figure = nullptr;
checkerbutton = new ClickableLabel;
checkerbutton->index = obj.checkerbutton->index;
checkerbutton->active = obj.checkerbutton->active;
}
field& field::operator=(const field &obj)
{
if(this != &obj)
{
this->~field();
fieldColor = obj.fieldColor;
coordinate = obj.coordinate;
beat = obj.beat;
moves = obj.moves;
beats = obj.beats;
figure = new Figure;
if(obj.figure)
{
figure->setKing(obj.figure->isKing());
figure->setColor(obj.figure->getColor());
figure->setBeat(obj.figure->getBeat());
}
else figure = nullptr;
checkerbutton = new ClickableLabel;
checkerbutton->index = obj.checkerbutton->index;
checkerbutton->active = obj.checkerbutton->active;
}
return *this;
}
/**
* @brief Destructor
*/
field::~field()
{
if(figure)
{
delete figure;
figure = nullptr;
}
delete checkerbutton;
}
/**
* @brief set figure on the field
* @param fig Figure that should be set
*/
void field::setFigure(Figure *fig)
{
figure = fig;
}
/**
* @brief remove figure from the field
* @return removed figure
*/
Figure* field::removeFigure()
{
Figure* f = figure;
figure = nullptr;
return f;
}
/**
* @brief get figure
* @return figure that is on the field or nullptr if there is no figure
*/
Figure* field::getFigure()
{
return figure;
}
/**
* @brief sets picture on the field
* @param path Path of the picture
*/
void field::setPicture(const QString& path)
{
QPixmap checkerbuttonPix(path);
checkerbutton->setPixmap(checkerbuttonPix);
checkerbutton->setScaledContents(true);
}
/**
* @brief choose what picture should be set according to the parametres of the field
* and set the picture
*/
void field::setPicture()
{
if(fieldColor == Color::WHITE) setPicture(whiteField);
else
{
if(!figure) setPicture(blackField);
else if(figure->getColor() == Color::WHITE)
{
if(figure->isKing()) setPicture(whiteKing);
else setPicture(whiteSimpleFigure);
}
else if(figure->getColor() == Color::BLACK)
{
if(figure->isKing()) setPicture(blackKing);
else setPicture(blackSimpleFigure);
}
}
}
/**
* @brief set whether the field should react on mouse click
* @param active - thrue if the field should react on click
* false otherwise
*/
void field::setActive(bool active)
{
checkerbutton->active = active;
}
/**
* @brief choose what picture should be set onthe field when it is clicked
*/
void field::markField()
{
if(!figure && fieldColor == Color::BLACK) setPicture(blackChosenField);
else if(figure->getColor() == Color::WHITE)
{
if(figure->isKing()) setPicture(whiteChosenKing);
else setPicture(whiteChosenFigure);
}
else if(figure->getColor() == Color::BLACK)
{
if(figure->isKing()) setPicture(blackChosenKing);
else setPicture(blackChosenFigure);
}
}
/**
* @brief set correct picture on the field
*/
void field::unmarkField()
{
setPicture();
}
/**
* @brief add possible move to the vector of moves
* @param i Index of the field for possible moves
*/
void field::addMove(int i)
{
moves.push_back(i);
}
/**
* @brief checks whether the figure on the field can move to the field with index to
* @param to Field where figure need to move
* @return true if the move is allowed
* false otherwise
*/
bool field::canMoveTo(int to)
{
//checks if figure need to beat on the field with index to
for(int i = 0; i < beats.size(); ++i)
{
if(beats[i] == to)
return true;
}
//checks if figure need to move on the field with index to
for(int i = 0; i < moves.size(); ++i)
{
if(moves[i] == to) return true;
}
return false;
}