Saving and loading a level

Hi. I've been writing a text adventure game that's based in a dungeon. I've managed to do most of what I've wanted to do, but I've come across one problem that I can't figure out. I've been trying to implement a save and load mechanic to my code. This is so that a user can save whereever they are and can load where they left off. I also want it to save whatever is in the inventory as well. That's the only thing that I'm struggling on at the moment, the saving and loading. I've searched around for a bit and tried to use fsteam but ccan't get my head around it.

Here's the code and see what you think. Yeah, I know that I'm using things like system("") in my code. At the moment I'm just writing the code to improve my knowledge and won't be using that in the future, but for now i'm just using it in this code.

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
#include <iostream> 
#include <conio.h> 
#include <Windows.h>
#include <string> 
#include <fstream>

#include "Room.h" 
#include "Inventory.h" 
#include "Player.h"

using namespace std;

const int NUM_OF_ITEMS = 5;

void Menu();
void NewGame();
void About();
void Help();
void displayInv(); 
void saveGame();
//void loadGame();


void Clearscreen();
int MenuNum = 0;

Room rooms[15];
Player thePlayer(0);

fstream filestr;
string line;
int pos = 0;

string validExits(int currentPos);
int Move(int currentPos, char direction);

int main()
{

	filestr.open("Room.txt");

	while (!filestr.eof())
	{
		getline(filestr, line);
		rooms[pos].setDescription(line);

		getline(filestr, line);
		rooms[pos].setNorth(stoi(line));

		getline(filestr, line);
		rooms[pos].setEast(stoi(line));

		getline(filestr, line);
		rooms[pos].setSouth(stoi(line));

		getline(filestr, line); 
		rooms[pos].setWest(stoi(line));

		pos++;
	}
	filestr.close();
	
	Menu();

	return 0;

}

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
void Menu()
{
	//char choice;
	 
	do
	{
		system("CLS");
		cout << "*************************************\n";
		cout << " 1 - Start the game.\n";
		cout << " 2 - About.\n";
		cout << " 3 - Help.\n";
		cout << " 4 - Exit.\n";
		cout << "*************************************\n";

		cout << endl;

		cout << "Enter your choice and press return : ";

		cin >> MenuNum;

		if (MenuNum == 1)
		{
			NewGame();
		}
		else if (MenuNum == 2)
		{
			About();
		}
		else if (MenuNum == 3)
		{
			Help();
		}
		else if (MenuNum >= 5)
		{
			cout << endl;
			cout << "You have entered an Invalid Input. Enter a vaild number : ";
		}
	} while (MenuNum != 4);


} // void Menu().

void NewGame()
{
	char choice; 

	thePlayer.setInventory("Sword");

	system("cls"); 
	cout << "The Text Adventure" << endl;
	cout << " N = North " << endl;
	cout << " E = East  " << endl;
	cout << " S = South " << endl;
	cout << " W = West  " << endl;
	cout << " Q = Quit  " << endl << endl; 

	cout << " Press F to save your game." << endl;
	cout << " Press C to continue your game." << endl << endl;

	cout << " P = Pick up an item you want." << endl;
	cout << " D = Drop the selected item on the floor." << endl << endl;

	cout << "Description = " << rooms[thePlayer.getPlayerPos()].getDescription() << endl;
	cout << "Valid Exits = " << validExits(thePlayer.getPlayerPos()) << endl << endl;

	cout << "Room contains = " << rooms[thePlayer.getPlayerPos()].getInventory() << endl;
	cout << "You have      = " << thePlayer.getInventory() << endl;

	cout << "Enter : ";
	choice = toupper(_getch());

	while (choice != 'Q')
	{

		if ((choice == 'P') && (rooms[thePlayer.getPlayerPos()].getInventory() != "nothing"))
		{
			if (thePlayer.getInventory() == "nothing")
			{
				thePlayer.setInventory(rooms[thePlayer.getPlayerPos()].getInventory());
				rooms[thePlayer.getPlayerPos()].setInventory("nothing");
			}
		}

		if ((choice == 'D') && (rooms[thePlayer.getPlayerPos()].getInventory() != "nothing"))
		{
			if (thePlayer.getInventory() != "nothing")
			{
				rooms[thePlayer.getPlayerPos()].setInventory(thePlayer.getInventory());
				thePlayer.setInventory("nothing");
			}
		}
		
		thePlayer.setPlayerPos( Move(thePlayer.getPlayerPos(), choice) );

		system("cls");
		cout << "The Dungeon" << endl;
		cout << " N = North " << endl;
		cout << " E = East " << endl;
		cout << " S = South " << endl;
		cout << " W = West " << endl;
		cout << " Q = Quit" << endl << endl;

		cout << " P  = PICK UP" << endl;
		cout << " D  = DROP" << endl << endl;

		cout << " Press F to save your game." << endl;
		cout << " Press C to continue your game." << endl << endl;

		cout << " Description = " << rooms[thePlayer.getPlayerPos()].getDescription() << endl;
		cout << " The valid exits are = " << validExits(thePlayer.getPlayerPos()) << endl << endl;

		cout << " The room contains = " << rooms[thePlayer.getPlayerPos()].getInventory() << endl;
		cout << " You have          = " << thePlayer.getInventory() << endl << endl;

		cout << "Enter : ";
		choice = toupper(_getch());
	}


}

string validExits(int currentPos)
{
	string result = " ";

	if (rooms[currentPos].getNorth() != NO_EXIT)
	{
		result = result + "N ";				//result = "N ";
	}

	if (rooms[currentPos].getEast() != NO_EXIT)
	{
		result = result + "E ";				//result = "N ";
	}

	if (rooms[currentPos].getSouth() != NO_EXIT)
	{
		result = result + "S ";				//result = "N ";
	}

	if (rooms[currentPos].getWest() != NO_EXIT)
	{
		result = result + "W ";				//result = "N ";
	}

	return result;
} // string validExits(int currentPos)

int Move(int currentPos, char direction)
{
	if ((direction == 'N') && (rooms[currentPos].getNorth() != NO_EXIT))
	{
		currentPos = rooms[currentPos].getNorth();
	}

	if ((direction == 'E') && (rooms[currentPos].getEast() != NO_EXIT))
	{
		currentPos = rooms[currentPos].getEast();
	}

	if ((direction == 'S') && (rooms[currentPos].getSouth() != NO_EXIT))
	{
		currentPos = rooms[currentPos].getSouth();
	}

	if ((direction == 'W') && (rooms[currentPos].getWest() != NO_EXIT))
	{
		currentPos = rooms[currentPos].getWest();
	}

	return currentPos;

}

Last edited on
You have a basic file read procedure, there. It needs a bit more checking to ensure that the file object remains valid during the middle of a loop, but I don't see anything fundamentally wrong with it. What's the problem you're having, or why do you think this is wrong?
I'm having problems trying to add a save and load mechanic to the code. I want it so that when the player presses a certain letter, the code would save the player location and whatever was in the inventory at the time of saving. Then from the Main menu they could load the save and continue where they left off, if that makes sense.

Topic archived. No new replies allowed.