-
Notifications
You must be signed in to change notification settings - Fork 5
/
adventure-game.c
84 lines (72 loc) · 2.56 KB
/
adventure-game.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_SIZE 50
typedef struct Room {
char name[MAX_INPUT_SIZE];
char description[MAX_INPUT_SIZE];
int north;
int south;
int east;
int west;
} Room;
int main() {
// Define the game map
Room map[3];
strcpy(map[0].name, "Room 1");
strcpy(map[0].description, "You are in a small, dimly-lit room.");
map[0].north = 1;
map[0].south = -1;
map[0].east = -1;
map[0].west = -1;
strcpy(map[1].name, "Room 2");
strcpy(map[1].description, "You are in a large, spacious room.");
map[1].north = -1;
map[1].south = 0;
map[1].east = -1;
map[1].west = -1;
strcpy(map[2].name, "Room 3");
strcpy(map[2].description, "You are in a dark, creepy room.");
map[2].north = -1;
map[2].south = -1;
map[2].east = 0;
map[2].west = -1;
// Set up the initial game state
int current_room = 0;
char input[MAX_INPUT_SIZE];
printf("Welcome to the game! Type 'help' for a list of commands.\n");
// Main game loop
while (1) {
// Print the current room description
printf("\n%s\n", map[current_room].description);
// Get user input
printf("> ");
fgets(input, MAX_INPUT_SIZE, stdin);
input[strcspn(input, "\n")] = '\0'; // Remove newline character from input
// Process user input
if (strcmp(input, "help") == 0) {
printf("Available commands: go [north|south|east|west], quit\n");
} else if (strcmp(input, "quit") == 0) {
printf("Goodbye!\n");
break;
} else if (strncmp(input, "go ", 3) == 0) {
// Extract the direction from the input
char *direction = input + 3;
// Move the player in the specified direction
if (strcmp(direction, "north") == 0 && map[current_room].north != -1) {
current_room = map[current_room].north;
} else if (strcmp(direction, "south") == 0 && map[current_room].south != -1) {
current_room = map[current_room].south;
} else if (strcmp(direction, "east") == 0 && map[current_room].east != -1) {
current_room = map[current_room].east;
} else if (strcmp(direction, "west") == 0 && map[current_room].west != -1) {
current_room = map[current_room].west;
} else {
printf("You can't go that way.\n");
}
} else {
printf("Invalid command. Type 'help' for a list of commands.\n");
}
}
return 0;
}