-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.h
454 lines (382 loc) · 9.25 KB
/
Header.h
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
char** Display;
char** DisplayBuffer;
typedef struct object {
char symbol;
COORD coord;
int blockType;
struct object* next;
} Object;
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
};
enum BlockType
{
BACKGROUND,
WALL,
SNAKE,
ITEM
};
typedef struct params {
double timeList[100];
int timeListLength;
int BACKGROUND_SIZE_X;
int BACKGROUND_SIZE_Y;
char BACKGROUND_SYMBOL;
char SNAKE_SYMBOL;
char WALL_SYMBOL;
char ITEM_SYMBOL;
} Params;
Params params;
Object* snake = NULL;
Object* item = NULL;
int point = 0;
Object* makeSnakeBlock(int, int);
Object* makeSnake(int, int);
Object* makeItem();
void init();
void initDisplay();
void loadParams();
void moveSnake(int, int);
void gotoxy(int, int);
void draw(int, int, char, int);
void drawBlock(Object*);
void drawBackground();
void drawSnake();
void drawItem();
int collide(Object*, int);
void displayPoint();
void update();
void lose();
void init()
{
loadParams();
initDisplay();
drawBackground();
point = 0;
snake = makeSnake((int)(params.BACKGROUND_SIZE_X / 2), (int)(params.BACKGROUND_SIZE_Y / 2));
item = makeItem();
update();
}
void loadParams()
{
FILE* settingFile = fopen("setting.ini", "r");
if (settingFile == NULL)
printf("NO FILE EXISTS\n");
char line[100];
params.BACKGROUND_SYMBOL = ' ';
while(fgets(line, 100, settingFile) != NULL)
{
char* ptr = strtok(line, " \n");
if (ptr == NULL) continue;
if (strcmp(ptr, "BACKGROUND_SIZE_X") == 0)
{
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
printf("\n설정 오류\n");
fprintf(stderr, "SETTING HAS NO BACKGROUND_SIZE_X ATTRIBUTE");
break;
}
printf("%s\n", ptr);
params.BACKGROUND_SIZE_X = atoi(ptr);
}
else if (strcmp(ptr, "BACKGROUND_SIZE_Y") == 0)
{
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
printf("\n설정 오류\n");
fprintf(stderr, "SETTING HAS NO BACKGROUND_SIZE_Y ATTRIBUTE");
break;
}
params.BACKGROUND_SIZE_Y = atoi(ptr);
}
else if (strcmp(ptr, "WALL_SYMBOL") == 0)
{
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
printf("\n설정 오류\n");
fprintf(stderr, "SETTING HAS NO WALL_SYMBOL ATTRIBUTE");
break;
}
params.WALL_SYMBOL = ptr[0];
}
else if (strcmp(ptr, "SNAKE_SYMBOL") == 0)
{
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
printf("\n설정 오류\n");
fprintf(stderr, "SETTING HAS NO SNAKE_SYMBOL ATTRIBUTE");
break;
}
else {
params.SNAKE_SYMBOL = ptr[0];
}
}
else if (strcmp(ptr, "ITEM_SYMBOL") == 0)
{
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
printf("\n설정 오류\n");
fprintf(stderr, "SETTING HAS NO ITEM_SYMBOL ATTRIBUTE");
break;
}
else {
params.ITEM_SYMBOL = ptr[0];
}
}
else if (strcmp(ptr, "SPEED") == 0) {
//printf("A");
params.timeListLength = 0;
while(1)
{
//printf("%d\n", params.timeListLength);
ptr = strtok(NULL, " \n");
if (ptr == NULL) {
break;
}
else {
params.timeList[params.timeListLength] = atof(ptr);
params.timeListLength++;
}
}
}
else
{
continue;
}
}
}
void initDisplay()
{
Display = (char**)malloc(sizeof(char*) * params.BACKGROUND_SIZE_X);
DisplayBuffer = (char**)malloc(sizeof(char*) * params.BACKGROUND_SIZE_X);
for (int i = 0; i < params.BACKGROUND_SIZE_X; i++)
{
Display[i] = (char*)malloc(sizeof(char) * params.BACKGROUND_SIZE_Y);
DisplayBuffer[i] = (char*)malloc(sizeof(char) * params.BACKGROUND_SIZE_Y);
for (int j = 0; j < params.BACKGROUND_SIZE_Y; j++)
{
Display[i][j] = DisplayBuffer[i][j] = BACKGROUND;
}
}
}
void drawBackground()
{
for (int i = 0; i < params.BACKGROUND_SIZE_X; i++)
{
for (int j = 0; j < params.BACKGROUND_SIZE_Y; j++)
{
if (i == 0 || i == params.BACKGROUND_SIZE_X - 1 || j == 0 || j == params.BACKGROUND_SIZE_Y - 1)
{
draw(i, j, params.WALL_SYMBOL, WALL);
}
else
{
draw(i, j, ' ', 0);
}
}
}
}
Object* makeItem()
{
if (item == NULL)
{
item = (Object*)malloc(sizeof(Object));
}
srand((unsigned int)time(NULL));
int x, y;
do {
x = rand() % (params.BACKGROUND_SIZE_X - 1) + 1;
y = rand() % (params.BACKGROUND_SIZE_Y - 1) + 1;
} while (DisplayBuffer[x][y]);
item->coord.X = x;
item->coord.Y = y;
item->symbol = params.ITEM_SYMBOL;
item->blockType = ITEM;
item->next = NULL;
return item;
}
Object* makeSnakeBlock(int x, int y)
{
Object* head = (Object*)malloc(sizeof(Object));
head->symbol = params.SNAKE_SYMBOL;
head->coord.X = x;
head->coord.Y = y;
head->blockType = SNAKE;
head->next = NULL;
return head;
}
Object* makeSnake(int x, int y)
{
Object* head = makeSnakeBlock(x, y);
Object* body = makeSnakeBlock(x - 1, y);
head->next = body;
return head;
}
void moveSnake(int dir, int expand)
{
COORD currentCoord, nextCoord;
currentCoord.X = snake->coord.X;
currentCoord.Y = snake->coord.Y;
switch (dir)
{
case UP:
snake->coord.Y--;
break;
case DOWN:
snake->coord.Y++;
break;
case LEFT:
snake->coord.X--;
break;
case RIGHT:
snake->coord.X++;
break;
default:
break;
}
Object* currentBlock = snake;
while (currentBlock->next != NULL)
{
nextCoord.X = currentBlock->next->coord.X;
nextCoord.Y = currentBlock->next->coord.Y;
currentBlock->next->coord.X = currentCoord.X;
currentBlock->next->coord.Y = currentCoord.Y;
currentCoord.X = nextCoord.X;
currentCoord.Y = nextCoord.Y;
currentBlock = currentBlock->next;
}
if (expand)
{
Object* newBlock = makeSnakeBlock(currentCoord.X, currentCoord.Y);
currentBlock->next = newBlock;
}
}
void gotoxy(int x, int y)
{
COORD Pos;
Pos.X = x;
Pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void draw(int x, int y, char symbol, int filling)
{
DisplayBuffer[x][y] = filling;
}
void drawBlock(Object* block)
{
draw(block->coord.X, block->coord.Y, block->symbol, block->blockType);
}
void drawSnake()
{
Object* block = snake;
while (block != NULL)
{
drawBlock(block);
block = block->next;
}
}
void drawItem()
{
drawBlock(item);
}
int collide(Object* snake, int dir)
{
COORD coord;
coord.X = snake->coord.X;
coord.Y = snake->coord.Y;
switch (dir)
{
case UP:
coord.Y--;
break;
case DOWN:
coord.Y++;
break;
case LEFT:
coord.X--;
break;
case RIGHT:
coord.X++;
break;
}
return DisplayBuffer[coord.X][coord.Y];
}
void remove_snake()
{
Object* snakeBlock = snake;
while (snakeBlock != NULL)
{
Object* nextBlock = snakeBlock->next;
free(snakeBlock);
snakeBlock = nextBlock;
}
snake = NULL;
}
void remove_item()
{
if (item != NULL)
free(item);
item = NULL;
}
void remove_filling()
{
for (int i = 0; i < params.BACKGROUND_SIZE_X; i++)
{
for (int j = 0; j < params.BACKGROUND_SIZE_Y; j++)
{
DisplayBuffer[i][j] = BACKGROUND;
}
}
}
void displayPoint()
{
gotoxy(params.BACKGROUND_SIZE_X + 2, 0);
printf("Point: %d ", point);
}
void flushDisplayBuffer()
{
for (int i = 0; i < params.BACKGROUND_SIZE_X; i++)
{
for (int j = 0; j < params.BACKGROUND_SIZE_Y; j++)
{
if (Display[i][j] != DisplayBuffer[i][j])
{
gotoxy(i, j);
switch (DisplayBuffer[i][j])
{
case BACKGROUND:
printf("%c", params.BACKGROUND_SYMBOL);
break;
case WALL:
printf("%c", params.WALL_SYMBOL);
break;
case SNAKE:
printf("%c", params.SNAKE_SYMBOL);
break;
case ITEM:
printf("%c", params.ITEM_SYMBOL);
break;
}
Display[i][j] = DisplayBuffer[i][j];
}
}
}
}
void update()
{
drawBackground();
drawSnake();
drawItem();
displayPoint();
flushDisplayBuffer();
}