The only thing that I can't really figure out is why when the traverse method (which returns a pointer to a Location) is called, the program crashes.
I've tried putting in debug prints at various points in this method and so far have NOT gotten any consistent readout of the problem.
As I see it, it should initialize a sentinel pointer to ConnectionNode as the head of the list of Connections (a ConnectionNode) of the current Location, then traverse the list as long as the shortcut for the sentinel's current Connection is not equal to inputChar. Then when it finds a match it should return the Location pointer in the Connection for which the shortcut matched.
#include <iostream>
#include <string>
using namespace std;
class Location;
class Connection{
public:
string direction;
char shortcut;
string description;
Location* connectionDestination;
Connection(string constructorDirection, char constructorShortcut, string constructorDescription){
direction = constructorDirection;
shortcut = constructorShortcut;
description = constructorDescription;
connectionDestination = NULL;
}
};
struct ConnectionNode{
ConnectionNode* next;
Connection* data;
};
class Location{
public:
string name;
string description;
ConnectionNode* connectionListHead;
Location(string constructorName, string constructorDescription){
name = constructorName;
description = constructorDescription;
connectionListHead = NULL;
}
void AddConnection(Connection* connectionToAdd){
ConnectionNode*elem = new ConnectionNode;
elem->data = connectionToAdd;
//if the list of connections is currently NULL
if (connectionListHead == NULL){
elem->next = NULL;
connectionListHead = elem;
}
//add new connection to end of list
else{
elem->next = connectionListHead;
connectionListHead = elem;
}
}
Location*traverse(char inputChar){
ConnectionNode*elem = connectionListHead;
while(elem->data->shortcut != inputChar){
elem = elem->next;
}
return elem->data->connectionDestination;
}
};
int main(){
//create our world
Location*roomC1 = new Location("Empty corridor",
"You are in a dimly lit corridor with\npassageways to the north and west.");
Connection*linkC1toB1 = new Connection("West", 'w', "Passageway to the west");
roomC1->AddConnection(linkC1toB1);
Connection*linkC1toC2 = new Connection("North", 'n', "Passageway to the north");
roomC1->AddConnection(linkC1toC2);
//initialize game state variables
bool quit = 0;
char inputChar;
Location*currentRoom = roomC1;
ConnectionNode*currentConnection = NULL;
//main loop
while (!quit){
//display current room information
cout << endl << currentRoom->name << endl;
for (int i=0; i<currentRoom->name.length(); i++) cout << "-";
cout << endl << currentRoom->description << endl << endl;
currentConnection = currentRoom->connectionListHead;
//list all the connections to currentRoom
while (currentConnection != NULL){
cout << "-> " << currentConnection->data->direction
<< " [" << currentConnection->data->shortcut << "]"
<< " - " << currentConnection->data->description << endl;
currentConnection = currentConnection->next;
}
cout << "-> Quit [q] - Exit game (progress will be lost)" << endl;
cin >> inputChar;
//set currentRoom equal to a pointer to the room whose connectionNode in the
//current room has shortcut == inputChar
if (inputChar == 'q') quit = 1;
else{
currentRoom = currentRoom->traverse(inputChar);
}
}
}






