Commander CLI Game playing up

Hi so I'm following a tutorial on making a CMD game with mages, warriors and archers. Everything seems fine EXCEPT when I try to send in anything except a warrior. I've checked my code and it's identicial to the warriors code so I must be missing something big.
This is my first big tutorial with so much nesting so it's certinatly possible, I would be very thankful if anyone could help me out here. Also I had to put in srand ( (unsigned int)time(NULL) ); instead of the srand(time((0) or however it is because otherwise I get thrown an error sadly...

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

using namespace std;

int main()
{
	/*********************************** Variable Declartion and Background Work*******************************/

	srand ( (unsigned int)time(NULL) );

	string name;
	bool quit = false;
	bool lost;

	int menuChoices;

	// Our Unit

	int warriors = 25;
	int mages = 50;
	int archers = 100;

	// Enemy Units

	int e_warriors = rand() % (45) + 1;
	int e_mages = rand() % (70) + 1;
	int e_archers = rand() % (120) + 1;


	int warriors_menu, mages_menu, archers_menu;
	int battle_menu;



	/*********************************** End off Variable Declartion and Background Work*******************************/

	cout << "Welcome Sir what is your name?\n";
	cin >> name;
	cout << "Well, " << name << " this is your new army.\n" << "You are attacking an enemy faction\n";

	while (!quit)
	{
		int warriors_sent, mages_sent, archers_sent;
		cout << "Your army consits off\n.";
		cout << "\n" << warriors << " warriors\n" << mages << " mages, and " << archers << " archers.\n";
		Sleep(2000);

		cout << "The enemies army consits off:\n";
		cout << e_warriors << " warriors" << e_mages << " mages, and " << e_archers << " archers.\n";
		Sleep(1500);


		do
		{
		int index = 1;
		if (archers > 0)
		{
			archers_menu = index;
			cout << "[" << index << "] Send archers.\n";
			index++;
		}
		else archers_menu = 0;

		if (mages > 0 )
		{
			mages_menu = index;
			cout << "[" << index << "] Send mages.\n";
			index++;
		}
		else mages_menu = 0;

		if (warriors > 0)
		{
			warriors_menu = index;
			cout << "[" << index << "] Send warriors.\n";
			index++;
		}
		else warriors_menu = 0;

		battle_menu = index;
		cout << "[" << index << "] Battle!\n";

		cin >> menuChoices;
		if(menuChoices == archers_menu)
		{
			do
			{
				cout << "How many archers would you like to send?\n";
				cin >> archers_sent;
			
			} while (!(archers_sent > -1 && archers_sent));
			
		while (menuChoices != battle_menu);

	}

				if(menuChoices == mages_menu)
		{
			do
			{
				cout << "How many mages would you like to send?\n";
				cin >> mages_sent;
			
			} while (!(mages_sent > -1 && mages_sent));



			
		while (menuChoices != battle_menu);

	}

				if(menuChoices == warriors_menu)
		{
			do
			{
				cout << "How many warriors would you like to send?\n";
				cin >> warriors_sent;
			
			} while (!(warriors_sent > -1 && warriors_sent));
			
				}
	}	while (menuChoices != battle_menu);
		// End of pre game loop

		cout << "Engading in combat...\n";
		                      
		int archers_dead, warriors_dead, mages_dead;
		int e_archers_dead, e_warriors_dead, e_mages_dead;

		archers_dead = 2 * e_mages;
		mages_dead = 3 *  e_warriors;
		warriors_dead = e_archers;

		e_archers_dead = 2 * mages;
		e_mages_dead = 2 * warriors;
		e_warriors_dead = archers;

		// Make sure we don't go negative

		archers = (archers_dead < archers) ? archers - archers_dead : 0;
		mages = (mages_dead < mages) ? mages - mages_dead : 0;
		warriors = (warriors_dead < warriors) ? warriors - warriors_dead : 0;

		e_archers = (e_archers_dead < e_archers) ? e_archers - e_archers_dead : 0;
		e_mages = (e_mages_dead < e_mages) ? e_mages - e_mages_dead : 0;
		e_warriors = (e_warriors_dead < e_warriors) ? e_warriors - e_warriors_dead : 0;

		Sleep(2000);

		cout << "The battle was tough.\n";

		cout << archers_dead << " of our archers died. " << mages_dead << "of our mages died, and " << warriors_dead << " of our warriors died.\n";

		// Check if we won

		if ((archers + mages + warriors) == 0)
		quit = lost = true;

		else if ((e_archers + e_mages + e_warriors) == 0)
		{
			quit = true;
			lost = false;
		}
}

	if(lost)
	{
		cout << "You lost, sorry\n";
	}
	else
	{
		cout << "You won, congradulations!\n";
		system("pause");
	}

}
lines 97 and 113 are just sending you into infinite loops.

