/
Maze_Main.c
526 lines (402 loc) · 20.4 KB
/
Maze_Main.c
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
Name: Jared Adamson
Description: Modifcation of prior Maze, this time using recursion to produce maze.
*/
//INCLUDES
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include "Console_Util.h"
//MACROS (VS)
#pragma warning(disable: 4996)
//MACROS (CONSTANTS)
#define LEVEL_WIDTH CONSOLE_WIDTH //use entire width of console.
#define LEVEL_HEIGHT (CONSOLE_HEIGHT-1) //minus one, so one line free for text at bottom.
//set of ASCII characters to use as icons
#define ICON_BLANK ' '
#define ICON_WALL '#'
#define ICON_GOAL 'G'
#define ICON_PLAYER '!'
//maximum length of strings (including terminator).
#define MAX_STR_LEN 1024
//save file
#define SAVE_TIMES "times.dat"
//STRUCTURES
//stores player's position.
struct Vector
{
//in our coordinate system, the top left character is the origin at 0.0.
//x increases to the right while y increases downward.
int x;
int y;
};
//stores player's best time.
struct PlayerTime {
char name[MAX_STR_LEN];
float seconds;
struct PlayerTime* next;
};
//GLOBAL VARIABLES
struct PlayerTime* list_head = NULL;
//FORWARD DECLARATIONS
void menu();
void menu_selection(char option);
void besttimes_view();
void besttimes_add(char name[MAX_STR_LEN], float seconds);
void besttimes_load();
void besttimes_save();
void besttimes_free(struct PlayerTime* node);
void play();
void generate_level(char level[LEVEL_HEIGHT][LEVEL_WIDTH]);
void draw_level(char level[LEVEL_HEIGHT][LEVEL_WIDTH]);
int is_walkable(char level[LEVEL_HEIGHT][LEVEL_WIDTH], int x, int y);
void move_player(struct Vector* player_position, int move_x, int move_y);
void make_maze(char level[LEVEL_HEIGHT][LEVEL_WIDTH], int new_width, int new_height);
void draw_walls(char level[LEVEL_HEIGHT][LEVEL_WIDTH]);
//entry point
void main()
{
char input_buffer;
printf("\n\nWelcome to MAZE\n");
//load best times
besttimes_load();
//menu and input loop
do
{
menu();
input_buffer = tolower(getchar());
dump_key_buffer();
menu_selection(input_buffer);
}
while (input_buffer != 'q');
//save best times and free linked list
besttimes_save();
besttimes_free(list_head);
}
//displays a menu containing actions the user may choose.
void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("p: Play\n");
printf("v: View Best Times\n");
printf("q: Quit\n");
printf("\n\nPlease enter a choice ---> ");
}
//takes a character representing an inputs menu choice and calls the appropriate
//function to fulfill that choice. Display an error message if the character is
//not recognized.
void menu_selection(char option)
{
switch(option)
{
case 'p':
play();
break;
case 'v':
besttimes_view();
break;
case 'q':
// main loop will take care of this.
break;
default:
printf("\nError: Invalid Input. Please try again...");
break;
}
}
void besttimes_view()
{
struct PlayerTime* active_head = list_head;
printf("---BEST TIMES---\n");
if(active_head == NULL)
{
printf("No times yet!\n");
}
else
{
while(active_head != NULL)
{
printf("%f : %s\n", active_head->seconds, active_head->name);
active_head = active_head->next;
}
}
}
void besttimes_add(char name[MAX_STR_LEN], float seconds)
{
struct PlayerTime* active_head;
struct PlayerTime* new_node;
new_node = (struct PlayerTime*) malloc(sizeof(struct PlayerTime));
if (new_node == NULL)
{
printf("Failed to allocate memory for new best time, skipping.");
return;
}
strcpy(new_node->name, name);
new_node->seconds = seconds;
//need to insert at front if empty list or this is the smallested one
if(list_head == NULL || new_node->seconds < list_head->seconds)
{
new_node->next = list_head;
list_head = new_node;
}
//otherwise, need to insert somewhere in its tail.
else
{
//find sorted position
active_head = list_head;
while (active_head->next != NULL && new_node->seconds > active_head->next->seconds)
active_head = active_head->next;
new_node->next = active_head->next;
active_head->next = new_node;
}
}
void besttimes_load()
{
FILE* file;
char name[MAX_STR_LEN];
float seconds;
file = fopen(SAVE_TIMES, "r");
if(file != NULL)
{
while(fread(name, MAX_STR_LEN, 1, file) == 1)
{
fread(&seconds, sizeof(float), 1, file);
//reuse add function so we don't have to deal with the list
besttimes_add(name, seconds);
}
fclose(file);
}
}
void besttimes_save()
{
FILE* file;
struct PlayerTime* active_head = list_head;
file = fopen(SAVE_TIMES, "w");
if(file != NULL)
{
while(active_head != NULL)
{
fwrite(active_head->name, MAX_STR_LEN, 1, file);
fwrite(&active_head->seconds, sizeof(float), 1, file);
active_head = active_head->next;
}
fclose(file);
}
else
printf ("ERROR: Could not open file for saving data!\n");
}
void besttimes_free(struct PlayerTime* node)
{
if (node == NULL)
{
return;
}
else
{
besttimes_free(node->next);
free(node);
}
}
void initalize_maze(char level[LEVEL_HEIGHT][LEVEL_WIDTH]){
char walls[LEVEL_HEIGHT][LEVEL_WIDTH];
int x,y;
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
walls[y][x] = ' ';
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
level[y][x] = walls[y][x];
}
void initalize_boundry(char level[LEVEL_HEIGHT][LEVEL_WIDTH]){
char walls[LEVEL_HEIGHT][LEVEL_WIDTH];
int x,y;
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
walls[y][x] = level[y][x];
for (x = 0; x < LEVEL_WIDTH; x++)
walls[0][x] = '#';
for (x = 0; x < LEVEL_WIDTH; x++)
walls[LEVEL_HEIGHT - 1][x] = '#';
for (y = 0; y < LEVEL_HEIGHT; y++)
walls[y][0] = '#';
for (y = 0; y < LEVEL_HEIGHT; y++)
walls[y][LEVEL_WIDTH - 1] = '#';
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
level[y][x] = walls[y][x];
}
void make_maze(char level[LEVEL_HEIGHT][LEVEL_WIDTH], int new_width, int new_height){
int randomX = rand() % new_width ;
int randomY = rand() % (new_height);
int randdoorX = rand() % (new_width/2) ;
int randdoorY = rand() % (new_height/2) ;
int randdoorX2 = rand() % (new_width/2) ;
int randdoorY2 = rand() % (new_height/2) ;
int x,y;
char walls[LEVEL_HEIGHT][LEVEL_WIDTH];
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
walls[y][x] = level[y][x];
for (x = 0; x < new_width; x++)
walls[randomY][x] = '#';
walls[randomY][randdoorX ] = ' ';
walls[randomY][randdoorX2 + (new_width/2)] = ' ';
for (y = 0; y < new_height; y++)
walls[y][randomX] = '#';
walls[randdoorY + 1][randomX] = ' ';
walls[randdoorY2 + (new_height/2)][randomX] = ' ';
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
level[y][x] = walls[y][x];
if(new_width > 3 && new_height > 3){
new_width = new_width* 0.7;
new_height = new_height * 0.7;
make_maze(level, new_width, new_height);
}
}
void play()
{
//define the main 2d array. it has a size that is constant at compile time.
char level[LEVEL_HEIGHT][LEVEL_WIDTH];
//pick the goal to be the very last position in the map.
int goal_x = LEVEL_WIDTH-2;
int goal_y = LEVEL_HEIGHT-2;
//buffer for the key pressed. initialized to a dummy value so compiler doesn't complain.
char key_pressed = ' ';
//for loops
int i;
//time for player's start and end. and name. used for best times.
struct timeb start, end;
char name[MAX_STR_LEN];
//holds player's position, defaulted to 1,1.
struct Vector player_position;
player_position.x = 1;
player_position.y = 1;
//create outer level in the level 2D array.
//generate_level(level);
//display the newly created level on the screen.
console_activate();
initalize_maze(level);
make_maze(level, LEVEL_WIDTH , LEVEL_HEIGHT);
initalize_boundry(level);
draw_walls(level);
//draw the player - this is the result of calling move_player with a movement of 0,0.
move_player(&player_position, 0, 0);
//start the timer
ftime(&start);
while(key_pressed != 'q')
{
//waits for the user to press one key and then returns it.
key_pressed = get_input();
//handles player movement.
//for each case, we check if the position the player wants to move is walkable,
//then we move the player if it is possible.
switch(key_pressed)
{
case 'w':
if(is_walkable(level, player_position.x + 0, player_position.y - 1))
move_player(&player_position, 0, -1);
break;
case 'a':
if(is_walkable(level, player_position.x - 1, player_position.y + 0))
move_player(&player_position, -1, 0);
break;
case 's':
if(is_walkable(level, player_position.x + 0, player_position.y + 1))
move_player(&player_position, 0, 1);
break;
case 'd':
if(is_walkable(level, player_position.x + 1, player_position.y + 0))
move_player(&player_position, 1, 0);
break;
default:
//don't need to do anything.
break;
}
//check if the player is at the goal and react accordingly
if (player_position.x == goal_x && player_position.y == goal_y)
{
break;
}
}
ftime(&end);
//only add high score if player didn't force quit
if(key_pressed != 'q')
{
for(i = 0; i < CONSOLE_HEIGHT; i++)
print_string(0, i, " ");
print_string(0, 0, "COMPLETED! Enter name (name, no spaces, then press enter.): ");
get_string(name);
besttimes_add(name, (end.time - start.time) + (end.millitm - start.millitm) / 1000.0f);
}
console_deactivate();
//WARNING: it seems that on linux this doesn't show the name as its entered or restore the menu correctly.
}
//generates a new level with a goal.
void generate_level(char level[LEVEL_HEIGHT][LEVEL_WIDTH])
{
int x, y;
char data[LEVEL_HEIGHT][LEVEL_WIDTH] =
{{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'G', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
level[y][x] = data[y][x];
}
void draw_walls(char level[LEVEL_HEIGHT][LEVEL_WIDTH]){
int x;
int y;
level[LEVEL_HEIGHT -2][LEVEL_WIDTH - 2] = 'G';
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
print_char(x, y, (level[y][x]));
}
//draws the level in the console.
void draw_level(char level[LEVEL_HEIGHT][LEVEL_WIDTH])
{
int y, x;
for (y = 0; y < LEVEL_HEIGHT; y++)
for (x = 0; x < LEVEL_WIDTH; x++)
print_char(x, y, level[y][x]);
}
//returns 1 if a location is walkable and 0 otherwise. this is a c-style boolean.
int is_walkable(char level[LEVEL_HEIGHT][LEVEL_WIDTH], int x, int y)
{
if (level[y][x] == ICON_BLANK || level[y][x] == ICON_GOAL)
return 1;
else
return 0;
}
//moves the player on the draw by erasing and redrawing them. adjusts the player's
//position after drawing.
void move_player(struct Vector* player_position, int move_x, int move_y)
{
print_char(player_position->x, player_position->y, ICON_BLANK);
player_position->x += move_x;
player_position->y += move_y;
print_char(player_position->x, player_position->y, ICON_PLAYER);
//hack: move cursor out of the way
print_char(CONSOLE_WIDTH-2, CONSOLE_HEIGHT-1, ' ');
}