-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dnd-minigame.c
539 lines (465 loc) · 11.2 KB
/
Dnd-minigame.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
527
528
529
530
531
532
533
534
535
536
537
538
539
//
// DND Mini-game!
// winter 2023
// author: Kara Barrese
//
// includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> // for random numbers?
#include <stdbool.h> // for booleans
#include <ctype.h>// for evaluating input
// defines
const int MAX_PEOPLE = 10; // somehow try and get the struct to use this instead of 10 for length?
const int MAX_BOARDS = 10;
const char north = '^';
const char west = '<';
const char south = 'v';
const char east = '>';
const char directions[] = {north,west,south,east};
//global variables
typedef struct person
{
int in_x;
int in_y;
int cur_x;
int cur_y;
int cur_dir;//current direction {(1,2,3,4)=(^,<,v,>)}
int in_dir;
char rep[100];
int relation_to_player; // 1= follows, 2= does opposite,
}PERSON;
typedef struct board
{
int length;
int width;
int num_people;
PERSON people[MAX_PEOPLE];
/*
future:
the max # of people should be the amount of squares?
which is the length X width right?
how do I make this initialize without a limit or define the
limit once the other stuff is defined
what's the use of a union again? doesn't it use the other
variables to define the first one
what if, a union that is an array of pointers to people but only
is decided based on the length & width
- i think a union might not be able to do that :(
*/
}BOARD;
//updates as what_is_here updates
char representation[2]= {' ',' '};
//updates as is_on_edge updates
int edges[2];
BOARD *boards[MAX_BOARDS];
int num_boards;
//functions
void new_board();
void show_board(BOARD *B);
int what_is_here(BOARD *B, int i, int j);
void new_person(BOARD *B);
void print_demo_board(int num_rows, int num_cols);
void pick_board();
void play(BOARD *B);
void reset(BOARD *B);
//bool is_in_range();
void person_moves_forward(PERSON *P);
int is_on_edge(BOARD *B, PERSON *P);
void show_board(BOARD *B)
{
int i,j = 0;
for(i=0; i<(B->length);i++)
{
for(j=0;j<(B->width);j++)
{
what_is_here(B,i,j); // should update global representation variable
printf("[%s] ", representation);
}
printf("\n");
}
}
//returns # of people on the spot
int what_is_here(BOARD *B, int i, int j)
{
representation[0] = ' ';
representation[1] = ' ';
int m;
int num_people_on_spot = 0;
for(m=0; m<= B->num_people; m++)
{
if((B->people[m].cur_x == i)&&(B->people[m].cur_y == j))
{
num_people_on_spot++;
representation[0] = B->people[m].rep[0];
representation[1] = directions[B->people[m].cur_dir];
return 1; // returns 1 if there's a person there
//could return num of people?
}
}
return 0;
/*if (num_people_on_spot>1)
{
reset(B);
}
return num_people_on_spot;*/
//this would make it slightly slower and idk if it's needed so i'll keep it ^
}
void new_board()
{
BOARD *B;
int n_rows, n_cols, n_people = 0;
printf("in new_board function!\n");
if (num_boards == MAX_BOARDS)
{
printf("no more saves for boards :(\n");
}
printf("How many columns should the board be?");
fpurge(stdin);
scanf ("%d", &n_cols);
if(n_cols>0)
{
B->width = n_cols;
}
else
{
printf("invalid columns\n");
new_board();
return;
}
printf("How many rows should the board be?");
fpurge(stdin);
scanf ("%d", &n_rows);
if(n_rows>0)
{
B->length = n_rows;
}
else
{
printf("invalid rows\n");
new_board();
return;
}
B->num_people = n_people;
new_person(B);
if (num_boards!=0)
{
boards[num_boards-1] = B;
}
else
{
boards[0] = B;
printf("first board placed!\n");
}
num_boards++;
return;
}
//future: make different functions for each variable inputted that you can call if inputted wrong with recursion!
//recursive and goes till people doesn't exceed total num of people
void new_person(BOARD *B)
{
PERSON P;
int x_pos, y_pos, direction, representation, rel_to_player, make_more;
int n_rows = B->length;
int n_cols = B->width;
int p4p = (B -> num_people)-1; //p4p = place for person
if((B->num_people)==MAX_PEOPLE)
{
printf("Can't make any new people!\n");
return;
}
else if ((B->num_people) == 0)
{
p4p = 0;
}
printf("in new_person function (get ready to make a cool human!)\n");
printf("Where do you want your person to be?:\n");
print_demo_board(B->width, B->length); // this is the x,y or i,j
printf("What column do they start on?\n - this would be the B in [A x B] for where you want it)\n - must be between 0 - %d\n",n_rows);
fpurge(stdin);
scanf ("%d", &x_pos);
// do validation later
// should take care of number input maybe?
if(x_pos>n_rows)
{
printf("That column doesn't exist, try again and make sure it's %d or below:\n", n_rows);
//recursion!
new_person(B); // do I put in *B or B here?
return;
//is return needed?
}
printf("What row do they start on?\n - this would be the B in [A x B] for where you want it)\n - must be between 0 - %d\n",n_cols);
fpurge(stdin);
scanf ("%d", &y_pos);
// do validation later
// can use isdigit() with new library :>
// should take care of number input maybe
if(y_pos>n_cols)
{
printf("That row doesn't exist, try again and make sure it's %d or below:\n", n_cols);
//recursion!
new_person(B); // do I put in *B or B here?
return;
//is return needed?
}
else
{
printf("valid input");
}
if(what_is_here(B,x_pos,y_pos)!=0)
{
printf("There's already a person on that spot! Try again:\n");
new_person(B);
return;
}
printf("What symbol would you like to represent the person?:\n");
fpurge(stdin);
scanf ("%s", P.rep);
printf("What does this person do?\n 1: they follow the inputs\n 2: they go opposite from the main person\n");
fpurge(stdin);
scanf("%d", &P.relation_to_player);
printf("What direction are they starting off in?\n 1: north (^)\n 2: west (<)\n 3: south (v)\n 4: east (>)\n");
fpurge(stdin);
scanf("%d", &direction);
if((direction<=0) || (direction>4))
{
printf("That's not a valid direction, please pick a number from 1-4!\n");
new_person(B);
return;
}
P.in_x = x_pos;
P.cur_x = x_pos;
P.in_y = y_pos;
P.cur_y = y_pos;
P.in_dir = (direction-1);
P.cur_dir = (direction-1);
B->people[p4p] = P;
B->num_people++;
printf("Do you want to make another person? (1 = yes, 2 = no):\n");
fpurge(stdin);
scanf("%d", &make_more);
if(make_more == 1)
{
new_person(B);
return;
}
else if (make_more == 2)
{
return;
}
return;
}
void print_demo_board(int num_rows, int num_cols)
{
int i,j=0;
printf("Demo Board: \n");
for(i=0;i<num_rows;i++)
{
for(j=0;j<num_cols;j++)
{
printf("[%d x %d] ", i, j);
}
printf("\n");
}
printf("\n");
}
//asks the user which board they want to play with
void pick_board()
{
printf("in pick board function\n");
//listing boards
int i, brd;
for(i=1;i<=num_boards;i++)
{
printf("1: Board #%d \n", i);
}
printf("which board would you like to play on?\n");
fpurge(stdin);
scanf("%d", &brd);
play(boards[brd-1]);
}
void play(BOARD *B)
{
int i, j, k = 0;
char direction;
//char souf = 'v';
printf("choose which direction you'd like to move in and type in an answer whenever prompted with ">"\n W: north (^)\n A: west (<)\n S: south (v)\n D: east (>)\nTry not to get players to overlap!\nHave fun :)\n");
while(lose(B) != 0)
{
printf("\n>");
fpurge(stdin);
scanf("%d", &direction);
switch(direction) {
case 'W':
move(B,0);//north
case 'A':
move(B,1);//west
case 'S':
move(B,2);//south
case 'D':
move(B,3);//east
default:
printf("invalid input\n");
}
}
}
void move(BOARD *B, int dir)
{
int i;
for(i=0;i<B->num_people;i++)
{
PERSON *P = B->people[i];
switch(is_on_edge(B,P)){
case 0: //they aren't on an edge! Hooray
if (P->cur_dir == dir)
{
person_moves_forward(P);
//person moves forward
}
case 1: //they're on one edge
if (((P->cur_dir) == dir) && (dir!= edges[0]))
{
person_moves_forward(P);
//person moves forward
}
//else if direction within 1 of absolute value or something make it turn
//call update cur_dir?
case 2://they're on 2 edges :(
if ((((B->people[i]->cur_dir) == dir) && (dir!= edges[0]))&&(dir!= edges[1]))
{
person_moves_forward(P);
//person moves forward
}
default:
printf("In default in move switch case\n");
}
}
//make it so that they go forward in whatever way
/*
person = ^ (0)input = ^(0) : current y--;
person = < (1), person = >(3), input = ^(0): current dir = ^(0);
person = v (2)input = ^(0) : nothing happens;
person = v (2)input = v(2) : current y++;
person = < (1), person = >(3), input = v(2): current dir = v(2);
person = v (2)input = ^(0) : nothing happens;
person = < (1)input = <(1) : current x--;
person = ^ (0), person = v(2), input = <(1): current dir = <(1);
person = > (3)input = <(1) : nothing happens;
person = > (3)input = >(3) : current x++;
person = ^ (0), person = v(2), input = >(3): current dir = >(3);
person = < (1)input = >(3) : nothing happens;
*/
}
//returns how many edges they are on and updates edges with which edges they are on
int is_on_edge(BOARD *B, PERSON *P)
{
/*
north edge = 0
west edge = 1
south edge = 2
east edge = 3
*/
// idk if this function will work if 4 edges touch
int num_edges = 0;
if(P->cur_x == 0)
{
edges[num_edges]= 0;
num_edges++;
}
else if(P->cur_x == ((B->width)-1))
{
edges[num_edges] = 3;
num_edges++;
}
if(P->cur_y == 0)
{
edges[num_edges] = 2;
num_edges++;
}
else if(P->cur_y == ((B->length)-1))
{
edges[num_edges] = 3;
num_edges++;
}
//2 ifs because it shouldn't be on both x edges & y edges at the same time
return num_edges;
}
//checks to see if there is more than one person on a spot
//returns 1 if you lose
int lose(BOARD *B)
{
int i, j =0;
for(i=0;i<(B->num_people);i++)
{
for(j=0;j<(B->num_people);j++)
{
if(((B->people[i]->cur_x) == (B->people[j]->cur_x)) && ((B->people[i]->cur_y) == (B->people[j]->cur_y)))
{
return 1;
}
}
}
return 0;
}
//resets people to their starting positions
void reset(BOARD *B)
{
int i =0;
for(i=0;i<(B->num_people);i++)
{
B->people[i]->cur_x = B->people[i]->in_x;
B->people[i]->cur_y = B->people[i]->in_y;
}
return;
}
void person_moves_forward(PERSON *P)
{
switch(P->cur_dir)
{
case 0://north
P->cur_y--;
case 1://west
P->cur_x--;
case 2://south
P->cur_y++;
case 3://east
P->cur_x++;
default:
printf("in default on person moves forward function\n this shouldn't happen\n");
}
}
//future list stuff might be needed
//list *board[];
//make a list of pointers to new boards
// create an array of boards where you select which one and can make new ones
// each one will have a 2D array of people and where they are
// when you select one it will put people at their starting positions
//print function will print the board at any position
// going forward changes current x pos to +1 if 2, -1 if 4 and y pos to be +1 if 1, and -1 if 3.
int main (void)
{
int option;
while (1)
{
printf ("Choose an option:\n 1: make new board\n 2: see boards & pick which one to play\n 0: exit\nEnter option: ");
if (scanf ("%d", &option) != 1)
{
printf ("error\n");
return 0;
}
switch (option)
{
case 1:
printf("make new board:\n");
new_board();
break;
case 2:
printf("seeing boards:\n");
pick_board();
break;
default:
printf ("wrong option\n");
}
}
}