-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
256 lines (221 loc) · 5.73 KB
/
game.cpp
File metadata and controls
256 lines (221 loc) · 5.73 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "game.h"
void first_seeds_generate(uint8_t one_pos_seeds_count) {
SerializebleSeeds data;
std::random_device rd;
std::mt19937 gen(rd());
uint16_t cnt_error = 0, cnt_mb_success = 0;
for (uint16_t diffic : {3}) {
std::cerr << "difficult: " << diffic << '\n';
for (uint16_t percent_to_delete : {30, 45, 70}) {
std::cerr << "percent_to_delete: " << percent_to_delete << '\n';
uint16_t current_cnt = 0;
while (current_cnt != one_pos_seeds_count) {
int seed = gen();
Field fd(diffic);
fd.random_fill(seed);
auto sudocu = fd.delete_random_cells(percent_to_delete, seed);
auto [sol_cnt, sol_table] = Field::get_solution(sudocu);
if (sol_cnt == 1) {
bool is_equal = std::equal(fd.field_.begin(),
fd.field_.end(), sol_table.begin());
if (!is_equal)
++cnt_error;
else {
++current_cnt;
data[diffic][percent_to_delete].push_front(seed);
}
++cnt_mb_success;
std::cerr << std::boolalpha
<< "is equal " << cnt_mb_success << ": " << is_equal << std::endl;
}
}
}
}
std::ofstream ofs("seeds.dump");
boost::archive::text_oarchive(ofs) << data;
}
//class Game
Game::Game() : seeds_([] {
SerializebleSeeds data;
std::ifstream ifs("seeds.dump");
boost::archive::text_iarchive(ifs) >> data;
return data;
}()), current_solved_field_(0){}
void Game::build_field(uint8_t difficult, uint8_t percent_to_delete) {
int valid_seed;
{auto access = seeds_.get_access();
auto& current_seeds = access.value_[difficult][percent_to_delete];
valid_seed = current_seeds.front();
current_seeds.pop_front();
}
current_solved_field_ = Field(difficult);
current_solved_field_.random_fill(valid_seed);
current_field_ = std::move(
current_solved_field_.delete_random_cells(percent_to_delete, valid_seed));
}
//Change text and background color.
template<int txt = 7, int bg = 0>
std::ostream& color(std::ostream & os) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, (WORD)((bg << 4) | txt));
return os;
}
//os = std::cout in current emplementation.
void Game::print_field(uint8_t i_idx, uint8_t j_idx) {
uint8_t difficulty = current_solved_field_.get_difficulty();
uint8_t current_num = current_field_[i_pos][j_pos];
auto print_line = [this]() {
uint8_t cnt = current_field_.size() + 1;
while (cnt--)
std::cout << "--";
std::cout << "-\n";
};
for (uint8_t i = 0; i < current_field_.size(); ++i) {
if (i % difficulty == 0) {
print_line();
}
for (uint8_t j = 0; j < current_field_.size(); ++j) {
//Make block sep.
if (j % difficulty == 0)
std::cout << '|';
//Marking if current cell or current num != 0.
if ( (i == i_idx && j == j_idx) ||
(current_num != 0 && current_field_[i][j] == current_num))
{
std::cout << color<0, 7>;
if (current_field_[i][j] != 0)
std::cout << static_cast<uint16_t>(current_field_[i][j]);
else
std::cout << '#';
std::cout << color << ' ';
}
//Deafult cell.
else
if (current_field_[i][j] != 0)
std::cout << static_cast<uint16_t>(current_field_[i][j]) << ' ';
else
std::cout << "# ";
}
std::cout << "\b|\n";
}
print_line();
}
bool Game::process_enter_button() {
std::cout << "Number: ";
uint16_t entered_num = static_cast<uint8_t>(-1);
std::cin >> entered_num;
while (!std::cin) {
std::cin.clear();
while (std::cin.get() != '\n');
std::cout << "Invalid input." << std::endl;
std::cout << "\nNumber: ";
std::cin >> entered_num;
}
if (entered_num != current_solved_field_[i_pos][j_pos]) {
std::cout << ">>Wrong<<" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
else {
current_field_[i_pos][j_pos] = entered_num;
std::cout << "<<Correct>>" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1)*0.7);
if (std::equal(current_field_.begin(), current_field_.end(),
current_solved_field_.begin()))
{
std::cout
<< "\n====\\Congratulations/====\n"
<< " You've won! "
<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
}
}
return true;
}
Game::MENUOPT Game::menu() {
uint8_t pos = 0;
const char* pos_s[] {
R"(
|========MENU========|
| <Continue> |
| Change game |
| Exit |
|====================|)",
R"(
|========MENU========|
| Continue |
| <Change game> |
| Exit |
|====================|)",
R"(
|========MENU========|
| Continue |
| Change game |
| <Exit> |
|====================|)"};
std::cout << pos_s[0];
bool is_in_menu = true;
while (is_in_menu) {
switch (_getch()) {
case 72://up
if (pos != 0)
--pos;
break;
case 80://down
if (pos != 2)
++pos;
break;
case 13://enter
return MENUOPT(pos);
}
std::system("cls");
std::cout << pos_s[pos];
}
return {};
}
void Game::loope() {
bool is_in_game = true;
MENUOPT menu_returning;
while (is_in_game) {
//std::cout << current_solved_field_ << std::endl << std::endl;
print_field(i_pos, j_pos);
switch (_getch()) {
case 72://up
if (i_pos != 0)
--i_pos;
break;
case 80://down
if (i_pos != current_field_.size() - 1)
++i_pos;
break;
case 75://left
if (j_pos != 0)
--j_pos;
break;
case 77://right
if (j_pos != current_field_.size() - 1)
++j_pos;
break;
case 13://enter
if (current_field_[i_pos][j_pos] == 0)
is_in_game = process_enter_button();
break;
case 27://escape
std::system("cls");
menu_returning = menu();
switch (menu_returning) {
case MENUOPT::Exit:
is_in_game = false;
break;
case MENUOPT::Change:
break;
case MENUOPT::Continue:
break;
}
break;
}
std::system("cls");
}
std::cout << "Game end." << std::endl;
}
//end class Game