Text-based maze game help

Nov 21, 2011 at 6:20pm
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

#include <iostream>
#include <fstream>
#include <string>



using namespace std;

struct Node
{
	Node();
	char Name;
	Node *North;
	Node *South;
	Node *East;
	Node *West;
	char get_Name();
	void set_Name(char k);
	string move_options();
	char name;
	Node get_North();
	Node get_South();
	Node get_East();
	Node get_West();
	void set_North(Node *n);
	void set_South(Node *s);
	void set_East(Node *e);
	void set_West(Node *w);

Node(char e)
{
	Name = e;
	North = NULL;
	South = NULL;
	East = NULL; 
	West = NULL;
}
};

Node Node:: get_North()
{
	return *North;
}

Node Node:: get_South()
{
	return *South;
}

Node Node:: get_East()
{
	return *East;
}

Node Node:: get_West()
{
	return *West;
}

void Node:: set_North(Node *n)
{
	North = n;
}

void Node:: set_South(Node *s)
{
	South = s;
}

void Node:: set_East(Node *e)
{
	East = e;
}

void Node:: set_West(Node *w)
{
	West = w;
}

char Node:: get_Name()
{
	return name;
}

void Node:: set_Name(char k)
{
	name = k;
}

Node:: Node()
{
}



struct MazeMovement
{
	int StepsTaken;
	Node CurrentRoom;
	bool MazeDone;
	MazeMovement() {StepsTaken = 0;}
	bool is_MazeDone();
	void Movement(char Direction);
	Node get_CurrentRoom();
	int get_StepsTaken();
	void Read_Maze(string file);
	char Name;
	Node Rooms[30];
	Node find_Node(char Name);
	
};

Node MazeMovement:: get_CurrentRoom()
{
	return CurrentRoom;
}

int MazeMovement:: get_StepsTaken()
{
	return StepsTaken;
}

bool MazeMovement:: is_MazeDone()
{
	if(CurrentRoom.get_Name() == 'd')
	{
		MazeDone = true;
	}
	return MazeDone;
}

void MazeMovement:: Movement(char Direction)
{
	string Moves = CurrentRoom.move_options();
	switch(Direction)
	{
		case 'N':
		case 'n':
			size_t nfound;
			nfound = Moves.find("North");
			if(int(nfound) >= 0)
			{
				CurrentRoom = CurrentRoom.get_North();
				StepsTaken++;
			}
			else 
			{
				cout << "Invalid selection. Please try again. \n";
			}
			break;
			
		case 'S':
		case 's':
			size_t sfound;
			sfound = Moves.find("South");
			if(int(sfound) >= 0)
			{
				CurrentRoom = CurrentRoom.get_South();
				StepsTaken++;
			}
			else
			{
				cout << "Invalid selection. Please try again. \n";
			}
			break;
			
		case 'E':
		case 'e':
			size_t efound;
			efound = Moves.find("East");
			if(int(efound) >= 0)
			{
				CurrentRoom = CurrentRoom.get_East();
				StepsTaken++;
			}
			else
			{
				cout << "Invalid selection. Please try again. \n";
			}
			break;
			
		case 'W':
		case 'w':
			size_t wfound;
			wfound = Moves.find("West");
			if(int(wfound) >= 0)
			{
				CurrentRoom = CurrentRoom.get_West();
				StepsTaken++;
			}
			else
			{
				cout << "Invalid selection. Please try again. \n";
			}
			break;
			
		default:
			cout << "Invalid selection. Please try again. \n";
	}
}
	
void MazeMovement:: Read_Maze(string FileName)
{
	string line;
	ifstream inStream;
	inStream.open(FileName.c_str());
	int test = inStream.peek();

	int i = 0;
	if (!(inStream.fail()))
	{
	while(!inStream.eof() && test != EOF)
    {
      getline(inStream, line);
		Node n(line[0]);
		i++;
		Rooms[i] = n;

		if(!(line[2] == '*'))
		{
			Node North = find_Node(line[2]);
			(find_Node(line[0])).set_North(&North);
		}
		
		if(!(line[4] == '*'))
		{
			Node East = find_Node(line[4]);
			(find_Node(line[0])).set_East(&East);
		}
		
		if(!(line[6] == '*'))
		{
			Node South = find_Node(line[6]);
			(find_Node(line[0])).set_South(&South);
		}
		
		if(!(line[8] == '*'))
		{
			Node West = find_Node(line[8]);
			(find_Node(line[0])).set_West(&West);
		}
	}
	CurrentRoom = find_Node('A');
	}
	else
	{
		cout << "Could not open the file name entered!" << endl;
		exit(1);
	}
}

string Node::move_options()
{
	string options;
	if(!(North == NULL))
	{
		options += "North\n";
	}
	if(!(South == NULL))
	{
		options += "South\n";
	}
	if(!(East == NULL))
	{
		options += "East\n";
	}
	if(!(West == NULL))
	{
		options += "West\n";
	}
	if(options.empty())
	{
		options += "There is no where for you to move, sorry. Please choose another maze!";
	}
	
	return options;
}

