-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
553 lines (545 loc) · 11.9 KB
/
main.c
File metadata and controls
553 lines (545 loc) · 11.9 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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "utils.h"
int main()
{
curs_set(0);
int row, col, ch, y = 1, x = 1;
char ctrls[] = "MOVEMENT: wasd, MAKE WALL: h, MAKE ENEMY: j, MAKE MOVEABLE BALL: k, MAKE TARGET: l, START LEVEL: q";
int i, j;
int walcnt = 1;
int enecnt = 1;
int tarcnt = 1;
int tardon = 0;
int movcnt = 1;
int* waly = malloc(sizeof(int));
int* walx = malloc(sizeof(int));
int* movy = malloc(sizeof(int));
int* movx = malloc(sizeof(int));
int* eney = malloc(sizeof(int));
int* enex = malloc(sizeof(int));
int* enediff = malloc(sizeof(int));
int* tary = malloc(sizeof(int));
int* tarx = malloc(sizeof(int));
srandom(time(NULL));
initscr();
raw();
palette(); //initalizes colors
printw("\nHow wide would you like your window to be?\n");
scanw("%d", &col);
printw("\nHow long would you like your window to be?\n");
scanw("%d", &row);
if(row > LINES || col > COLS)
{
endwin();
fprintf(stderr, "\nVALUES ENTERED WERE TOO LARGE: MAX WIDTH = %d, MAX LENGTH = %d\n", COLS, LINES);
return -1;
}
mvprintw(LINES - 1, (COLS - (strlen(ctrls))) / 2, "%s", ctrls);
WINDOW * mywin = newwin(row, col, (LINES - row) / 2, (COLS - col) / 2); //creates window based on user input if the user tries to make the box too large the program will stall indefinitely
refresh();
while(ch != 'q')
{
curs_set(0);
ch = wgetch(mywin);
wclear(mywin);
wrefresh(mywin);
if(ch == 'w' || ch == 'a' || ch == 's' || ch == 'd')
{
mvmnt(row, col, &y, &x, ch);
}
switch(ch)
{
case 'h' :
walx = realloc(walx, sizeof(int)*walcnt); //this function dynamically allocated memory for wall positions
walx[walcnt - 1] = x;
waly = realloc(waly, sizeof(int)*walcnt);
waly[walcnt - 1] = y;
walcnt++;
break;
case 'j' :
curs_set(1);
mvwprintw(stdscr, 7, 0, "How difficult do you want it to be? 1-5: ");
wscanw(stdscr, "%d", (enediff + (enecnt -1)));
curs_set(0);
enex = realloc(enex, sizeof(int)*enecnt); //this function dynamically allocates memory for enemy positions and movement directions
enex[enecnt - 1] = x;
eney = realloc(eney, sizeof(int)*enecnt);
eney[enecnt - 1] = y;
enecnt++;
break;
case 'k' :
movx = realloc(movx, sizeof(int)*movcnt); //this function dynamically allocated memory for wall positions
movx[movcnt - 1] = x;
movy = realloc(movy, sizeof(int)*movcnt);
movy[movcnt - 1] = y;
movcnt++;
break;
case 'l' :
tarx = realloc(tarx, sizeof(int)*tarcnt); //this function dynamically allocated memory for wall positions
tarx[tarcnt - 1] = x;
tary = realloc(tary, sizeof(int)*tarcnt);
tary[tarcnt - 1] = y;
tarcnt++;
break;
}
wattron(mywin, COLOR_PAIR(6));
box(mywin, 0, 0);
wattroff(mywin, COLOR_PAIR(6));
wattron(mywin, COLOR_PAIR(4));
mvwprintw(mywin, y, x, "#");
wattroff(mywin, COLOR_PAIR(4));
wattron(mywin, COLOR_PAIR(2));
for(i = 0; i < walcnt - 1; i++)
{
mvwprintw(mywin, waly[i], walx[i], "&");
}
wattroff(mywin, COLOR_PAIR(2));
wattron(mywin, COLOR_PAIR(5));
for(i = 0; i < enecnt - 1; i++)
{
mvwprintw(mywin, eney[i], enex[i], "$");
}
wattroff(mywin, COLOR_PAIR(5));
wattron(mywin, COLOR_PAIR(3));
for(i = 0; i < movcnt - 1; i++)
{
mvwprintw(mywin, movy[i], movx[i], "@");
}
wattroff(mywin, COLOR_PAIR(3));
wattron(mywin, COLOR_PAIR(1));
for(i = 0; i < tarcnt - 1; i++)
{
mvwprintw(mywin, tary[i], tarx[i], "X");
}
wattroff(mywin, COLOR_PAIR(1));
wrefresh(mywin);
}
wclear(mywin);
ch = '\0';
while(ch != 'q')
{
halfdelay(2);
ch = wgetch(mywin);
wclear(mywin);
wrefresh(mywin);
mvmnt(row, col, &y, &x, ch);
wallcheck(&y, waly, &x, walx, walcnt, ch);
movcheck(&y, movy, &x, movx, ch, row, col, movcnt);
for(i = 0; i < movcnt - 1; i++)
{
wallcheck(movy + i, waly, movx + i, walx, walcnt, ch);
}
compcheck(tary, tarx, &tardon, tarcnt, movy, movx, movcnt);
chase(y, waly, x, walx, eney, enex, enediff, enecnt, walcnt, (const int)col);
wattron(mywin, COLOR_PAIR(6));
box(mywin, 0, 0);
wattroff(mywin, COLOR_PAIR(6));
wattron(mywin, COLOR_PAIR(4));
mvwprintw(mywin, y, x, "#");
wattroff(mywin, COLOR_PAIR(4));
wattron(mywin, COLOR_PAIR(2));
for(i = 0; i < walcnt - 1; i++)
{
mvwprintw(mywin, waly[i], walx[i], "&");
}
wattroff(mywin, COLOR_PAIR(2));
wattron(mywin, COLOR_PAIR(5));
for(i = 0; i < enecnt - 1; i++)
{
mvwprintw(mywin, eney[i], enex[i], "$");
}
wattroff(mywin, COLOR_PAIR(5));
wattron(mywin, COLOR_PAIR(3));
for(i = 0; i < movcnt - 1; i++)
{
mvwprintw(mywin, movy[i], movx[i], "@");
}
wattroff(mywin, COLOR_PAIR(3));
wattron(mywin, COLOR_PAIR(1));
for(i = 0; i < tarcnt - 1; i++)
{
mvwprintw(mywin, tary[i], tarx[i], "X");
}
wattroff(mywin, COLOR_PAIR(1));
for(i = 0; i < enecnt - 1; i++)
{
if(eney[i] == y && enex[i] == x)
{
cbreak();
wclear(mywin);
wattron(mywin, COLOR_PAIR(5));
mvwprintw(mywin, row / 2, col / 2, "YOU LOSE");
wattroff(mywin, COLOR_PAIR(5));
wgetch(mywin);
ch = 'q';
}
}
if(tardon == tarcnt - 1 && tarcnt - 1 != 0)
{
cbreak();
wclear(mywin);
wattron(mywin, COLOR_PAIR(1));
mvwprintw(mywin, row / 2, col / 2, "YOU WIN");
wattroff(mywin, COLOR_PAIR(1));
wgetch(mywin);
ch = 'q';
}
else
{
tardon = 0;
}
wrefresh(mywin);
}
free(waly);
free(walx);
free(eney);
free(enex);
free(enediff);
free(movy);
free(movx);
free(tary);
free(tarx);
endwin();
return 0;
}
void mvmnt(int row, int col, int *y, int *x, int ch)
{
switch(ch) //implements basic character movement based on wasd user input
{
case 'w' :
(*y)--;
if(*y < 1) //this if statement and similar following ones stop the character from leaving the screen
{
(*y)++;
}
break;
case 'a' :
(*x)--;
if(*x < 1)
{
(*x)++;
}
break;
case 's' :
(*y)++;
if(*y > row - 2)
{
(*y)--;
}
break;
case 'd' :
(*x)++;
if(*x > col - 2)
{
(*x)--;
}
break;
}
}
void palette()
{
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_MAGENTA, COLOR_BLACK);
init_pair(3, COLOR_CYAN, COLOR_BLACK);
init_pair(4, COLOR_YELLOW, COLOR_BLACK);
init_pair(5, COLOR_RED, COLOR_BLACK);
init_pair(6, COLOR_BLUE, COLOR_BLACK);
}
void chase(int y, int* waly, int x, int* walx, int* eney, int* enex, int* enediff, int enecnt, int walcnt, const int col)
{
int i, j, randtim, randmv;
for(i = 0; i < enecnt - 1; i++)
{
randmv = random() % 4;
randtim = random() % (7 - (enediff[i])); //random movement timer of enemy decided based on user defined difficulty
if(randtim == 0 && abs(y-(eney[i])) + abs(x-(enex[i])) < col)
{
if(abs(y-(eney[i])) > abs(x-(enex[i])))
{
if(y > eney[i])
{
(eney[i])++;
for(j = 0; j < walcnt - 1; j++) //implements wall collisions for enemys
{
if(eney[i] == waly[j] && enex[i] == walx[j])
{
eney[i]--;
if(random() % 3 == 0)
{
switch(randmv) //adds occasional random movement if enemy runs it a wall to prevent them from getting trapped sometimes allows them to phase through walls
{
case 0 :
enex[i]--;
break;
case 1 :
enex[i]++;
break;
case 2 :
eney[i]--;
break;
}
}
if(eney[i] == waly[j] && enex[i] == walx[j])
{
switch(randmv)
{
case 0 :
enex[i]++;
break;
case 1 :
enex[i]--;
break;
case 2 :
eney[i]++;
break;
}
}
j = walcnt - 1; //this statement and similar one stop the for loop from continuing to check something if that thing can only happen once and has already happened
}
}
}
else
{
(eney[i])--;
for(j = 0; j < walcnt - 1; j++)
{
if(eney[i] == waly[j] && enex[i] == walx[j])
{
eney[i]++;
if(random() % 3 == 0)
{
switch(randmv)
{
case 0 :
enex[i]--;
break;
case 1 :
enex[i]++;
break;
case 2 :
eney[i]++;
break;
}
}
if(eney[i] == waly[j] && enex[i] == walx[j])
{
switch(randmv)
{
case 0 :
enex[i]++;
break;
case 1 :
enex[i]--;
break;
case 2 :
eney[i]--;
break;
}
}
j = walcnt - 1;
}
}
}
}
else
{
if(x > enex[i])
{
(enex[i])++;
for(j = 0; j < walcnt - 1; j++) //implements wall collisions for enemys
{
if(eney[i] == waly[j] && enex[i] == walx[j])
{
enex[i]--;
if(random() % 3 == 0)
{
switch(randmv)
{
case 0 :
enex[i]--;
break;
case 1 :
eney[i]--;
break;
case 2 :
eney[i]++;
break;
}
}
if(eney[i] == waly[j] && enex[i] == walx[j])
{
switch(randmv)
{
case 0 :
enex[i]++;
break;
case 1 :
eney[i]++;
break;
case 2 :
eney[i]--;
break;
}
}
j = walcnt - 1;
}
}
}
else
{
(enex[i])--;
for(j = 0; j < walcnt - 1; j++) //implements wall collisions for enemys
{
if(eney[i] == waly[j] && enex[i] == walx[j])
{
enex[i]++;
if(random() % 3 == 0)
{
switch(randmv)
{
case 0 :
enex[i]++;
break;
case 1 :
eney[i]--;
break;
case 2 :
eney[i]++;
break;
}
}
if(eney[i] == waly[j] && enex[i] == walx[j])
{
switch(randmv)
{
case 0 :
enex[i]--;
break;
case 1 :
eney[i]++;
break;
case 2 :
eney[i]--;
break;
}
}
j = walcnt - 1;
}
}
}
}
}
}
}
void wallcheck(int *y, int *waly, int *x, int *walx, int walcnt, int ch)
{
int i;
switch(ch) //prevents the player from entering a wall
{
case 'w' ://this logic and loop and similar following ones send character back one space if they attempt to enter a wall according to the direction they were going before
for(i = 0; i < walcnt - 1; i++)
{
if((*y) == waly[i] && (*x) == walx[i])
{
(*y)++;
i = walcnt - 1;
}
}
break;
case 'a' :
for(i = 0; i < walcnt - 1; i++)
{
if((*y) == waly[i] && (*x) == walx[i])
{
(*x)++;
i = walcnt - 1;
}
}
break;
case 's' :
for(i = 0; i < walcnt - 1; i++)
{
if((*y) == waly[i] && (*x) == walx[i])
{
(*y)--;
i = walcnt - 1;
}
}
break;
case 'd' :
for(i = 0; i < walcnt - 1; i++)
{
if((*y) == waly[i] && (*x) == walx[i])
{
(*x)--;
i = walcnt - 1;
}
}
break;
}
}
void compcheck(int* tary, int* tarx, int* tardon, int tarcnt, int* movy, int* movx, int movcnt) //checks if the user has won yet by checking if all targets have balls on them
{
int i, j;
for(i = 0; i < tarcnt - 1; i++)
{
for(j = 0; j < movcnt - 1; j++)
{
if(tary[i] == movy[j] && tarx[i] == movx[j])
{
(*tardon)++;
}
}
}
}
void movcheck(int* y, int* movy, int* x, int* movx, int ch, int row, int col, int movcnt) //this function allows the player to push moveable balls
{
int i;
for(i = 0; i < movcnt - 1; i++)
{
if(*x == movx[i] && *y == movy[i])
{
switch(ch)
{
case 'a' :
(movx[i])--;
break;
case 's' :
(movy[i])++;
break;
case 'w' :
(movy[i])--;
break;
case 'd' :
(movx[i])++;
break;
}
if(movy[i] < 2)
{
(*y)++;
(movy[i])++;
}
if(movx[i] < 2)
{
(*x)++;
(movx[i])++;
}
if(movx[i] > col - 3)
{
(*x)--;
(movx[i])--;
}
if(movy[i] > row - 3)
{
(*y)--;
(movy[i])--;
}
i = movcnt - 1;
}
}
}