Need help with simple linked list maze

Hi my names Angelica and I'm new to programming.
I'm doing terrible at this so a few questions.
Just need to know how to code "my current room." location. Using the hero and switch and how to code moving into the next room using the set up from my map function.

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Rooms
{
public:
	Rooms(const string& name = "");
	Rooms*GetNorth();
	Rooms*GetSouth();
	Rooms*GetEast();
	Rooms*GetWest();
	void SetNorth(Rooms*next);
	void SetSouth(Rooms*next);
	void SetEast(Rooms*next);
	void SetWest(Rooms*next);

private:
	Rooms* m_pNorth;
	Rooms* m_pSouth;
	Rooms* m_pWest;
	Rooms* m_pEast;

	string m_Name;
	vector<string> RoomInv;

};

Rooms* Rooms::GetNorth()
{
	return m_pNorth;
}
void Rooms::SetNorth(Rooms*next)
{
	m_pNorth = next;
}
void Rooms::SetSouth(Rooms*next)
{
	m_pSouth = next;
}
void Rooms::SetEast(Rooms*next)
{
	m_pEast = next;
}
void Rooms::SetWest(Rooms*next)
{
	m_pWest = next;
}
class Map
{
public:
	Map();
	~Map();
	void RoomInfo();

private:
	Rooms* m_pHead;
};
Map::Map()
{
	Rooms* pNewRoom = new Rooms("Lobby");
	Rooms* pNewRoom2 = new Rooms(" Room1");
	Rooms* pNewRoom3 = new Rooms("Room2");
	Rooms* pNewRoom4 = new Rooms("Room3");
	Rooms* pNewRoom5 = new Rooms("Room4");
	Rooms* pNewRoom6 = new Rooms("Room5");
	Rooms* pNewRoom7 = new Rooms("Room6");
	Rooms* pNewRoom8 = new Rooms("Room7");
	Rooms* pNewRoom9 = new Rooms("Room8");
	Rooms* pNewRoom10 = new Rooms("Room9");


	pNewRoom->SetNorth(pNewRoom5);
	pNewRoom->SetEast(pNewRoom2);
	pNewRoom->SetWest(pNewRoom8);
	pNewRoom2->SetEast(pNewRoom);
	pNewRoom2->SetNorth(pNewRoom3);
	pNewRoom3->SetSouth(pNewRoom2);
	pNewRoom3->SetEast(pNewRoom5);
	pNewRoom3->SetNorth(pNewRoom4);
	pNewRoom4->SetSouth(pNewRoom3);
	pNewRoom4->SetEast(pNewRoom6);
	pNewRoom5->SetSouth(pNewRoom);
	pNewRoom5->SetEast(pNewRoom3);
	pNewRoom5->SetWest(pNewRoom9);
	pNewRoom5->SetNorth(pNewRoom6);
	pNewRoom6->SetSouth(pNewRoom5);
	pNewRoom6->SetEast(pNewRoom4);
	pNewRoom6->SetWest(pNewRoom10);
	pNewRoom6->SetNorth(pNewRoom7);
	pNewRoom7->SetSouth(pNewRoom6);
	pNewRoom8->SetEast(pNewRoom);
	pNewRoom8->SetNorth(pNewRoom9);
	pNewRoom9->SetSouth(pNewRoom8);
	pNewRoom9->SetEast(pNewRoom5);
	pNewRoom9->SetNorth(pNewRoom10);
	pNewRoom10->SetSouth(pNewRoom9);
	pNewRoom10->SetEast(pNewRoom6);
}
class Hero
{
	

public:
	void SetCurRoom(Rooms* next);
	void WalkNorth();
	void WalkSouth();
	void WalkEast();
	void WalkWest();
	
private:
	vector<string> inventory;
	Rooms*CurrRoom;
};

void Hero::SetCurRoom(Rooms*next)
{
	
	CurrRoom = 0;
}

int main()
{
	Rooms myrooms;
	int choice;

	do
	{
		
		cout << "1 - Go north.";
		cout << "2 - Go south.";
		cout << "3 - Go east.";
		cout << "4 - Go west.";
		cin >> choice;

		switch (choice)
		{
		
		case 1: myrooms.GetNorth(); break;
		case 2: myrooms.GetSouth(); break;
		case 3: myrooms.GetEast(); break;
		case 4: myrooms.GetWest(); break;
		}
	} while (choice != 0);
	return 0;
}
Last edited on
I would try something simpler to begin with, just to test how things are working. I would keep the Rooms class, and I would rewrite the main function.
1. Implement the constructor for the Rooms class. You need to set the name, and set all neighbors to NULL. Finish implementing all functions in that class.
2. Write a member function for rooms, to give you the name. That way, when you move to a room, it will tell you where you are.
3. Write a function, with two arguments: a Rooms pointer, and a direction, that returns a Rooms pointer, and call it say Move
Rooms* Move(Rooms*current, int direction)
You need to get the pointer to the next room. So you use a switch, like you have in main, and you assign the next room to be the output of current->GetNorth().
1
2
3
4
5
6
7
8
9
Rooms *next;
switch(direction)
{
case 1:
  next=current->GetNorth();
  break

....//other cases
}

Then you need to check if there is a room like that
1
2
3
4
5
6
7
8
9
10
if(next==NULL)
{
  cout<<"cannot go in that direction"<<endl;
  return current; //You do not go to a next room if one does not exists. 
}
else
{
   cout<<"I can move to room "<<next->getName()<<endl;
   return next;
}


4. Set up your rooms in the main function, similar to what you have currently in Map
5. Try to traverse a few rooms:
1
2
3
4
5
cout<<"I am in room "<<pNewRoom->getName()<<endl;
Rooms * next;
next=Move(pNewRoom, 1);//move north
next=Move(next, 2);
...


Here is also a very important point: whenever you have pointers, make sure you delete them. Otherwise your program will leak memory
That sounds like great advice but I'm having trouble implementing it. :(( I just got into the linked list thingy and it's really hard for me. Where do I put those and how do I generate the current room. And how would the member function look for giving you the name of the current room? I am so stuck at the moment.
Topic archived. No new replies allowed.