Text based adventure storylines colliding!

I want to create a text based adventure where the options can collide into one option. So for example, say renaming a city. It would not have much effect on the game but would I have to code the same story line in the two different possibility spaces?

Here is my noob begginer code so far:

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  #include <iostream>
#include <string> 
#include <Windows.h> 
#include <stdlib.h>

using namespace std;

bool gameRunning = true;
int rand1to100 = rand() % 100 + 1;

void Totalotarian()
{

}

int main()
{
	while (gameRunning == true)
	{
		//system("Color 0a");
		cout << " !*PRE - ALPHA EXPECT SPELLING ERRORS AND GLITCHES*! Welcome to World History \n adventures! To play press the \" p \" and \"enter\" . To quit at any time press \" esc \". \n Please enjoy the adventure! Lead writer: Locke Thornburge, \n Lead Programmer Matthew Byrd" << endl;
		string startD;
		cin >> startD;
		if (startD == "p")
		{
			cout << "You want to start your civilization. You come across a river vally. \n What do you do? (s = settle, l = leave)" << endl;
			string secondD;
			cin >> secondD;
			if (secondD == "s")
			{
				cout << "you settle a wonderous civilization! However it appears to be missing \n something, what? (Hint: Mr. Barhams website. Alphabetical order \n (!*ALPHA*! the answer is \"TEMP\")" << endl;

				string thirdD;
				cin.ignore();
				getline(cin, thirdD);
				if (thirdD == "TEMP")
				{
					cout << "Your civilization's culture is booming! Insert a random number to have a \n event occur!" << endl;
					string fourthD;
					cin >> fourthD;

					if (fourthD == "1")
					{
						cout << "Your civilization devlops a wonder of the world! Your citizens rejoice! Press \"c\" to continue!" << endl;
						string decisionA1; //Start of culture campaign 
						cin >> decisionA1;

						if (decisionA1 == "c")
						{
							cout << "welcome to a new era! Your civ gains writing, sailing, calendar, trapping, the wheel, masonry, and bronze working. What do you want to achieve with these technologys? (p = pyrimids, g = great wall, s = statues, " << endl;

						}

						else
						{
							cout << "u wot m8?" << endl;
							Sleep(2000);
							return 0;
						}

					}

					if (fourthD == "420")
					{
						cout << "You disoverd hemp!" << endl;
					}
				
					if (fourthD == "666")
					{
						cout << "why?" << endl;
						Sleep(2000);
						return 0;
					}


				



					else
					{
						cout << "Your civilizaion discovers gold mining! Your citizens suggest to use it for holy structures, however you could use this to trade with neighboring civilizations. What do you do? (r = holy structures, t = Trade with neighbors)" << endl;
						string decisionB1;
						cin >> decisionB1;
						if (decisionB1 == "r")
						{
							cout << "You build great structures however enslave your pepole to acheive this. press \"c\" to continue" << endl;
							string continuationB1;
							cin >> continuationB1;
							if (continuationB1 == "c")
							{

							}

							else
							{
								cout << "u wot m8?" << endl;
								Sleep(2000);
								return 0;
							}

						}


						if (decisionB1 == "t")
						{
							cout << "You trade away your gold for goods and now have the option to keep these resources for personle use or provide them to your pepole. (k = Keep, p = Give to pepole)" << endl;
							string decisionB2;
							cin >> decisionB2;
							if (decisionB2 == "k")
							{
								cout << "You civilization grows in goverment power. You can either establish a military, or establishment of a religon promoting government. (\"m\" = military, \"r\" = religon" << endl;
								string TotalotarianD; // start of Totalotarian campaign
								cin >> TotalotarianD;

								if (TotalotarianD == "m")
								{


									{
										cout << "You establishment of a military ensures protection of your civilization for decades to come. Welcome to a new era. Your civ gains writing, sailing, calendar, trapping, the wheel, masonry, and bronze working. What do you want to achieve with these technologys? Establish a new city (c)" << endl;
										string totalotarianD2;
										cin >> totalotarianD2;
										if (totalotarianD2 == "c")
										{
											string cityName;
											cout << "What would you like to name your city?" << endl;
											cin.ignore();
											getline(cin, cityName);
											cout << cityName << " has been established" << endl;
											Sleep(2000);
											string  cityDecision;
											cout << "How much protection do you provide in the city? (No wrong answer) " << endl;
											cin.ignore();
											getline(cin, cityDecision);
											if (cityDecision == "condom")
											{
												cout << "ur bad, git ratketd. Ur civ burns and you suck." << endl; 
											}
											cout << cityDecision << " applied to " << cityName << endl;
											Sleep(2000);
											string gameChangeT;
											cout << "You meet a new starter - up civilization, what do you do? Attack or leave (a for attack, \"l\" for leave" << endl;
											cin >> gameChangeT;

											if (gameChangeT == "l")
											{
												cout << "You decieded to leave the civilization alone, before you military leaves here a conspiracy of a suprise attack comes from the starter up, what do you do? Destory them? Or dismiss the conspiracys as mere folly. (\"d\" = Destory, \"f\" = Folly" << endl; 
												string endgame;
												cin >> endgame;

												if (endgame == "d")
												{
													cout << "Attacking them based on unjust claims caused you civilization to attack you :[" << endl;
												}


												if (endgame == "f")
												{
													cout << "Turns out it twas not folly :[ your civilization fails!" << endl; 
												}

											}

											if (gameChangeT == "a")
											{
												cout << "Your civilization attacks" << endl;
												if (rand1to100 <= 90)
												{
													rand1to100 = rand() % 100 + 1;
													cout << "Your civilization was sucsesfull the starter up fails. What would you like to do with the city? \"r\" = raze";
													string totalotarianD3;
													cin >> totalotarianD3;
													if (totalotarianD3 == "r")
													{
														Sleep(2000); 
														cout << "The city of mesopotamia burns, your citizens enjoy the light show. Welcome to a new era! You unlock chivalry, Civil service, Compass, Education, Guilds, Machinry, Metal casting, Physics, Steel, and Theology! What do you use these technologys for? Establish a new monarchy system? Create newer more adaptive city defenses for " << cityName << "? Expand boarders through military conquest? c = city defenses, e = expand, m = monarch" << endl;
													}
												}
												}
											}
										}

										if (decisionB2 == "p")
										{
											cout << "By giving to the pepole your civilization has established trust in the government and gained more workers. Welcome to a new era. Your civ gains writing, sailing, calendar, trapping, the wheel, masonry, and bronze working. What do you want to achieve with these technologys? Esablish a theatre (t) Eastablish a early form of democracy (d)" << endl;
											string DemocraticD1; //start of democratic campaign
											cin >> DemocraticD1;

											if (DemocraticD1 == "t")
											{
												cout << "Your cuture provides happiness to all who surround it. There is a period of peace and prosparity. The lack of government causes citizens to worry, trade routes are begging to intercept your city and your economy is growing. However without a government run protection service, the crime is starting to increase. You have two options, either form a millitary or start democratic form of government. What do you do? (\"d\" = democratic, \"m\" = millitary" << endl;
												string DemocraticD2;
												cin >> DemocraticD2;

												if (DemocraticD2 == "m")
												{

												}

												if (DemocraticD2 == "d")
												{

												}



											}

											if (DemocraticD1 == "d")
											{

											}
										}
									}
								}
							}
						}

						if (secondD == "l")
						{
							cout << "you die of dehydration idiot" << endl;
						}
					}
					}
				}
			}
		}
	
