-
Notifications
You must be signed in to change notification settings - Fork 0
/
egertonl.adventure.c
512 lines (425 loc) · 14.6 KB
/
egertonl.adventure.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
/****************************************************************
Lauren Egerton
CS 344 / Fall 2016
Program 2
Description : A program that uses an array of 10 strings and 7 struct rooms to randomly
populate 7 different files to play a simple text-based game.
Cite : Office hours with Prof. Brewster, 10/26/16, who offered helpful examples
of how I could break up my program into functions for the randomization
of connecting the rooms.
Cite : Ignoring "." files
http://stackoverflow.com/questions/20265328/readdir-beginning-with-dots-instead-of-//files
Cite : Creating directories https://linux.die.net/man/3/chdir
Cite : Used often as a reference, Programming in C by Stephan Kochan, 4th ed.
***************************************************************/
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
/****************************************************************
Function and struct definitions
***************************************************************/
//define struct room
struct room{
char name[10];
char type[12];
int numConnections;
struct room *connections[6];
};
//will populate newRoom with data from the files
struct newRoom{
char name[12];
char type[12];
int numberConnections;
char *connectRoom[6];
};
//returns a pointer to a random room from the array of struct rooms
struct room *getRandomRoom(struct room array[]){
int random;
struct room *A;
random = rand() % 7;
A = &array[random];
return A;
}
//returns true if the rooms have the same name
int isSameRoom(struct room *A, struct room *B){
if(strcmp(A->name, B->name) == 0){
return 0;
}
return 1;
}
//returns true if less than 6 connections exist (can have max of 6)
int canAddConnectionFrom(struct room *A){
if(A->numConnections < 6){
return 0;
}
else
return 1;
}
//returns true when all connections are between 3-6
int isGraphFull(struct room array[]){
int x, flag;
flag = 0;
//check connections for each room in array
for(x=0; x<7; x++){
if((array[x].numConnections > 2) && (array[x].numConnections < 7)){
flag = 0;
}
else{
flag = 1;
break;
}
}
return flag;
}
//check if the rooms are already connected
int isConnected(struct room *A, struct room *B){
int x, y;
y = A->numConnections;
for(x=0; x<y; x++){
if(A->connections[x] == B){
//return as soon as a connection is found
return 0;
}
}
//no connection found
return 1;
}
//creates a one-way connection between two rooms
void connectRoom(struct room *A, struct room *B){
int first;
first = A->numConnections;
//next available spot in room's connections arrays
A->connections[first] = B;
A->numConnections++;
}
//returns 0 if number is already in array
int containsNum(int array[], int value){
int i;
for (i=0; i < 7; i++) {
if (array[i] == value)
return 0;
}
return 1;
}
//returns 0 if the name is one of the connections for the room
int hasConnection(struct newRoom *ptr, char name[10]){
int i;
for(i=0; i<ptr->numberConnections; i++){
if(strncmp(ptr->connectRoom[i], name, 10) == 0)
return 0;
}
return 1;
}
/****************************************************************
Main function begins here
***************************************************************/
int main(void){
//seed random number
srand(time(0));
//create directory name using pid
char directoryName[20];
int pid = getpid();
sprintf(directoryName, "%s%d", "./egertonl.rooms.", pid);
//make a directory
mkdir(directoryName, 0755);
//create directory
char *directory = directoryName;
chdir (directory);
//use this array for indices of room names
int numbers[7];
int random, i, j;
//generate 7 random numbers between 0-9 and add to numbers array
for(i=0; i<7; i++){
do{
random = rand() % 10;
j = containsNum(numbers, random);
//keep looping until the number is unique
}while(j == 0);
//Add unique number to the array
numbers[i] = random;
}
//create a list of 10 names to use for struct rooms
char *roomNames[10] = { "Tokyo", "Jakarta", "Delhi", "Manila", "Chengdu", "Lima",
"London", "Madrid", "Mumbai", "Cairo" };
//Create an array 7 room structs
struct room allRooms[7];
//set all room connections to 0 to start
for(i=0; i<7; i++){
allRooms[i].numConnections = 0;
}
int x;
//1. For each struct room in array allRooms :
for(x=0; x<7; x++){
//Set room types
//first one is start_room
if(x == 0){
strncpy(allRooms[x].type, "START_ROOM", 12);
}
//last one is end_room
else if(x == 6){
strncpy(allRooms[x].type, "END_ROOM", 10);
}
//all other rooms to mid_room
else{
strncpy(allRooms[x].type, "MID_ROOM", 10);
}
//set name to indice of 'numbers' using roomNames array
strncpy(allRooms[x].name, roomNames[numbers[x]], 10);
}
//Set random connections between rooms
//loop until all rooms have 3-6 connections
do{
struct room *A, *B;
do{
A = getRandomRoom(allRooms);
}while(canAddConnectionFrom(A) == 1);
do{
B = getRandomRoom(allRooms);
//keep looping until a room has a free connection OR is the same room
//OR already have a connection
}while((canAddConnectionFrom(B) == 1) ||
(isSameRoom(A, B) == 0) || (isConnected(A, B) == 0));
connectRoom(A, B);
connectRoom(B, A);
}while(isGraphFull(allRooms) == 1);
//Put 7 rooms into a 7 different files, inside the Room directory
//For each struct room in allRooms
for(x=0; x<7; x++){
int w, z;
w = allRooms[x].numConnections;
//get the name of the room and save it in a string
char currName [10];
strncpy(currName, allRooms[x].name, 10);
FILE *inputFile;
inputFile = fopen(currName, "w");
//check that file is created
if (inputFile == 0){
fprintf(stderr, "Could not open %s\n", currName);
perror("Error in main()\n");
exit(1);
}
else{
//printf("File created and opened!\n");
}
fprintf(inputFile, "ROOM NAME: %s\n", currName);
for(z=0; z<w; z++){
//convert int to string for parsing later
char num[2];
snprintf(num, 2, "%d", (z+1));
fprintf(inputFile, "CONNECTION %s: %s\n", num, allRooms[x].connections[z]->name);
}
fprintf(inputFile, "ROOM TYPE: %s\n", allRooms[x].type);
fclose(inputFile);
}
//Read all data from files into a new struct room array
//create an array of 7 struct newRoom
struct newRoom gameRoom[7];
//set each newRoom connectRoom pointer to NULL ?
int w, c;
int length;
length = 10;
for(w=0; w<7; w++){
for(c=0; c<6; c++){
//printf("Do we get here?\n");
gameRoom[w].connectRoom[c] = malloc( length * sizeof(char) );
}
}
/*** Open Directory and read from each file into an array of structs for use in Game ****/
DIR *dir;
struct dirent *dirName;
char * file_name;
FILE *outFile;
dir = opendir(".");
char buffer[512];
memset(buffer, '\0', 512);
char check1[12]; //to check for Connection
memset(check1, '\0', 12);
char check2[12]; //tp check for Type or Name
memset(check2, '\0', 12);
char checkName[12]; //to use to save name or type
memset(checkName, '\0', 12);
int count; //to increment through array of struct rooms
count = 0;
//for each file
//ignore "." files
while ((dirName=readdir(dir)) != NULL){
int t;
t=0;
//skipping the dot files
if ( (strcmp(dirName->d_name, ".") == 0) || (strcmp(dirName->d_name, "..") == 0) ){
}
//get info from each file
else{
//open file
outFile = fopen(dirName->d_name, "r");
//check that file is open
if(outFile == 0){
fprintf(stderr, "Could not open %s\n", dirName->d_name);
perror("Error in main()\n");
exit(1);
}
else{
//printf("Current file: %s \n", dirName->d_name);
}
//get each line and parse
while(fgets(buffer, sizeof buffer, outFile) != NULL){
//printf("Getting the line now!\n");
//printf("Current line is: %s\n", buffer);
//parse line
sscanf(buffer, "%s%s%*c%s\n", check1, check2, checkName);
//printf("check1 is: %s check2 is: %s checkName is: %s\n", check1, check2, checkName);
//save in gameRoom array
//set name
if(strcmp(check2, "NAME:") == 0){
//printf("Check2 = Name\n");
strncpy(gameRoom[count].name, checkName, 12);
//printf("Current struct in game is name: %s\n", gameRoom[count].name);
memset(check2, '\0', 12);
memset(checkName, '\0', 12);
}
//set connections
if(strcmp(check1, "CONNECTION") == 0){
//printf("t = %d\n", t);
//printf("Check1 = CONNECTION\n");
strncpy(gameRoom[count].connectRoom[t], checkName, 12);
t++;
gameRoom[count].numberConnections = t;
memset(check1, '\0', 10);
memset(checkName, '\0', 12);
}
//set type
if(strcmp(check2, "TYPE:") == 0){
//printf("Check2 = Type\n");
strncpy(gameRoom[count].type, checkName, 12);
//printf("Current struct in game is type: %s\n", gameRoom[count].type);
memset(check2, '\0', 10);
memset(checkName, '\0', 12);
}
}
//close file
fclose(outFile);
//move to next struct in gameRoom array
count++;
//make sure char array is clear for next struct's info
memset(buffer, '\0', 512);
}
}
//close directory
closedir(dir);
/********************************************************************
Game Interface Starts Here
*********************************************************************/
//number of steps taken during game
int steps;
steps = 0;
//array of strings to track path taken
char *path[200];
//error message
char bigError[] = "HUH? I DON’T UNDERSTAND THAT ROOM. TRY AGAIN.";
//printf("Error is: %s\n", bigError);
char choice[10];
char here[10];
struct newRoom *current;
//start game in START_ROOM
printf("\n");
for(i=0; i<7; i++){
if(strcmp(gameRoom[i].type, "START_ROOM") == 0){
//pointer to current room
current = &gameRoom[i];
}
}
do{
printf("CURRENT LOCATION: %s\n", current->name);
printf("POSSIBLE CONNECTIONS:");
//print connections
for(x=0; x<current->numberConnections; x++){
printf(" %s", current->connectRoom[x]);
if(x < (current->numberConnections - 1)){
printf(",");
}
if(x == (current->numberConnections - 1)){
printf(".\n");
}
}
printf("WHERE TO? >");
//memset(choice, '\0', 10);
fgets(choice, 10, stdin);
//remove trailing newline char
strtok(choice, "\n");
//printf("Your current choice is: %s\n", choice);
printf("\n");
int g;
g = hasConnection(current, choice);
//if use has entered a correct connection choice
if(g == 0){
for(i=0; i<7; i++){
if(strncmp(gameRoom[i].name, choice, 10) == 0){
//allocate mem for a spot in path array
path[steps] = malloc( length * sizeof(char) );
//add current room to path
strncpy(path[steps], current->name, 10);
//change current pointer to next room
current = &gameRoom[i];
//add a step
steps++;
break;
}
}
}
//user has entered an incorrect user choice
else if(g == 1){
printf("%s\n", bigError);
printf("\n");
}
}while(strncmp(current->type, "END_ROOM", 12) != 0);
//User ends up in the END_ROOM, game is over
if(strncmp(current->type, "END_ROOM", 12) == 0){
printf("YOU HAVE FOUND THE END ROOM. CONGRATULATIONS!\n");
}
//add final room to path / steps count
for(i=0; i<7; i++){
if(strncmp(gameRoom[i].name, choice, 10) == 0){
//allocate mem for a spot in path array
path[steps] = malloc( length * sizeof(char) );
//add current room to path
strncpy(path[steps], current->name, 10);
//change current pointer to next room
current = &gameRoom[i];
//add a step
steps++;
}
}
//print steps
if(steps == 2){
printf("YOU TOOK %d STEP. YOUR PATH TO VICTORY WAS:\n", (steps-1));
}
else if(steps > 2){
printf("YOU TOOK %d STEPS. YOUR PATH TO VICTORY WAS:\n", (steps-1));
}
//print path taken
for(x=1; x<steps; x++){
printf("%s\n", path[x]);
}
printf("\n");
//free mem for struct room pointers to connections
for(w=0; w<7; w++){
for(c=0; c<6; c++){
//printf("Do we free?\n");
free(gameRoom[w].connectRoom[c]);
}
}
//free mem allocated for pointers for game path array
for(w=0; w<steps; w++){
free(path[w]);
}
//terminate and return 0 if program runs successfully
return 0;
}