I'm trying to implement a maze program by using nodes that reads in a file and the user is able to enter a direction and solve the maze. My class will not output correctly and I've been working on it for a LONG time. Any help is appreciated! I also included the maze configuration that the project is supposed to read and solve.
#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;
}
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 Amazing Maze Program! " << 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.";
char a;
bool found = false;
ifstream InFile;
Node *Root[20][20] = {NULL};
InFile.open("maze.txt");
if(!a)
{
cout << "Could not open!" << endl;
return 1;
}
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;
}
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(), ios:: app);
int i = 0;
while(!(inStream.eof()))
{
getline(inStream, line);
Node n(line[0]);
Rooms[i] = n;
i++;
}
inStream.close();
inStream.open(FileName.c_str(), ios:: app);
while(!(inStream.eof()))
{
getline(inStream, line);
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');
}
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;
}