I think you should try to represent the state of the game using data. And then you run different code depending on the state of the data.

The way you have it now the position in the code decides the state of the game, and that is very inflexible because the code only runs in one direction. It's hard to jump from one state to another without duplicating code.

So you you could have a variable that contains information about where you are in the game. For your game an enum (or int) is probably all you need. Then you can use a switch statement to decide what code to run, and that code will change the state to a different state.

Something like this:
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
enum class GameState
{
	start_game,
	start_civilization,
	// ...
	quit
};

int main()
{
	GameState gameState = GameState::start_game;
	while (gameState != GameState::quit)
	{
		switch (gameState)
		{
			case GameState::start_game:
			{
				cout << " !*PRE - ALPHA EXPECT SPELLING ERRORS AND GLITCHES*! Welcome to World History \n adventures! To play press the \" p \" and \"enter\" . To quit at any time press \" esc \". \n Please enjoy the adventure! Lead writer: Locke Thornburge, \n Lead Programmer Matthew Byrd" << endl;
				string startD;
				cin >> startD;
				if (startD == "p")
				{
					gameState = GameState::start_civilization;
				}
				else if (startD == "esc")
				{
					gameState = GameState::quit;
				}
				break;
			}
			case GameState::start_civilization:
			{
				cout << "And so on ..." << endl;
				gameState = GameState::quit;
				break;
			}
			// ...
			case GameState::quit:
			{
				cout << "Bye!" << endl;
				break;
			}
		}
	}
}


