I am making a maze that consists of 48 nodes and the user needs to start in A1 and make there way to H6. The maze is created by reading a file, each line in the file looks something like A1 A2 B1 - - - where A1 is the current node A2 is the node to the north, B1 is the node to the east and the next place holder is to the south then the next is to the west and the last is a chute or ladder. Not to worried about the chute/ladders currently as it is not in the source yet. It is creating the nodes fine and it appears to link fine when debugged. The CurrentRoom pointer appears to follow the north pointer on the first pass but when
cout << myMaze.get_CurrentRoom()->getNodeName(); on line 348 is passed through again the program has been terminating with a segmentaion fault. Thank you for anything you can help with even if you spot some error in my code. I am new to c++ so please go easy :)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
using namespace std;
class Node
{
public:
Node(){};
Node(string newname)
{
name = newname;
attachedNodes[0] = NULL;
attachedNodes[1] = NULL;
attachedNodes[2] = NULL;
attachedNodes[3] = NULL;
attachedNodes[4] = NULL;
}
void attachLadderChuteNode(Node *newNode);
Node *getLadderChuteNode();
string move_options();
Node *getAttachedNode(int direction)
{
return attachedNodes[direction];
}
void setNodeName(string newname)
{
name = newname;
}
string getNodeName()
{
return name;
}
void attachNewNode(Node *newNode, int direction)
{
if (direction == 0)
{
attachedNodes[0] = newNode;
}
if (direction == 1)
{
attachedNodes[1] = newNode;
}
if (direction == 2)
{
attachedNodes[2] = newNode;
}
if (direction == 3)
{
attachedNodes[3] = newNode;
}
if (direction == 4)
{
attachedNodes[4] = newNode;
}
}
private:
string name;
Node *attachedNodes[4];
Node *ladderOrChuteNodes;
};
class MazeMovement
{
private:
int StepsTaken;
bool MazeDone;
string Name;
vector<Node> Rooms;
Node *CurrentRoom;
public:
MazeMovement() {StepsTaken = 0;}
bool is_MazeDone();
void Movement(char Direction);
Node* get_CurrentRoom();
Node get_CurrentNode();
int get_StepsTaken();
void Read_Maze(string file);
void addRoom(Node roomIn);
Node getAt(int i);
Node find_Node(string Name);
void updateNode(Node n);
void set_CurrentRoom(Node *n);
string getName();
};
void MazeMovement:: addRoom(Node roomIn) //Adds the nodes to a vector
{
Rooms.push_back(roomIn);
}
Node MazeMovement:: getAt(int i) //Gets the node at the specified index in the vector
{
return Rooms[i];
}
Node* MazeMovement:: get_CurrentRoom()
{
return CurrentRoom;
}
Node MazeMovement:: get_CurrentNode()
{
return *CurrentRoom;
}
void MazeMovement:: set_CurrentRoom(Node *n)
{
CurrentRoom = n;
}
int MazeMovement:: get_StepsTaken()
{
return StepsTaken;
}
bool MazeMovement:: is_MazeDone()
{
if(get_CurrentRoom()->getNodeName() == "H6")
{
MazeDone = true;
}
return MazeDone;
}
void MazeMovement:: Movement(char Direction)
{
string Moves = get_CurrentRoom()->move_options();
switch(Direction)
{
case 'N':
case 'n':
size_t nfound;
nfound = Moves.find("North");
if(int(nfound) >= 0)
{
CurrentRoom = CurrentRoom ->getAttachedNode(0); //suppposed to set the current room to the node from the chosen pointer
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 -> getAttachedNode(1);
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 -> getAttachedNode(2);
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 -> getAttachedNode(3);
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::in);
int test = inStream.peek();
int i = 0;
if (!(inStream.fail()))
{
while(getline(inStream, line))
{
Node nodeIn;
string north, south, east, west;
int position1, position2;
position1 = line.find(' ');
nodeIn = find_Node(line.substr(0, position1));
position2 = line.find(' ', position1 + 1);
north = line.substr(position1 + 1, (position2 - position1) - 1); //Get the north node
position1 = line.find(' ', position2);
position2 = line.find(' ', position1 + 1);
east = line.substr(position1 + 1, (position2 - position1) - 1);//Get the east node
position1 = line.find(' ', position2);
position2 = line.find(' ', position1 + 1);
south = line.substr(position1 + 1, (position2 - position1) - 1);//Get the south node
position1 = line.find(' ', position2);
position2 = line.find(' ', position1 + 1);
west = line.substr(position1 + 1, (position2 - position1) - 1);//Get the west node
if(!(north[0] == '*'))//Checks to see if the node is valid, and if it is sets them
{
Node North = find_Node(north);
nodeIn.attachNewNode(&North, 0);
}
if(!(east[0] == '*'))
{
Node East = find_Node(east);
nodeIn.attachNewNode(&East, 1);
}
if(!(south[0] == '*'))
{
Node South = find_Node(south);
nodeIn.attachNewNode(&South, 2);
}
if(!(west[0] == '*'))
{
Node West = find_Node(west);
nodeIn.attachNewNode(&West, 3);
}
updateNode(nodeIn);
CurrentRoom = &Rooms[0];
}
}
else
{
cout << "Could not open the file name entered!" << endl;
exit(1);
}
}
string Node::move_options()
{
string options;
if(!(attachedNodes[0] == NULL))
{
options += "North\n";
}
if(!(attachedNodes[2] == NULL))
{
options += "South\n";
}
if(!(attachedNodes[1] == NULL))
{
options += "East\n";
}
if(!(attachedNodes[3] == 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(string Name)
{
Node found;
for(int i=0; i < Rooms.size(); i++)
{
string current = Rooms[i].getNodeName();
if(strcmp(current.c_str(), Name.c_str()) == 0)
{
found = Rooms[i];
return found;
}
}
return found;
}
void MazeMovement:: updateNode(Node n)
{
for(int i=0; i < Rooms.size(); i++)
{
string updatedNode = n.getNodeName();
string current = Rooms[i].getNodeName();
if(strcmp(current.c_str(), updatedNode.c_str()) == 0)
{
Rooms[i] = n;
}
}
}
string MazeMovement:: getName()
{
string name = get_CurrentRoom()->getNodeName();
return name;
}
int main()
{
string FileName;
MazeMovement myMaze;
ostringstream in;
for (int i = 1; i < 7; i++) { //This creates all 48 nodes and puts them in a vector
char nodeChar = 'A';
for (int j = 0; j < 8; j++) {
in << i;
string nodeName = nodeChar + in.str();
in.str("");
Node node(nodeName);
myMaze.addRoom(nodeName);
nodeChar++;
}
}
cout << "Please enter the name of the file: ";
getline(cin,FileName);
FileName += ".txt";
myMaze.Read_Maze(FileName);
cout << "========================================================================= " << endl;
cout << " Welcome to the Ladder and Chute Maze! " << endl;
cout << "========================================================================= " << endl;
do
{
string SelectedDirection;
char selection;
cout << "You are currently in Room ";
cout << myMaze.get_CurrentRoom()->getNodeName();
cout << " of the Ladder and Chute 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;
exit(1);
}
return 0;
}