I'm working on a text adventure

If anyone knows how to shorten this I would be greatful. Also, I'm stuck towards the end where it gives you either option 1 or 2.


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
#include <iostream>
using namespace std;
#include <string>;
#include <Windows.h>


string answer, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10;
int a, b;


int main()
{
	cout << "INSRUCTIONS: This is a text adventure. You will be forced to make decisions that will ultimately decide if you live or die. Some decisions require a 'yes' or a 'no,' while others require you to type in the correct number. That is all you need to know! "<<endl;
	system("pause");
Beginning:
	system("CLS");
	cout << "You're a secret agent working for the American Government. Your mission is to infiltrate a Russian prison and break out a fellow American who is being held hostage there. "<<endl;
	cout << "The person you are breaking out is a white female. She is about 19 years old andis named Nicole. Break her out before the Russians are able to interrogate her successfully. "<<endl;
	cout << "Your codename will be Boris. We've arranged a plane to take you there. You'll   ";
	cout << "receive further orders once in Moscow."<<endl;
	cout << "Do you accept your mission agent?"<<endl;
		cin >> answer;
		//continues after yes
		if (answer == "yes" || answer == "Yes")
		{	cout << "Goodluck agent. ~END TRANSMISSION~"<<endl; }

		//continues after no
		else if (answer == "No" || answer == "no")
		{   cout << "I am dissapointed in you..."<<endl;
		Sleep (2 * 1000);
		system("CLS");
		cout << "I must regretfully inform you that you will not be allowed to leave. You will"<<endl; 
		cout << "now die."<<endl;
		Sleep (4 * 1000);
		a = 1;
		b = 500;
		while (a < b)
	{ cout << "-  -    -      - -  -    -  - -   -     -       -   -    -   - -    -  -";
	a = a + 1; }
		system("CLS");
		cout << "YOU DIED";
		Sleep (4 * 1000);
		system("CLS");
		cout << "Would you like to restart?"<<endl;
		cin >> answer1;

		if ( answer1 == "yes" || answer1 == "Yes")
		{ goto  Beginning; }
		
		else if (answer1 == "no" || answer1 == "No")
			{ return 0; }
		}

		else if (answer != "yes" && answer != "no")
			{ cout << "Did you read the instructions? Either you didn't read the instructions or your just plain stupid."<<endl;  //security protocol if someone doesnt answer yes or no
		Sleep (4* 1000);                                                                                                          
		return 0; }
		//continues after yes

	Sleep (2 * 1000);
	system ("CLS");

	a = 1;
	b = 500;

	while (a < b)
	{ cout << "-  -    -      - -  -    -  - -   -     -       -   -    -   - -    -  -";
	a = a + 1; }

	system ("CLS");
	cout << "As you walk out of the Moscow International Airport, a dark black van with tinted windows pulls up. The van door slides open. ";
	cout << "A man wearing shades beckons you in. ``I am your supplier. Get in,'' he whispers. DO YOU TRUST HIM?"<<endl;
	cin >> answer2;

	if (answer2 == "yes")
	{ goto yes;  }

	if (answer2 == "no")
	{ system("CLS");
	cout << "The man waves frantically. ``Get in! There's not much time!'' SHOULD YOU ENTER?"<<endl; }
		cin >> answer3;
		if (answer3 == "yes")
		{ goto yes; }

		else if (answer3 == "no")
		{ system ("CLS");            
	cout << "``You are making big mistake!'' he shouts. The door slams shut and the van squeals away."<<endl;
	cout << "That guy was a weirdo, you think. You decide to head to a bus stop to make it to the prison. All of a sudden you feel something hard press against your back."<<endl;
	cout << "You turn around and there's a guy with a gun aimed at you. ``Do not make this any more difficult for yourself. I know you are an American agent. Do you know what ";
	cout << "the Russiam Government does to spies? It's not pretty,'' he laughs.             ~12 HOUR LATER~  You are tried in court and convicted guilty of being a spy. The punishment is torture until death."<<endl;
	cout << "GAME OVER"<<endl;
	system("pause");

	a = 1;
	b = 500;

	while (a < b)
	{ cout << "-  -    -      - -  -    -  - -   -     -       -   -    -   - -    -  -";
	a = a + 1; }  
		system("CLS");
		cout << "Would you like to restart?"<<endl;
		cin >> answer4;

		if ( answer4 == "yes" || answer1 == "Yes")
		{ goto  Beginning; }
		
		else if (answer4 == "no" || answer1 == "No")
			{ return 0; }
	}

		else if (answer2 != "yes" && answer2 != "no")
			{ cout << "Did you read the instructions? Either you didn't read the instructions or your just plain stupid."<<endl;  //security protocol if someone doesnt answer yes or no
		Sleep (4* 1000);                                                                                                          
		return 0; }


		else if (answer3 != "yes" && answer3 != "no")
			{ cout << "Did you read the instructions? Either you didn't read the instructions or your just plain stupid."<<endl;  //security protocol if someone doesnt answer yes or no
		Sleep (4* 1000);                                                                                                          
		return 0; }


		yes:            //yes

	if (answer2 == "yes" || answer3 == "yes")
	{ system("CLS");
	cout << "You step into the van and it speeds off. The man takes off his shades. ``Are you Boris?'' he asks. The man has a rough voice and a heavy Russian accent as if he's ";
	cout << "spent a lot of time helping spies like you out in Russia. ``Da,'' you answer. ``I am Victor. You are being taken you to our secret facility under the American Embassy. I will provide weapons.''"<<endl;
	cout << "                            ~15 MINUTES LATER~"<<endl;
	system("pause");
	system("CLS");
	cout << "Once at the Embassy, you and Victor walk through the entrance. Victor walks over to the counter and shows them a card. The intern tells him to wait in room 333. Victor and you walk along a hallway with numbered doors on either side.Victor is about to say something when his face tightens up and he falls down. "<<endl;
	cout << "YOU HAVE A MOMENT TO REACT AND INSTANTLY TWO OPTONS POP UP IN YOUR HEAD: 1) CHECK HIS BODY FOR SIGNS OF WHAT HAPPENED 2) RUN TOWARDS THE FRONT DESK AND REPORT WHAT HAPPENED? MAKE THE DECISION QUICKLY!"<<endl; 
	}
	cin >> answer5;

	

	a = 1;

	while (a < 500)
	{ 
	
  if (answer5 == "2")                                                              //this is if answer is 2
  { cout << "You rush towards the front desk and tell the clerk what happened. Armed guards are instantly scrambled and you lead them towards the place Victor became unconsious and possibly dead."<<endl;
    cout << "Suddenly your legs go stiff and you begin to fall. Someone catches you, and yelling breaks out. But their voices are distant, hardly noticeble. A wave of peace comes over you, as if all the stress you ever felt is melting away." <<endl ;
	cout <<	"You suddenly don't care if you never wake up. If you never leave this peace. Your wish is granted and you never wake up. You were killed by a vicious toxin known as polonium. The authorities never find who did it. "<<endl;
    Sleep (4 * 1000);
	system("CLS");
		a = 1;
		b = 500;
		while (a <= b)
	{ cout << "-  -    -      - -  -    -  - -   -     -       -   -    -   - -    -  -";
		a = a + 1; } 
		system("CLS");
		cout << "YOU DIED";
		Sleep (4 * 1000);
		system("CLS");
		cout << "Would you like to restart?"<<endl;
		cin >> answer1;

		if ( answer1 == "yes" || answer1 == "Yes")
		{ goto  Beginning; }
		
		else if (answer1 == "no" || answer1 == "No")
			{ return 0; }
  }


  else if (answer5 != "1" && answer5 != "2")
			{ cout << "Did you read the instructions? Either you didn't read the instructions or your just plain stupid."<<endl;  //security protocol if someone doesnt answer yes or no
		Sleep (4* 1000);                                                                                                          
		return 0; }
	






  if (answer5 == "1")  //this is if answer is 1 (CORRECT)
  {   system("CLS");        
  cout << "You grab his shirt and  "<<endl; }
 
  a = a + 1; 
  if (a >= 100)
  { cout << "You wasted too much time and felt a prick in the back of your neck and never woke up."; }
	}




	system("PAUSE");
	return 0; 
}
While it's not exactly pertinent to your question, Windows.h is a huge library to be using for a simple sleep command. I would suggest using ctime instead and building a function like

