Skip to content

Commit be475c9

Browse files
committed
Add game logic
1 parent 9fb04f5 commit be475c9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Program 2 - adventure/gakhalh.adventure.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <sys/stat.h>
44
#include <unistd.h>
55
#include <dirent.h>
6-
#include <regex.h>
76
#include <stdlib.h>
87
#include <string.h>
98

@@ -14,21 +13,22 @@ typedef struct {
1413
char type; /* can be s, m, or e */
1514
} Room;
1615

16+
1717
char* getDir();
1818
Room* buildMap(char* dir);
1919
void printRooms(Room* roomList);
2020
int findIndex(Room* roomList, char* str);
21-
void startGame(Room* roomList);
21+
int startGame(Room* roomList);
2222
int getStartRoom(Room* roomList);
2323
void printPossibleConnections(Room* roomList, int);
2424
int isConnected(Room* roomList, char* str, int currentRoom);
2525
void printHistory(Room* roomList, int* arr, int count);
2626

2727
int main(int argc, char const *argv[]) {
28-
startGame(buildMap(getDir()));
28+
return startGame(buildMap(getDir()));
2929
}
3030

31-
void startGame(Room* roomList) {
31+
int startGame(Room* roomList) {
3232
int currentRoom = getStartRoom(roomList), exit = 0, askAgain = 0, count = 0;
3333
int history[1000];
3434
char readIn[100], str[100];
@@ -40,18 +40,21 @@ void startGame(Room* roomList) {
4040
printf("POSSIBLE CONNECTIONS: ");
4141
printPossibleConnections(roomList, currentRoom);
4242
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);
4548

4649
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");
4851
else {
4952
askAgain = 1;
5053
currentRoom = findIndex(roomList, str);
5154
history[count] = currentRoom;
5255
count++;
53-
printf("\n");
5456
}
57+
printf("\n");
5558
}
5659
if (roomList[currentRoom].type == 'e') {
5760
printf("YOU HAVE FOUND THE END ROOM. CONGRATULATIONS!\n");
@@ -62,6 +65,7 @@ void startGame(Room* roomList) {
6265
else
6366
askAgain = 0;
6467
}
68+
return 0;
6569
}
6670

6771
void printHistory(Room* roomList, int* arr, int count) {

0 commit comments

Comments
 (0)