-
Notifications
You must be signed in to change notification settings - Fork 3
/
Source.cpp
366 lines (310 loc) · 8.66 KB
/
Source.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
#include "Header.hpp"
void System::internal_hide_consol_cursor()
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
bool showFlag = false;
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = showFlag; // set the cursor visibility
SetConsoleCursorInfo(out, &cursorInfo);
}
string System::internal_get_file_content(const string path)
{
/*
Utiliza um iterator - ponteiros para enderecos de memoria- para construir uma string com os dados da ifstream - input file stream.
- istreambuf_iterator<char>(file)) = iterator para o inicio do arquivo
- istreambuf_iterator<char>() = iterator para o final do arquivo
*/
ifstream file("Map.txt"); // Ponteiro para o local do arquivo
string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
return content;
}
int System::internal_generate_ramdom_number()
{
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 mt_rand(seed);
return mt_rand();
}
int System::internal_set_x(int one_dimension_position)
{
return one_dimension_position % Map::get_width();
}
void System::internal_gotoxy(short x, short y)
{
COORD pos = {x, y};
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, pos);
}
void System::internal_go_to_console_position(int one_dimension_position)
{
System::gotoxy(one_dimension_position % Map::get_width(), one_dimension_position / Map::get_width());
}
COORD System::internal_getxy(CONSOLE_SCREEN_BUFFER_INFO *csbi)
{
COORD coord = csbi->dwCursorPosition;
return coord;
}
char System::internal_get_cursor_char()
{
char c = '\0';
CONSOLE_SCREEN_BUFFER_INFO con;
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
if (hcon != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hcon, &con))
{
DWORD read = 0;
if (!ReadConsoleOutputCharacterA(hcon, &c, 1, con.dwCursorPosition, &read) || read != 1)
c = '\0';
}
return c;
}
int System::internal_set_y(int one_dimension_position)
{
return one_dimension_position / Map::get_width();
}
Timer::Timer()
{
this->clock = 0;
}
void Timer::start()
{
m_StartTime = std::chrono::system_clock::now();
m_bRunning = true;
}
void Timer::stop()
{
m_EndTime = std::chrono::system_clock::now();
m_bRunning = false;
}
double Timer::elapsedMilliseconds()
{
std::chrono::time_point<std::chrono::system_clock> endTime;
if (m_bRunning)
endTime = std::chrono::system_clock::now();
else
endTime = m_EndTime;
return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - m_StartTime).count();
}
Timer Timer::update()
{
stop();
clock = clock + elapsedMilliseconds();
return *this;
}
Timer Timer::print()
{
printf("%0.1f\n", clock / 1000.0);
return *this;
}
string Map::canvas;
Map::Map() : width(calculate_width()), height(calculate_height()) {}
int Map::calculate_width() { return (canvas.find('\n') + 1); }
int Map::calculate_height() { return (canvas.size() / width) + 1; }
int Map::internal_get_width() { return width; }
int Map::internal_get_height() { return height; }
void Map::internal_print()
{
System::gotoxy(0, 0);
cout << GRAY_CHAR << Map::canvas << RESET_COLOR_SCHEME;
}
void print_score(int map_height, int score, int lives)
{
System::gotoxy(1, map_height + 2);
cout << "Score: " << score << endl;
System::gotoxy(1, map_height + 3);
cout << "Lives: " << lives << endl;
}
void print_colour_scheme()
{
int i, j, n;
for (i = 0; i < 11; i++)
{
for (j = 0; j < 10; j++)
{
n = 10 * i + j;
if (n > 108)
break;
printf("\033[%dm %3d\033[m", n, n);
}
printf("\n");
}
}
void set_console_size()
{
char str[11];
sprintf(str, "MODE %d, %d", Map::get_width(), Map::get_height() + 5);
system(str);
}
void test_sound()
{
int f, t;
while (!GetAsyncKeyState('Q'))
{
if (GetAsyncKeyState('V'))
{
fflush(stdin);
cin >> f;
}
if (GetAsyncKeyState('P'))
Beep(f, 100);
}
//532Hz, 500ms
//cout << s_AllocCount << " allocations\n";
//220Hz good for menu
}
void print_information(int lives, int score, Timer &timer)
{
const int map_height = Map::get_height();
const int map_width = Map::get_width();
for (int i = 0; i < 3; i++)
{
System::gotoxy(0, map_height + i);
for (int j = 0; j < map_width - 1; j++)
{
if (i != 0 && i != 1)
{
cout << GRAY_CHAR << '#' << RESET_COLOR_SCHEME;
}
else
{
System::gotoxy(0, map_height + i);
cout << GRAY_CHAR << "##" << RESET_COLOR_SCHEME;
System::gotoxy(map_width - 3, map_height + i);
cout << GRAY_CHAR << "##" << RESET_COLOR_SCHEME;
}
}
printf("\n");
}
System::gotoxy(4, map_height);
printf("LIVES");
System::gotoxy(6, map_height + 1);
printf("%d", lives);
System::gotoxy(24, map_height);
printf("SCORE");
if (score < 100)
System::gotoxy(26, map_height + 1);
else
System::gotoxy(26 - (calculate_digits(score) - 3), map_height + 1);
printf("%d", score);
System::gotoxy(map_width - 8, map_height);
printf("TIME");
System::gotoxy(map_width - 8, map_height + 1);
timer.update().print();
/*if (timer < 100.0)
printf("%0.1f", timer);
else
printf("%d", int(timer));*/
}
void show_consol_cursor(bool showFlag)
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = showFlag; // set the cursor visibility
SetConsoleCursorInfo(out, &cursorInfo);
}
int calculate_digits(int val)
{
int i = 0;
while (val)
{
val = val / 10;
i++;
}
return i;
}
void System::internal_welcome_screen(char &choice)
{
system("cls");
cout << "Snake Game\n";
cout << "by Vitor Matias\n\n";
cout << "1-New Game\n";
cout << "2-Continue\n";
cout << "3-Records\n";
cout << "4-Quit\n";
while (!GetAsyncKeyState('Q'))
{
if (_kbhit())
{
choice = toupper(_getch());
imprimir_selecao_menu(choice);
}
}
}
void imprimir_selecao_menu(char &choice)
{
switch (choice)
{
case '1':
{
system("cls");
cout << "Snake Game\n";
cout << "by Vitor Matias\n\n";
cout << MENU_SELECTOR << "1-New Game\n"<< RESET_COLOR_SCHEME;
cout << "2-Continue\n";
cout << "3-Records\n";
cout << "4-Quit\n";
if(_kbhit())
break;
}
case '2':
{
system("cls");
cout << "Snake Game\n";
cout << "by Vitor Matias\n\n";
cout << "1-New Game\n";
cout << MENU_SELECTOR << "2-Continue\n"<< RESET_COLOR_SCHEME;
cout << "3-Records\n";
cout << "4-Quit\n";
break;
}
case '3':
{
system("cls");
cout << "Snake Game\n";
cout << "by Vitor Matias\n\n";
cout << "1-New Game\n";
cout << "2-Continue\n";
cout << MENU_SELECTOR << "3-Records\n"<< RESET_COLOR_SCHEME;
cout << "4-Quit\n";
break;
}
case '4':
{
system("cls");
cout << "Snake Game\n";
cout << "by Vitor Matias\n\n";
cout << "1-New Game\n";
cout << "2-Continue\n";
cout << "3-Records\n";
cout << MENU_SELECTOR << "4-Quit\n"<< RESET_COLOR_SCHEME;
break;
}
case '\n':
default:
break;
}
}
void System::internal_write_file()
{
ofstream myfile("Saves/example.txt");
if (myfile.is_open())
{
myfile << Map::canvas;
myfile.close();
}
else
cout << "Unable to open file";
}
int System::internal_convert_coord_to_one_dimendion(const int x, const int y)
{
return y * Map::get_width() + x;
}
void System::internal_update_map_to_print(int fruit_position){
list<int> tail = Tail::get_tail();
tail.pop_back();
Map::canvas.replace(Head::get_position(), 1, "0");
for(int node : tail){
Map::canvas.replace(node, 1, "o");
}
Map::canvas.replace(fruit_position, 1, "*");
}