Node MazeMovement::find_Node(char Name)
{
	Node found('*');
	for(int i=0; i <30; i++)
	{
		if(Rooms[i].get_Name() == Name)
		{
		found = Rooms[i];
		}
	}
	return found;
}
int main() 
{
	string FileName;
	MazeMovement myMaze;

	cout << "Please enter the name of the file: ";
	getline(cin,FileName);
	FileName += ".txt";

	myMaze.Read_Maze(FileName);
	
	cout << "========================================================================= " << endl;
	cout << "			Welcome to the Tiger Maze!						                       " << endl;
	cout << "========================================================================= " << endl;
	
	do
	{
		string SelectedDirection;
		char selection;
		cout << "You are currently in Room ";
		cout << myMaze.get_CurrentRoom().get_Name();
		cout << " of the Amazing Maze, you can go " + 
		myMaze.get_CurrentRoom().move_options() +".\n What is your choice?";
		getline(cin, SelectedDirection);
		selection = SelectedDirection[0];
		myMaze.Movement(selection);
	}while(!myMaze.is_MazeDone());
	
	cout << "Congratulations! You have reached the finish point. \nYou took ";
	cout << myMaze.get_StepsTaken();
	cout << " steps." << endl;

	char a;
	bool found = false;
	ifstream InFile;
	Node *Root[20][20] = {NULL};
	InFile.open("FileName");
	
	for(int i = 0; i <=30; i++)
	{
		for(int j = 0; j <= 30; j++)
		{
			InFile >> a;
			if(Root[i][j] == NULL)
				Root[i][j] = new Node(a);
			
			Root[i][j] -> North = Root[i-1][j];
			Root[i][j] -> South = Root[i+1][j];
			Root[i][j] -> West = Root[i][j-1];
			Root[i][j] -> East  = Root[i][j+1];
			
			//cout << Root[i][j].Name;
		}
		cout << endl;
	}
				

while(found == false)
{
	found = true;
}
	
	for(int i= 0; i <= 30; i++)
	{
		for(int j = 0; j <= 30; j++)
		{
			a = Root[i][j] -> Name;
			cout << a;
		}
}

	return 0;
}


My output is as follows:

Please enter the name of the file: Maze
=========================================================================
Welcome to the Tiger Maze!
=========================================================================
You are currently in Room 0 of the Amazing Maze, you can go There is no where for you to move, sorry. Please choose another maze!.
What is your choice?Invalid selection. Please try again.
Congratulations! You have reached the finish point.
You took 0 steps.

Last edited on Nov 21, 2011 at 6:21pm
Nov 21, 2011 at 6:21pm
The instructions are as follows:
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
In this programming assignment, you will implement the maze above using references to instances of a Node class, which you will define. Each node in the graph will be implemented as an instance of Node. The edges correspond to the links that connect one node to another and can be represented in Node as instance variables that reference another Node class object. Since there are four possible links, Node must contain four links (pointers)  ̶  north, south, east and west  ̶  to other Node objects.

The Start variable references is a Node pointer type that points to the node where the user starts, which may represent the room A. The goal is to reach the finish point which is the node that is referenced by the Finish variable which may reference the node representing the room L.

Graph Configuration

The configuration of the graph that you will use in the program will be read in from a text file. Your program must first prompt the user for name of the Graph Configuration file, e.g. graphConfig1.txt, and then opens the file for reading. Each line in the file indicates information on each node, i.e. the name of the node, and the links that may exist from that node to another node in the North, East, South and West directions. If there is no link in a specific direction, then there is an ‘*’ in place of the node name. For example, the configuration file for the graph above is as follows:

(This is what appears inside my Maze.txt file, so if you are testing my program just  type the following in a txt document with no spaces)
A  E  B  *  *

B  *   *   *  A

C  G  *   *  *

D  H  *   *  *

E  *    F  A  *

F  J    G  *   E

G  K  H  C  F

H  *   *   D  G

I   *   J    *   *

J   *   *   F   I

K  *   L   G  *

L  *   *   *   K

In the first line of the configuration file above, the name of the node is A. It has a pointer to node E in the North direction and a pointer to node B in the East direction but no link in either the South or West direction. Thus the South and West pointers are set to NULL.

Your program will first read the graph configuration file and construct the graph data structures used for the Tiger Maze app. Your program must work with any graph configuration file. Several test configuration files will be released to you close to the deadline for you to test the correct execution of your program. All graph configuration files will have exactly twenty-four nodes, but their edges will be different.

To construct the graph based on the configuration file, you need to search for a certain node with a particular name, e.g. in the first line, Node A must be linked to Node E and you need to find E in order to set the north link of A to E. In order to search for E, the easiest way is to put every node in an array and then scan the array for a particular node name. Define a new class called NodeList  that contains an array of Node pointers that points to all the nodes in the maze. The array may be initialized by creating the nodes in the array using new operator. Then each of the node is given the nae 'A', 'B', ... The NodeList contains a member function to search for a particular node with a particular name.

Traversing the Graph

The game will traverse the graph based on the inputs given by the user. When the user is at a current room, the program will output the possible moves in the North, East, South or West directions. The user will then enter the room in the desired direction. While the program traverses the graph, it also counts the number of steps. Upon reaching the finish point, it will print out the congratulation banner and the number of steps taken.  

The user interface must check for correct input value from the users. If there is any error, e.g. selecting an invalid direction, then the program must display the appropriate error message and continue to prompt for the correct input. Your program must not exit or terminate when there is an incorrect input value.

I can't seem to get my program to read in the correct room and what directions the user is allowed to go. Any help would be appreciated. Thanks!
Nov 22, 2011 at 2:52pm
Any suggestions? I've posted on several c++ programming forums but I haven't received any replies
Nov 23, 2011 at 1:05am
toooo long code
Nov 23, 2011 at 1:17am
Cuz it's wayyyyyy toooooooo longggggggggg..
Topic archived. No new replies allowed.