Notice how easy it is to jump from one state to another by just setting the gameState variable to whatever you want.
I am having trouble understanding how these variables interact could you do a more specific example?

Also here is a flow chart, where attack or leave is it needs to link into the democratic branch could you send me an example of that code? https://www.lucidchart.com/documents/view/79492c58-6876-49f8-b70b-b4f7a56c7d7b
Last edited on
Do you know how to use classes (OOP)? Or at least create structures for your data and functions to manipulate that data?

If not you will be in for a world of spaghetti code that will get out of control. Text adventure games are NOT simple just because they don't use graphics. They're a great way to keep you interested while you learn more advanced coding techniques though... hint, hint. ;-)
I know classes but I don't know how to access certain strings in classes. I would love to learn in patience but I need to create this for a project in my world history class and can't afford to restart now. I really need an example and should be able to go from there.
I am having trouble understanding how these variables interact

Do you understand the concept of a state variable? A player can in in only one place at a time. That is represented by a state variable.

Lines 3-6 are enumerated values that give a name to each "state" of the game. Add named values here as you add states to your game.

Line 11: gameState is an enumerated variable of type GameState that controls the state of the game.

Line 12 loops until you quit the game by setting gameState to quit.

Line 14 jumps to the case of the switch statement corresponding to the value of gameState. Add a case to the switch statement for each "state" you add to your game. You will stay in the same state (case) until you explicitly change the value of gameState.

could you do a more specific example?
Peter87 has given you an excellent starting example. Simply extend it by adding state names to the enum and cases to the switch statement. We not going to writie your homework for you.

As for not having time to start over, you are going to spend far more time struggling with your program the way you have it.



Last edited on
Ahh buggar I did not mean for you to write my homework, sorry.

how do I declare the states? That is what I am really having problems with.

Say I want one line of story to loop around to another line so here is an examople

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "you come across a lake what do you do? swim or don't swim" << endl; 
string decision
cin >> decision 
if (decision == swim)
{
cout << "You continue your adventure" << endl; 
}

if(decision == don't swim)
{
cout << "You continue your adventure" << endl; 
}
           
Then I want these to loop into this or something 

cout << "X event" << endl; 


 


That is what I help with, sorry for being rather rude.
Not sure why you have an if statement for both conditions (lines 4,9), if the message is the same.

Add to the enum:
1
2
3
4
     found_lake,
     continue_swim,
     continue_dont_swim,
     event_x,


Add to the switch statement:
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
 
        //  Add to some existing case
        gameState = found_lake;  // Set next state 
        break;
 
    case found_lake: 
         cout << "you come across a lake what do you do? swim or don't swim" << endl; 
        string decision
        cin >> decision 
        if (decision == "swim")
            gameState = continue_swim;
        else
           if (decision == "don't swim")
             gameState = continue_dont_swim;
        // If not "swim" or "don't swim", stay in the same state (repeat the description and question.
        break;

    case continue_swim:
        cout << "You swim across the lake" >> endl;
        gameState = event_x;   // Set the next state 
        break;

    case continue_dont_swiim:
        cout << "You walk around the lake" << endl;
        gameState = event_x;  //  Set the next state
        break;

    case event_x:
       // do whatever event x does.  Set next state.
       break;


Getting the idea?

Last edited on
I am getting it, thanks!

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

enum class Gamestates
{
	start_game,
	end_game,
	totalotarian_campaign,
	democratic_campaign,
	culture_campaign,
	military_campaign,
	religon_campaign,
};

int main()
{
	Gamestates gameState = Gamestates::start_game;

		while (gameState != Gamestates::end_game)
		{
			switch (gameState)
			{
			case Gamestates::start_game:
				{
				
				}
			}
		}
}



However I can't seem to use cout or anyhting else for that matter without getting an error " Error 1 error C2065: 'cout' : undeclared identifier c:\users\soult_000\onedrive\school\world history\projects\danktank420\project1\project1\test.cpp 27 1 Project1
"
You dont have using namespace std;

But I'd recommend starting to do std::cout and std::endl and std::string etc
I understand the difference there but what advantage does using std every time serve over not?
Why is using namespace std; bad? http://stackoverflow.com/q/1452721/1959975 has plenty of explanations.
Last edited on
Topic archived. No new replies allowed.