-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5.cpp
224 lines (196 loc) · 4.63 KB
/
lab5.cpp
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
//Autumn Henderson
//Lab 5
//CS 130
//February 15th, 2019
//This program is a simple MUD that supports a single user and several rooms.
#include<string>
#include<fstream>
#include<stdlib.h>
#include<sstream>
using namespace std;
struct Room{
string room_num;
string description;
//Will represent n, e, s, w. Will be 1 if direction exists
int room_direction[4] = {0, 0, 0, 0};
//Will store corresponding room index
int room_index[4] = {0, 0, 0, 0};
};
int main(int argc, char *argv[]) {
string file_name;
ifstream fin1;
ifstream fin2;
string line;
char direction;
int index;
stringstream sout;
int i = 0;
int num_rooms = 0;
int num_tildes = 0;
Room *ptr = NULL;
//Game Variables
char command;
int curr_game_index = 0;
char discard;
//OPENS FILE, PROCESSES, AND CLOSES FILE
//Checks argc
if(argc < 2) {
printf("Usage: <./lab5> <file_name>\n");
return -1;
}
//Opens file and checks to ensure file was able to be opened
file_name = argv[1];
fin1.open(file_name);
if (!fin1) {
printf("Unable to open file\n");
return -2;
}
//While loop counts rooms in file to allocate memory
while(getline(fin1, line)) {
//Checks for tildes. If there is one, updates num_tildes.
if(line == "~") {
++num_tildes;
//Checks to see if three tildes on lines on their own have been found.
//If so, updates num_rooms.
if((num_tildes + 3) % 3 == 0) ++num_rooms;
}
else continue;
}
//Uses while loop information to allocate memory
ptr = new Room[num_rooms];
//Checks pointer validity
if(!ptr) {
printf("Failed to allocate memory.\n");
return -3;
}
fin1.close();
//2nd While loop gathers information into stringstream and puts it in memory
sout.clear();
num_tildes = 0;
line = "";
fin2.open(file_name);
if (!fin2) {
printf("Unable to open file.\n");
return -4;
}
while(getline(fin2, line)) {
//Checks for tildes. If there is one, updates num_tildes
if(line == "~") {
++num_tildes;
//Places information into memory
if(num_tildes == 1) {
ptr[i].room_num = sout.str();
sout.clear();
sout.str("");
}
else if(num_tildes == 2) {
ptr[i].description = sout.str();
sout.clear();
sout.str("");
}
else if(num_tildes == 3) {
//While loop divides up room direction and room index
while(sout >> direction) {
sout >> index;
switch (direction) {
case 'n': ptr[i].room_direction[0] = 1;
ptr[i].room_index[0] = index;
break;
case 'e': ptr[i].room_direction[1] = 1;
ptr[i].room_index[1] = index;
break;
case 's': ptr[i].room_direction[2] = 1;
ptr[i].room_index[2] = index;
break;
case 'w': ptr[i].room_direction[3] = 1;
ptr[i].room_index[3] = index;
break;
}
}
sout.clear();
sout.str("");
++i;
num_tildes = 0;
}
}
else sout << line << "\n";
}
//Closes file
fin2.close();
//GAME BEGINS
do{
printf("> ");
scanf("%c%c", &command, &discard);
//Quits the game
if (command == 'q') break;
if (command == 'l') {
string exits = "";
printf("%s", ptr[curr_game_index].room_num.c_str());
printf("%s\n", ptr[curr_game_index].description.c_str());
printf("Exits: ");
//For loop prints exits
for(int j = 0; j < 4; ++j) {
if (ptr[curr_game_index].room_direction[j] == 1) {
switch (j) {
case 0: printf("n ");
break;
case 1: printf("e ");
break;
case 2: printf("s ");
break;
case 3: printf("w ");
break;
}
}
}
printf("\n");
}
//Checks commands to determine direction to move
else if (command == 'n') {
if(ptr[curr_game_index].room_direction[0] == 0) {
printf("You can't go NORTH!\n");
continue;
}
else {
printf("You moved NORTH.\n");
curr_game_index = ptr[curr_game_index].room_index[0];
}
}
else if (command == 'e') {
if(ptr[curr_game_index].room_direction[1] == 0) {
printf("You can't go EAST!\n");
continue;
}
else {
printf("You moved EAST.\n");
curr_game_index = ptr[curr_game_index].room_index[1];
}
}
else if (command == 's') {
if(ptr[curr_game_index].room_direction[2] == 0) {
printf("You can't go SOUTH!\n");
continue;
}
else {
printf("You moved SOUTH.\n");
curr_game_index = ptr[curr_game_index].room_index[2];
}
}
else if (command == 'w') {
if(ptr[curr_game_index].room_direction[3] == 0) {
printf("You can't go WEST!\n");
continue;
}
else {
printf("You moved West.\n");
curr_game_index = ptr[curr_game_index].room_index[3];
}
}
else {
printf("Command does not exist.\n");
continue;
}
}while(true);
delete[] ptr;
return 0;
}