3
3
#include <sys/stat.h>
4
4
#include <unistd.h>
5
5
#include <dirent.h>
6
- #include <regex.h>
7
6
#include <stdlib.h>
8
7
#include <string.h>
9
8
@@ -14,21 +13,22 @@ typedef struct {
14
13
char type ; /* can be s, m, or e */
15
14
} Room ;
16
15
16
+
17
17
char * getDir ();
18
18
Room * buildMap (char * dir );
19
19
void printRooms (Room * roomList );
20
20
int findIndex (Room * roomList , char * str );
21
- void startGame (Room * roomList );
21
+ int startGame (Room * roomList );
22
22
int getStartRoom (Room * roomList );
23
23
void printPossibleConnections (Room * roomList , int );
24
24
int isConnected (Room * roomList , char * str , int currentRoom );
25
25
void printHistory (Room * roomList , int * arr , int count );
26
26
27
27
int main (int argc , char const * argv []) {
28
- startGame (buildMap (getDir ()));
28
+ return startGame (buildMap (getDir ()));
29
29
}
30
30
31
- void startGame (Room * roomList ) {
31
+ int startGame (Room * roomList ) {
32
32
int currentRoom = getStartRoom (roomList ), exit = 0 , askAgain = 0 , count = 0 ;
33
33
int history [1000 ];
34
34
char readIn [100 ], str [100 ];
@@ -40,18 +40,21 @@ void startGame(Room* roomList) {
40
40
printf ("POSSIBLE CONNECTIONS: " );
41
41
printPossibleConnections (roomList , currentRoom );
42
42
printf ("WHERE TO? >" );
43
- fgets (readIn , 100 , stdin );
44
- sscanf (readIn , "%s" , str );
43
+ if (fgets (readIn , 100 , stdin ) == NULL ) {
44
+ return -1 ;
45
+ }
46
+ else
47
+ sscanf (readIn , "%s" , str );
45
48
46
49
if (findIndex (roomList , str ) == -1 || isConnected (roomList , str , currentRoom ) == -1 )
47
- printf ("\nHUH? I DON’T UNDERSTAND THAT ROOM. TRY AGAIN.\n\n " );
50
+ printf ("\nHUH? I DON’T UNDERSTAND THAT ROOM. TRY AGAIN.\n" );
48
51
else {
49
52
askAgain = 1 ;
50
53
currentRoom = findIndex (roomList , str );
51
54
history [count ] = currentRoom ;
52
55
count ++ ;
53
- printf ("\n" );
54
56
}
57
+ printf ("\n" );
55
58
}
56
59
if (roomList [currentRoom ].type == 'e' ) {
57
60
printf ("YOU HAVE FOUND THE END ROOM. CONGRATULATIONS!\n" );
@@ -62,6 +65,7 @@ void startGame(Room* roomList) {
62
65
else
63
66
askAgain = 0 ;
64
67
}
68
+ return 0 ;
65
69
}
66
70
67
71
void printHistory (Room * roomList , int * arr , int count ) {
0 commit comments