1
2
3
4
5
6
7
void Timer(int seconds)
{
   clock_t timer;
   timer = clock() + seconds * CLOCKS_PER_SEC;
   while (clock() < timer) {}
}


and if you want to wait for 3 seconds just use Timer(3);

Also, you've written your program entirely in main. While it works, it's kind of like a run on sentance. If you were to partition it into functions, it would be a) easier to read, b) easier to edit, and c)easier to follow paths). Just some suggestions
Last edited on
Windows.h is a huge library to be using for a simple sleep command. I would suggest using ctime instead and building a function like


I would suggest not. What you're suggesting will bring your cpu to 100% utilization for no good reason, whereas Sleep does not.

What might be a reasonable replacement in C++11 is:

1
2
3
4
5
6
7
8
#include <thread>
#include <chrono>

// ...

std::this_thread::sleep_for(std::chrono::milliseconds(4000)) ;

// ... 
How do you partition it into functions? Could you please just give me an example by changing one part of my program?




Also, would it be possible to at the beginning eliminate

string answer, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10;

and only have

string answer;

so that after every cin << it would just replace the last answer? Or would that just create too many unnecessary problems? Sorry about all the questions!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
#include <string>;
#include <Windows.h>

int part1();


string answer, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10;
int a, b;


int main()
{
    if(part1() == 1)
    {
         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
int part1()
{
cout << "INSRUCTIONS: This is a text adventure. You will be forced to make decisions that will ultimately decide if you live or die. Some decisions require a 'yes' or a 'no,' while others require you to type in the correct number. That is all you need to know! "<<endl;
	system("pause");
Beginning:
	system("CLS");
	cout << "You're a secret agent working for the American Government. Your mission is to infiltrate a Russian prison and break out a fellow American who is being held hostage there. "<<endl;
	cout << "The person you are breaking out is a white female. She is about 19 years old andis named Nicole. Break her out before the Russians are able to interrogate her successfully. "<<endl;
	cout << "Your codename will be Boris. We've arranged a plane to take you there. You'll   ";
	cout << "receive further orders once in Moscow."<<endl;
	cout << "Do you accept your mission agent?"<<endl;
		cin >> answer;
		//continues after yes
		if (answer == "yes" || answer == "Yes")
		{	cout << "Goodluck agent. ~END TRANSMISSION~"<<endl; }

		//continues after no
		else if (answer == "No" || answer == "no")
		{   cout << "I am dissapointed in you..."<<endl;
		Sleep (2 * 1000);
		system("CLS");
		cout << "I must regretfully inform you that you will not be allowed to leave. You will"<<endl; 
		cout << "now die."<<endl;
		Sleep (4 * 1000);
		a = 1;
		b = 500;
		while (a < b)
	{ cout << "-  -    -      - -  -    -  - -   -     -       -   -    -   - -    -  -";
	a = a + 1; }
		system("CLS");
		cout << "YOU DIED";
		Sleep (4 * 1000);
		system("CLS");
		cout << "Would you like to restart?"<<endl;
		cin >> answer1;

		if ( answer1 == "yes" || answer1 == "Yes")
		{ goto  Beginning; }
		
		else if (answer1 == "no" || answer1 == "No")
			{ return 1; }
		}

		else if (answer != "yes" && answer != "no")
			{ cout << "Did you read the instructions? Either you didn't read the instructions or your just plain stupid."<<endl;  //security protocol if someone doesnt answer yes or no
		Sleep (4* 1000);                                                                                                          
		return 1; }
}


An example. Personally I like to keep my main function as empty as possible, because debugging is easier when following a path of functions.
@roger911

I wouldn't worry about Sleep

The Sleep function is exported by kernel32.dll

And aven if you don't explicitly link to it, all console programs use the CRT (either as one of the DLLs, like mscvrt.dll, or as a static lib), which links to kernel32.dll. This means that kernel32.dll is allready being mapped into your process, so there's no additional cost when it comes to using the Sleep function.

And on the flip side, it is bad practice to spin an empty loop, as cire has already pointed out, as it burns CPU.

Andy
Topic archived. No new replies allowed.