this is a good example of why being consistent and accurate in indentation is important. Here's your code properly formatted:
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
#include <iostream>
#include <string>
#include <Windows.h>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	/*********************************** Variable Declartion and Background Work*******************************/

	srand( (unsigned int)time( NULL ) );

	string name;
	bool quit = false;
	bool lost;

	int menuChoices;

	// Our Unit

	int warriors = 25;
	int mages = 50;
	int archers = 100;

	// Enemy Units

	int e_warriors = rand() % (45) + 1;
	int e_mages = rand() % (70) + 1;
	int e_archers = rand() % (120) + 1;


	int warriors_menu, mages_menu, archers_menu;
	int battle_menu;



	/*********************************** End off Variable Declartion and Background Work*******************************/

	cout << "Welcome Sir what is your name?\n";
	cin >> name;
	cout << "Well, " << name << " this is your new army.\n" << "You are attacking an enemy faction\n";

	while( !quit )
	{
		int warriors_sent, mages_sent, archers_sent;
		cout << "Your army consits off\n.";
		cout << "\n" << warriors << " warriors\n" << mages << " mages, and " << archers << " archers.\n";
		Sleep( 2000 );

		cout << "The enemies army consits off:\n";
		cout << e_warriors << " warriors" << e_mages << " mages, and " << e_archers << " archers.\n";
		Sleep( 1500 );


		do
		{
			int index = 1;
			if( archers > 0 )
			{
				archers_menu = index;
				cout << "[" << index << "] Send archers.\n";
				index++;
			}
			else archers_menu = 0;

			if( mages > 0 )
			{
				mages_menu = index;
				cout << "[" << index << "] Send mages.\n";
				index++;
			}
			else mages_menu = 0;

			if( warriors > 0 )
			{
				warriors_menu = index;
				cout << "[" << index << "] Send warriors.\n";
				index++;
			}
			else warriors_menu = 0;

			battle_menu = index;
			cout << "[" << index << "] Battle!\n";

			cin >> menuChoices;
			if( menuChoices == archers_menu )
			{
				do
				{
					cout << "How many archers would you like to send?\n";
					cin >> archers_sent;

				} while( !(archers_sent > -1 && archers_sent) );

				while( menuChoices != battle_menu );

			}

			if( menuChoices == mages_menu )
			{
				do
				{
					cout << "How many mages would you like to send?\n";
					cin >> mages_sent;

				} while( !(mages_sent > -1 && mages_sent) );




				while( menuChoices != battle_menu );

			}

			if( menuChoices == warriors_menu )
			{
				do
				{
					cout << "How many warriors would you like to send?\n";
					cin >> warriors_sent;

				} while( !(warriors_sent > -1 && warriors_sent) );

			}
		} while( menuChoices != battle_menu );
		// End of pre game loop

		cout << "Engading in combat...\n";

		int archers_dead, warriors_dead, mages_dead;
		int e_archers_dead, e_warriors_dead, e_mages_dead;

		archers_dead = 2 * e_mages;
		mages_dead = 3 *  e_warriors;
		warriors_dead = e_archers;

		e_archers_dead = 2 * mages;
		e_mages_dead = 2 * warriors;
		e_warriors_dead = archers;

		// Make sure we don't go negative

		archers = (archers_dead < archers) ? archers - archers_dead : 0;
		mages = (mages_dead < mages) ? mages - mages_dead : 0;
		warriors = (warriors_dead < warriors) ? warriors - warriors_dead : 0;

		e_archers = (e_archers_dead < e_archers) ? e_archers - e_archers_dead : 0;
		e_mages = (e_mages_dead < e_mages) ? e_mages - e_mages_dead : 0;
		e_warriors = (e_warriors_dead < e_warriors) ? e_warriors - e_warriors_dead : 0;

		Sleep( 2000 );

		cout << "The battle was tough.\n";

		cout << archers_dead << " of our archers died. " << mages_dead << "of our mages died, and " << warriors_dead << " of our warriors died.\n";

		// Check if we won

		if( (archers + mages + warriors) == 0 )
			quit = lost = true;

		else if( (e_archers + e_mages + e_warriors) == 0 )
		{
			quit = true;
			lost = false;
		}
	}

	if( lost )
	{
		cout << "You lost, sorry\n";
	}
	else
	{
		cout << "You won, congradulations!\n";
		system( "pause" );
	}

}
Last edited on
Also, when a single function starts getting long and deeply nested, it can help to look for sections of code which can be broken out into separate functions. Human brains aren't very good at keeping track of multiple levels of nested logic. 3 or 4 levels and most people will start to get confused. Busting it down into separate functions allows you to abstract the code physically and conceptually. I'll look into this deeper and post some examples.
Thank you! It was a video tutorial and the guy was going quickly, I must've messed up or he did. Cheers :) Sadly despite setting the correct maths for each mage, archers and warrior it will still say for example 127 our of mages died. Any idea why?

EDIT: Sorry the tutorial coded like this I agree!
What I'm confused with is how 78 of my mages can die when I only set it to have 50...
1
2
3
4
5
6
7
		archers_dead = 2 * e_mages;
		mages_dead = 3 *  e_warriors;
		warriors_dead = e_archers;

		e_archers_dead = 2 * mages;
		e_mages_dead = 2 * warriors;
		e_warriors_dead = archers;

It must be this code, the maths is incorrect but I do not have the experiance to do a precise RNG to make it correct.
Any input would be nice, no hand holding just hints :)
Last edited on
Topic archived. No new replies allowed.