forked from mayerui/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
370 lines (324 loc) · 8.72 KB
/
scene.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <cmath>
#include <iostream>
#include <fstream>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <vector>
#include "common.h"
#include "scene.h"
#include "utility.inl"
CScene::CScene(int index)
: _index(index),
_max_column(pow(index, 2)),
_cur_point({0, 0})
{
init();
}
CScene::~CScene()
{
}
void CScene::show() const
{
cls();
printUnderline();
for (int row = 0; row < _max_column; ++row)
{
CBlock block = _row_block[row];
block.print();
printUnderline(row);
}
}
void CScene::printUnderline(int line_no) const
{
std::string underline;
for (int colunm = 0; colunm < 9; ++colunm)
{
if (_cur_point.y == line_no && _cur_point.x == colunm)
underline += "--^-";
else
underline += "----";
}
underline += '-';
for (int i = 0; i < 4; i++) {
underline[i * 12] = ((line_no + 1) % 3 == 0) ? '+' : '|';
}
std::cout << underline.c_str() << std::endl;
}
void CScene::init()
{
memset(_map, UNSELECTED, sizeof _map);
// column_block 所有列
for (int col = 0; col < _max_column; ++col)
{
CBlock column_block;
for (int row = 0; row < _max_column; ++row)
{
column_block.push_back(_map + row * 9 + col);
}
_column_block[col] = column_block;
}
// row_block 所有行
for (int row = 0; row < _max_column; ++row)
{
CBlock row_block;
for (int col = 0; col < _max_column; ++col)
{
row_block.push_back(_map + row * 9 + col);
}
_row_block[row] = row_block;
}
// xy_block 所有九宫格, [行][列]
for (int block_index = 0; block_index < _max_column; ++block_index)
{
CBlock xy_block;
int xy_begin = block_index / 3 * 27 + block_index % 3 * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
xy_block.push_back(_map + xy_begin + i * 9 + j);
}
}
_xy_block[block_index / 3][block_index % 3] = xy_block;
}
return;
}
bool CScene::setCurValue(const int nCurValue, int &nLastValue)
{
auto point = _map[_cur_point.x + _cur_point.y * 9];
if (point.state == State::ERASED)
{
nLastValue = point.value;
setValue(nCurValue);
return true;
}
else
return false;
}
void CScene::setValue(const point_t& p, const int value)
{
_map[p.x + p.y * 9].value = value;
}
void CScene::setValue(const int value)
{
auto p = _cur_point;
this->setValue(p, value);
}
// 选择count个格子清空
void CScene::eraseRandomGrids(const int count)
{
point_value_t p = {UNSELECTED, State::ERASED};
std::vector<int> v(81);
for (int i = 0; i < 81; ++i) {
v[i] = i;
}
for (int i = 0; i < count; ++i) {
int r = random(0, v.size() - 1);
_map[v[r]] = p;
v.erase(v.begin() + r);
}
}
bool CScene::isComplete()
{
// 任何一个block未被填满,则肯定未完成
for (size_t i = 0; i < 81; ++i)
{
if (_map[i].value == UNSELECTED)
return false;
}
// 同时block里的数字还要符合规则
for (int row = 0; row < 9; ++row)
{
for (int col = 0; col < 9; ++col)
{
if (!_row_block[row].isValid() ||
!_column_block[col].isValid() ||
!_xy_block[row / 3][col / 3].isValid())
return false;
}
}
return true;
}
void CScene::save(const char *filename) {
std::fstream fs;
// TODO: check whether the file has existed
fs.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// save _map
for (int i = 0; i < 81; i++) {
fs << _map[i].value << ' ' << static_cast<int>(_map[i].state) << std::endl;
}
// save _cur_point
fs << _cur_point.x << ' ' << _cur_point.y << std::endl;
// save _vCommand
fs << _vCommand.size() << std::endl;
for (CCommand command : _vCommand) {
point_t point = command.getPoint();
fs << point.x << ' ' << point.y << ' '
<< command.getPreValue() << ' '
<< command.getCurValue() << std::endl;
}
fs.close();
}
void CScene::load(const char *filename) {
std::fstream fs;
// TODO: check whether the file has existed
fs.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// load _map
for (int i = 0; i < 81; i++) {
int tmpState;
fs >> _map[i].value >> tmpState;
_map[i].state = static_cast<State>(tmpState);
}
// load _cur_point
fs >> _cur_point.x >> _cur_point.y;
// load _vCommand
int commandSize;
fs >> commandSize;
for (int i = 0; i < commandSize; i++) {
point_t point;
int preValue, curValue;
fs >> point.x >> point.y >> preValue >> curValue;
_vCommand.emplace_back(this, point, preValue, curValue);
}
}
void CScene::play()
{
show();
char key = '\0';
while (1)
{
key = getch();
if (key >= '0' && key <= '9')
{
CCommand oCommand(this);
if (!oCommand.execute(key - '0'))
{
std::cout << "this number can't be modified." << std::endl;
}
else
{
_vCommand.push_back(std::move(oCommand)); // XXX: move without move constructor
show();
continue;
}
}
switch (key)
{
case 0x1B: // ESC
{
std::cout << "quit game ? [Y/N]" << std::endl;
std::string strInput;
std::cin >> strInput;
if (strInput[0] == 'y' || strInput[0] == 'Y') {
std::cout << "do you want to save the game progress ? [Y/N]" << std::endl;
std::cin >> strInput;
if (strInput[0] == 'y' || strInput[0] == 'Y') {
std::cout << "input path of the progress file: ";
std::cin >> strInput;
save(strInput.c_str());
}
exit(0);
}
else
{
std::cout << "continue." << std::endl;
break;
}
}
case 0x75: // u
{
if (_vCommand.empty())
std::cout << "no more action to undo." << std::endl;
else
{
CCommand &oCommand = _vCommand.back();
oCommand.undo();
_vCommand.pop_back();
show();
}
break;
}
case 0x61: // a
_cur_point.x = (_cur_point.x - 1) < 0 ? 0 : _cur_point.x - 1;
show();
break;
case 0x64: // d
_cur_point.x = (_cur_point.x + 1) > 8 ? 8 : _cur_point.x + 1;
show();
break;
case 0x73: // s
_cur_point.y = (_cur_point.y + 1) > 8 ? 8 : _cur_point.y + 1;
show();
break;
case 0x77: // w
_cur_point.y = (_cur_point.y - 1) < 0 ? 0 : _cur_point.y - 1;
show();
break;
case 0x0D: // enter
if (isComplete())
{
std::cout << "congratulation! you win!" << std::endl;
getchar();
exit(0);
}
else
{
std::cout << "sorry, not completed." << std::endl;
}
break;
default:
break;
}
}
}
// 一个场景可以多次被初始化
void CScene::generate()
{
// XXX: pseudo random
static char map_pattern[10][10] = {
"ighcabfde",
"cabfdeigh",
"fdeighcab",
"ghiabcdef",
"abcdefghi",
"defghiabc",
"higbcaefd",
"bcaefdhig",
"efdhigbca"};
std::vector<char> v = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
// 产生字母到数字的随机映射
std::unordered_map<char, int> hash_map;
for (int i = 1; i <= 9; ++i)
{
int r = random(0, v.size() - 1);
hash_map[v[r]] = i;
v.erase(v.begin() + r);
}
// 填入场景
for (int row = 0; row < 9; ++row)
{
for (int col = 0; col < 9; ++col)
{
point_t point = {row, col};
char key = map_pattern[row][col];
setValue(point, hash_map[key]);
}
}
assert(isComplete());
return;
}
bool CScene::setPointValue(const point_t &stPoint, const int nValue)
{
auto point = _map[stPoint.x + stPoint.y * 9];
if (State::ERASED == point.state)
{
_cur_point = stPoint;
setValue(nValue);
return true;
}
else
return false;
}
point_t CScene::getCurPoint()
{
return _cur_point;
}