-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagboard.cpp
332 lines (291 loc) · 7.03 KB
/
tagboard.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "tagboard.h"
#include <stdexcept>
#include <cmath>
#include <set>
#include <iomanip>
void TagBoard::resize(std::size_t size)
{
_size = size;
_board.resize(size * size);
}
int &TagBoard::Value(TagBoard::Position cellPos)
{
if(cellPos.first >= _size || cellPos.second >= _size)
{
throw std::out_of_range("TagBoard::setCellValue out of range");
}
return _board[cellPos.first * _size + cellPos.second];
}
int &TagBoard::Value(std::size_t first, std::size_t second)
{
return Value(Position(first, second));
}
void TagBoard::swapCell(TagBoard::Position firstCell, TagBoard::Position secondCell)
{
std::swap(Value(firstCell), Value((secondCell)));
}
TagBoard::TagBoard(std::size_t size):
_size(size)
{
_board.resize(size * size);
}
TagBoard::TagBoard(const TagBoard &parent, TagBoard::Move move):
_size(parent._size),
_board(parent._board),
_emptyCellPos(parent._emptyCellPos)
{
makeMove(move);
}
bool TagBoard::makeMove(TagBoard::Move move)
{
std::pair<std::size_t, std::size_t> newPos = _emptyCellPos;
switch(move)
{
case top:
if(_emptyCellPos.first > 0)
{
newPos.first--;
}
break;
case bottom:
if(_emptyCellPos.first < (_size - 1))
{
newPos.first++;
}
break;
case left:
if(_emptyCellPos.second > 0)
{
newPos.second--;
}
break;
case right:
if(_emptyCellPos.second < (_size - 1))
{
newPos.second++;
}
break;
case notCorrect:
break;
}
if(newPos == _emptyCellPos)
{
return false;
}
swapCell(_emptyCellPos, newPos);
_emptyCellPos = newPos;
distanceToVictory = -1;
return true;
}
bool TagBoard::isCorrectMove(TagBoard::Move move) const
{
switch(move)
{
case top:
if(_emptyCellPos.first > 0)
{
return true;
}
break;
case bottom:
if(_emptyCellPos.first < (_size - 1))
{
return true;
}
break;
case left:
if(_emptyCellPos.second > 0)
{
return true;
}
break;
case right:
if(_emptyCellPos.second < (_size - 1))
{
return true;
}
break;
case notCorrect:
return false;
}
return false;
}
bool TagBoard::isSolutionExists() const
{
int inv = 0;
for(size_t i = 0; i < _board.size(); i++)
{
if(_board[i])
{
for (size_t j = 0; j < i; ++j)
if (_board[j] > _board[i])
++inv;
}
}
if(!(_size & 1))
{
inv += _emptyCellPos.first + 1;
}
return ((inv & 1) ? false : true);
}
int TagBoard::getDistanceToVictory() const
{
if(distanceToVictory != -1) {
return distanceToVictory;
}
distanceToVictory = 0;
for(std::size_t i = 0; i < _size; i++)
{
for(std::size_t j = 0; j < _size; j++)
{
distanceToVictory += getDistanceToCell(getValue(i, j), i, j);
}
}
return distanceToVictory;
}
int TagBoard::getDistanceToCell(int val, TagBoard::Position pos) const
{
return getDistanceToCell(val, pos.first, pos.second);
}
int TagBoard::getDistanceToCell(int val, std::size_t first, std::size_t second) const
{
if(val <= 0 || val >= (int)(_size * _size))
return 0;
val--;
const int correctFirst = val / _size;
const int correctSecond = val % _size;
const int divFirst = std::fabs(correctFirst - (int)first);
const int divSecond = std::fabs(correctSecond - (int)second);
return divFirst + divSecond;
}
int TagBoard::getValue(std::size_t first, std::size_t second) const
{
// if(first >= _size || second >= _size)
// {
// throw std::length_error("TagBoard::setCellValue out of range");
// }
return _board.at(first * _size + second);
}
bool TagBoard::operator ==(const TagBoard &tag) const
{
if(_size != tag._size)
return false;
for(Board::const_iterator it1 = _board.begin(), it2 = tag._board.begin();
it1 != _board.end(); it1++, it2++)
{
if((*it1) != (*it2))
return false;
}
return true;
}
bool TagBoard::operator !=(const TagBoard &tag) const
{
return !((*this) == tag);
}
bool TagBoard::isTurnBack(TagBoard::Move a, TagBoard::Move b)
{
switch (a) {
case left:
if(b == right)
return true;
break;
case right:
if(b == left)
return true;
break;
case top:
if(b == bottom)
return true;
break;
case bottom:
if(b == top)
return true;
case notCorrect:
return false;
break;
}
return false;
}
std::istream &operator >>(std::istream &in, TagBoard &tag)
{
std::size_t size;
in >> size;
const int maxValue = size * size;
std::set<int> checkDuplicate;
if(size != tag._size)
{
tag.resize(size);
}
TagBoard::Board::iterator it = tag._board.begin();
for(std::size_t i = 0; i < size; i++)
{
for(std::size_t j = 0; j < size; j++, it++)
{
int val;
in >> val;
if(val >= maxValue)
throw std::invalid_argument("TagBoard set wrong value: value more then Max Value");
if(checkDuplicate.find(val) == checkDuplicate.end())
checkDuplicate.insert(val);
else
throw std::invalid_argument("TagBoard set wrong value: replay value");
if(val == 0)
{
tag._emptyCellPos = TagBoard::Position(i, j);
}
(*it) = val;
}
}
tag.distanceToVictory = -1;
return in;
}
std::ostream &operator<<(std::ostream &out, const TagBoard& tag)
{
const std::size_t size = tag._size;
for(std::size_t i = 0; i < size; i++)
{
out << "| ";
for(std::size_t j = 0; j < size; j++)
{
int val = tag.getValue(i, j);
if(val)
out << std::setw(4) << val << " | ";
else
out << std::setw(4) << " " << " | ";
}
out << std::endl;
}
out << "Distance to victory is: " << tag.getDistanceToVictory() << std::endl;
return out;
}
std::ostream &operator<<(std::ostream &out, const TagBoard::Move& move)
{
switch(move)
{
case TagBoard::left:
out << "Move::Left";
break;
case TagBoard::right:
out << "Move::Right";
break;
case TagBoard::top:
out << "Move::Top";
break;
case TagBoard::bottom:
out << "Move::Bottom";
break;
case TagBoard::notCorrect:
out << "Move::not correct";
break;
}
return out;
}
std::size_t std::hash<TagBoard>::operator()(const TagBoard &tag) const
{
std::size_t result = 0, size = tag._size, multipler = 1;
for(const auto &value : tag._board) {
result += multipler * value;
multipler *= size;
}
return result;
}