-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
213 lines (172 loc) · 4.01 KB
/
Cell.cpp
File metadata and controls
213 lines (172 loc) · 4.01 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
#include "Cell.h"
short Cell::_idCounter = 0;
Cell::Cell(CellType type) : _id(_idCounter++), _type(type)
{
_cellSize = sf::Vector2f(62.f, 325.f);
_textOffset = sf::Vector2f(10.f, 10.f);
_font.loadFromFile(FONT_PATH);
_idText.setFont(_font);
_idText.setString(std::to_string(_id));
_idText.setFillColor(sf::Color::Red);
_idText.setCharacterSize(20);
_debugRect.setOutlineColor(sf::Color::Green);
_debugRect.setOutlineThickness(1.f);
_debugRect.setFillColor(sf::Color(0, 255, 0, 40));
_debugRect.setSize(_cellSize);
_hintCheck = new Check(CheckType::Hint);
}
Cell::~Cell()
{
for (auto check : _checks)
{
delete check;
}
delete _hintCheck;
}
void Cell::SetPosition(sf::Vector2f position)
{
_debugRect.setPosition(position);
_idText.setPosition(position + _textOffset);
}
sf::Vector2f Cell::GetPosition()
{
return _debugRect.getPosition();
}
void Cell::Draw(Window* window)
{
window->Draw(_debugRect);
window->Draw(_idText);
for(auto check : _checks)
{
check->Draw(window);
}
}
bool Cell::IsAddCheckPossible(CellStatus fromCellStatus)
{
switch (fromCellStatus)
{
case CellStatus::Free:
Debug::LogWarning("Cannot move check from free cell");
return false;
break;
case CellStatus::FirstPlayer:
return IsAddCheckPossible(CheckType::FirstPlayer);
break;
case CellStatus::SecondPlayer:
return IsAddCheckPossible(CheckType::SecondPlayer);
break;
}
}
bool Cell::IsAddCheckPossible(CheckType checkType)
{
if (IsHinted())
{
Debug::LogError("Cannot add check to Cell with id: " + std::to_string(_id) + "\nHint is shown");
return false;
}
if (checkType == CheckType::FirstPlayer && _status == CellStatus::SecondPlayer)
{
Debug::LogError("Cannot add check to Cell with id: " + std::to_string(_id) + "\nDifferent type of cell status and check type");
return false;
}
if (checkType == CheckType::SecondPlayer && _status == CellStatus::FirstPlayer)
{
Debug::LogError("Cannot add check to Cell with id: " + std::to_string(_id) + "\nDifferent type of cell status and check type");
return false;
}
return true;
}
bool Cell::IsAddCheckPossible(Check* check)
{
if (check == nullptr)
{
Debug::LogError("Cannot add check to Cell with id: " + std::to_string(_id) + "\n Incorrect pointer given");
return false;
}
return IsAddCheckPossible(check->GetType());
}
bool Cell::IsRemoveCheckPossible()
{
if (IsHinted())
{
Debug::LogError("Cannot remove check from Cell with id: " + std::to_string(_id) + "\n Hint is shown");
return false;
}
if (_checks.empty())
{
Debug::LogError("Cannot remove check from Cell with id: " + std::to_string(_id) + "\nContainer is empty");
return false;
}
return true;
}
void Cell::AddCheck(Check* check)
{
if (!IsAddCheckPossible(check))
return;
switch (check->GetType())
{
case CheckType::FirstPlayer:
_status = CellStatus::FirstPlayer;
break;
case CheckType::SecondPlayer:
_status = CellStatus::SecondPlayer;
break;
}
sf::Vector2f globalPosition = _debugRect.getPosition();
sf::Vector2f vertialOffset;
if (_id / 12)
{
vertialOffset.y += _checks.size() * PER_CHECK_OFFSET;
}
else
{
vertialOffset.y = CELL_HEIGHT - CHECK_HEIGHT;
vertialOffset.y -= _checks.size() * PER_CHECK_OFFSET;
}
check->SetPosition(globalPosition + vertialOffset);
_checks.push_back(check);
}
Check* Cell::RemoveCheck()
{
Check* returnValue = nullptr;
if (!IsRemoveCheckPossible())
return returnValue;
returnValue = _checks.back();
_checks.pop_back();
if (_checks.empty())
_status = CellStatus::Free;
return returnValue;
}
void Cell::ShowHint()
{
if (!IsHinted())
{
AddHint();
_isHintShown = true;
}
}
void Cell::HideHint()
{
if (IsHinted())
{
RemoveHint(_hintItr);
_isHintShown = false;
}
}
void Cell::AddHint()
{
AddCheck(_hintCheck);
auto itr = _checks.end();
std::advance(itr, -1);
_hintItr = itr;
}
void Cell::RemoveHint(std::list<Check*>::iterator itr)
{
if (!_checks.empty())
{
_checks.erase(itr);
return;
}
Debug::LogError("Cannot remove Hint from Cell with id: " + std::to_string(_id) + "\nContainer is empty");
return;
}