Hey there,
I have an assignment that I'm working on that I feel I am very close to finishing. Basically the idea is that I need to read in a "maze" from a text file that looks something like this:
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
Then,using classes and pointers, I need to construct the maze, where each line above is a "Node".
The first character in the line is the name of the "Node". The next character to the right is the Node located directly North, then next character is the node that is located east of the node, then next is south, then next is west. If there is a "*". That means there is no node in that direction. The maze is done when I navigate correctly from Node A to Node L.
If done correctly, it will look something like this:
===========================================================
| Welcome to Tiger Maze! |
===========================================================
Enter the graph configuration file name: graphConfig1.txt
Maze graph construction completed.
You are currently in Room A of the Amazing Maze, you can go North or East. What is your choice? N
You are currently in Room E of the Amazing Maze, you can go East or South. What is your choice? E
You are currently in Room F of the Amazing Maze, you can go North, East or West. What is your choice? E
You are currently in Room G of the Amazing Maze, you can go North, East, South or West. What is your choice? N
You are currently in Room K of the Amazing Maze, you can go East or South. What is your choice? E
Congratulations! You have reached the finish point.
You took 5 steps.
I believe I almost have it complete, but I am having difficulties constructing the get and set statements, as well as the class constructor. Any help on this matter would be much appreciated. Thank you!
Code thus far:
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Node
{
public:
Node();
string name;
Node* north;
Node* south;
Node* east;
Node* west;
Node GetNorth();
Node GetSouth();
Node GetEast();
Node GetWest();
void SetNorth(Node*);
void SetSouth(Node*);
void SetEast(Node*);
void SetWest(Node*);
string GetName();
void SetName(string k);
};
class MazeConstructor
{
private:
Node nodes[12];
public:
Node* ConstructMaze();
};
Node Node:: GetNorth()
{
return *north;
}
Node Node:: GetSouth()
{
return *south;
}
Node Node:: GetEast()
{
return *east;
}
Node Node:: GetWest()
{
return *west;
}
void Node:: SetNorth(Node*)
{
north = *Node;
}
void Node:: SetSouth(Node*)
{
south = *Node;
}
void Node:: SetEast(Node*)
{
east = *Node;
}
void Node:: SetWest(Node*)
{
west = *Node;
}
string Node:: GetName()
{
return name;
}
void Node:: SetName(string k)
{
name = k;
}
Node:: Node()
{
}
Node* MazeConstructor::ConstructMaze()
{
nodes[0] = Node("A");
nodes[1] = Node("B");
nodes[2] = Node("C");
nodes[3] = Node("D");
nodes[4] = Node("E");
nodes[5] = Node("F");
nodes[6] = Node("G");
nodes[7] = Node("H");
nodes[8] = Node("I");
nodes[9] = Node("J");
nodes[10] = Node("K");
nodes[11] = Node("L");
string filename;
int index = 0;
cout << "Please enter the name of the file:";
cin >> filename;
filename += ".txt";
string tempString;
ifstream inStream;
//inStream.open(filename.c_str());
inStream.open("test.txt");
int test = inStream.peek();
while(!inStream.eof() && test != EOF)
{
getline(inStream, tempString);
cout << tempString << endl;
//The north connection
string tempChar = tempString.substr(2,1);
if(tempChar == "*")
{
Node temp("null");
nodes[index].SetNorth(temp);
}
else
{
for(int i = 0; i < 12; i++)
{
if(nodes[i].GetName() == tempChar)
{
nodes[index].SetNorth(nodes[i]);
cout << "Set north to: " <<nodes[i].GetName() <<endl;
}
}
}
//The east connection
tempChar = tempString.substr(4,1);
if(tempChar == "*")
{
Node temp("null");
nodes[index].SetEast(temp);
}
else
{
for(int i = 0; i < 12; i++)
{
if(nodes[i].GetName() == tempChar)
nodes[index].SetEast(nodes[i]);
}
}
//The south connection
tempChar = tempString.substr(6,1);
if(tempChar == "*")
{
Node temp("null");
nodes[index].SetSouth(temp);
}
else
{
for(int i = 0; i < 12; i++)
{
if(nodes[i].GetName() == tempChar)
nodes[index].SetSouth(nodes[i]);
}
}
//The west connection
tempChar = tempString.substr(8,1);
if(tempChar == "*")
{
Node temp("null");
nodes[index].SetWest(temp);
}
else
{
for(int i = 0; i < 12; i++)
{
if(nodes[i].GetName() == tempChar)
nodes[index].SetWest(nodes[i]);
}
}
}
inStream.close();
Node* startNode;
startNode = &nodes[0];
}
class Navigation
{
private:
int stepsTaken;
public:
Navigation(){ stepsTaken = 0;}
void NavigateMaze(Node* startNode);
};
void Navigation::NavigateMaze(Node* startNode)
{
bool finished = false;
string direction;
while(!finished)
{
cout << "GIMME A DIRECTION";
cin >> direction;
if(direction == "N")
{
cout << "Before" <<endl;
startNode = startNode->GetNorth();
cout << "After" <<endl;
}
else if(direction == "E")
startNode = startNode->GetEast();
else if(direction == "S")
startNode = startNode->GetSouth();
else if(direction == "W")
startNode = startNode->GetWest();
else if(startNode->GetName() == "L")
{
cout << "You finished!" << endl;
finished = true;
}
}
}
int main()
{
MazeConstructor mazeConstructor;
Navigation mazeNavigator;
Node* startNode = mazeConstructor.ConstructMaze();
mazeNavigator.NavigateMaze(startNode);
return 0;
}
|