Can my code be reduced? (I'm sure it can be)

I am trying to make a game where the game randomly chooses orcs that you fight. I want the game to have a rock/paper/scissors kind of gameplay. The choices will eventually be attack/strong attack/dodge/and block. Each of the choices would do different things and the enemy would randomly choose an attack. Since I am new to programming my code is probably really clunky and inefficient. I am having trouble making the orc attack after the player attacks. So far the only option is to attack, and dodge terminates the program.

can someone help me reduce my code or give me general advice?
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <string>
#include <conio.h>
#include <time.h>
using namespace std;

struct orc
	{
		char name[20];
		int mindamage;
		int maxdamage;
		int armor;
		int hp;
		int agility; 
	};

struct item
{
	char name[30];
	std::string description;
	std::string call;
};
struct wep
{
	char name[30];
	int mindmg;
	int maxdmg;
	std::string description;
	std::string call;
};
int main()
{
	//ORC STATS

	orc weak = 
	{
		"Weak Orc",
		1,		//min damage
		3,		//max damage
		1,		//armor
		8,		//hp
		6,		//agility
	};

	orc regular = 
	{
		"Orc",
		2,
		5,
		2,
		12,
		7,
	};

	orc strong =
	{
		"Strong Orc",
		3,
		7,
		3,
		15,
		8,
	};
	
	orc enemy;

	//ITEM STATS

	item whpot =
	{
		"(whp) Weak Health Potion",
		"A Weak Health Potion heals %10 of your hit points",
		"whp",
	};

	item hpot =
	{
		"(hp) Health Potion",
		"A Health Potion heals %30 of your hit points",
		"hp",
	};

	item shpot =
	{
		"(shp) Strong Health Potion",
		"A Strong Health Potion completely heals your hit points",
		"shp",
	};

	item empty =
	{
		"(empty)",
		"A slot may hold an item",
		"",
	};
	item slot1, slot2, slot3, slot4;

	//WEAPON STATS

	wep wempty =
	{
		"(bh) bare hands",
		1,
		2,
		"Desperate times call for desperate methods...",
		"bh",
	};

	wep rdagger =
	{
		"(rd) rusty dagger",
		1,
		4,
		"the dull blade is covered in rust",
		"rd",
	};


	wep rhand;



	//PROGRAM RANDOMLY SELECTS ORC TYPE

	cout << "\n\nFate rolls a dice for you, revealing your enemy...\n\n" << endl;
	getch();
	srand (time(NULL));
	int iRandom = rand() % 10 + 1;
	if (iRandom <= 5) 
     { 
        enemy = weak; 
		cout << "A weak orc limps into the arena, still bleeding from an earlier match.";
     } 
   else if (iRandom <= 9)
     { 
        enemy = regular;
		cout << "A common orc enters the arena, his blood red eyes locked directly on yours.";
     } 
   else
   {
	   enemy = strong;
	   cout << "A strong orc dashes into the arena directly at you!";
   }
	
   //STATS
   int hp = 20;
   int maxhp = 20;
   int agility = 5;
   int amindmg;				//active min and max damage, these are assigned weapon stats.
   int amaxdmg;
   int armor = 3;				//assigned armor value from worn armor. given a value for now.

   

    //INVENTORY
    rhand = wempty;
	slot1 = hpot; 
	slot2 = whpot;
	slot3 = whpot;
	slot4 = whpot;


	string icommand;
 

	do {
		cout << "\n\n\n**************************\nYour Hitpoints:  (" << hp << ")" << endl;
		cout << ""<<enemy.name << "\'s Hitpoints: (" << enemy.hp << ")\n";
		cout << "**************************\n\n";
		cout << "(a)ttack\n"
			"(d)odge\n";
		string orcroll;
		int iRandom = rand() % 10 + 1;		//roll to see what orc does
		if (iRandom <= 5) orcroll = "a";
		else orcroll = "d";
		int ydmg;
		int edmg;
		amaxdmg = rhand.maxdmg;
		amindmg = rhand.mindmg;
		cin >> icommand;
		//PLAYER'S ATTACK

		if ((icommand == "a")&(orcroll == "a"))
		{
			
			int iRandom = rand() % 20 + 1;				//player's roll to hit
			if (iRandom < 2*agility)
			{
				int iRandom = rand() % 20 + 1;				//rolls for orc dodge
				if (iRandom < 0.5*enemy.agility)
				{
					cout << "\n\n\nThe " << enemy.name << " dodges your attack!";
				}else if (iRandom == 0.5*enemy.agility)
				{
					cout << "\n\n\nThe " << enemy.name << " narrowly dodges your attack!";
				}else
				{
					int ydmg = rand() % amaxdmg + (amindmg);
					enemy.hp -= ydmg;
					cout << "\n\n\nYou hit the " << enemy.name << " for " << ydmg << " damage";
				}
			}else if (iRandom = 2*agility)
			{
				int iRandom = rand() % 20 + 1;				//rolls for orc dodge
				if (iRandom < 0.5*enemy.agility)
				{
					cout << "\n\n\nThe " << enemy.name << " dodges your attack!";
				}else if (iRandom == 0.5*enemy.agility)
				{
					cout << "\n\n\nThe " << enemy.name << " narrowly dodges your attack!";
				}else
				{
					int ydmg = 1.5*(rand() % amaxdmg + (amindmg));
					enemy.hp -= ydmg;
					cout << "\n\n\nYou critically hit the " << enemy.name << " for " << ydmg << " damage";
				}
			}else 
				cout << "\n\n\nYou missed\n";
			
		}else if ((icommand=="a")&(orcroll=="d"))
		{
			cout << "\n\nThe " << enemy.name << " tries dodging your attack!\n";
			getch();
			int iRandom = rand() % 20 + 1;				//orc rolls for dodge
			if (iRandom <= 1.5*agility)
			{
				cout << "\n\nThe " << enemy.name << " has dodged your attack\n";
				getch();
				cout << "\n\nYou are stunned\n";
				getch();
				int iRandom = rand() % 20 + 1;				//Orc rolls to hit
				if (iRandom < 2*enemy.agility)
				{
					int edmg = (rand() % enemy.maxdamage + (enemy.mindamage));
					hp -=edmg;
					cout << "\n\n\nThe " << enemy.name << " hits you for " << edmg << " damage!";
				}else if (iRandom == 2*enemy.agility)
				{
					int edmg = 1.5*(rand() % enemy.maxdamage + (enemy.mindamage));
					hp -=edmg;
					cout << "\n\n\nThe " << enemy.name << " critically hits you for " << edmg << " damage!";
				}else
				{
					cout << "\n\nThe " << enemy.name << " misses you.";
				}
			}else
			{
				int iRandom = rand() % 20 + 1;				//player's roll to hit
				if (iRandom < 1.5*agility)
				{
					ydmg = rand() % amaxdmg + (amindmg);
					enemy.hp -= ydmg;
					cout << "\n\nYou hit the " << enemy.name << " for " << ydmg << " damage";
					}else if (iRandom == 1.5*agility)
					{
						ydmg = 1.5*(rand() % amaxdmg + (amindmg));
						enemy.hp -= ydmg;
						cout << "\n\nYou critically hit the " << enemy.name << " for " << ydmg << " damage";
					}else
					{
						cout << "\n\nYou missed";
					}
			}
 }else cout << "\n\nPlease enter a valid command.";	
			 


		// END INVENTORY


	   }while 
		   (icommand != "d"); //closes do while for attacking, stops loop when you hit d.
 
	return 0;
}

Are you sure this code works for you? Since I see a multitude of re-initialisations of iRandom. It could just be me, but this shouldn't be running.
Yeah it works for me but thanks for pointing that out, I don't know why I kept doing that.
Not to bring you off topic but the game dynamic of rock, paper scissor wont work with four options. It works with an odd of options in which each choice beats half of the remaining options and is beaten by the other half, so you need one more option. Can I suggest parry, throw sand\dirt or kick-leg?
http://en.wikipedia.org/wiki/Rock-paper-scissors

On a side note games like this are excellent ways to practice C++. If you change your struct orc into a class monster you can practice all kinds of things like methods, inheritence and polymorphism later on. You've already seen what polymorphism is for judging by lines 35 - 63, so it might be a good idea to look that up next, and yes this concept will help reduce your code once you start trying to keep track of multipule monsters.
I guess the game isn't pure rock paper scissors because if both players attack then they both may hit each other, and if one player dodges and one blocks, nothing happens, but thanks for the suggestions.

I guess my biggest problem is that I have a very limited number of techniques at my disposal and I am at a point where I can't make the program do what I want it to do anymore. I'm trying to make it so when the player chooses attack and the orc chooses attack, the player attacks, then there is a pause, then the orc has an attack back on the player which is shown.

Lines 186-218 govern what happens when the player and the orc attack at the same time, and so far it only allows the player to do an attack while the orc's choice to attack is pretty much ignored. The orc's attack would be the same code as 186-218 except the variables would be reversed so the players hp would go down and the orc's damage would be used etc...

I've tried adding the orc's version of lines 186-218 right after the player's attack (line 219) with a pause in between, but the program comes up with errors. Is it impossible to put two different if else statements inside of another block like that?

I've also tried putting the orc's version of lines 186-218 right after the result of each of the players attacks (lines 192, 195, 200 etc...) and that causes the code to look extremely confusing.

Does anyone know how I can do this? Ideally I would like to make it so lines 186-218 become functions that return a result so I can just call the functions in the main block and have it look more simple, but I haven't been able to do the successfully either.
I acutually just go it to work by using goto orcattack; after each of the player's attack possibilities, and having the block of code for the orc's attack on line 219 after a break. I read in another post that goto isn't the best thing to be using so I suppose I'll keep trying to learn stuff so I don't keep using it.
rand() % amaxdmg + (amindmg)
will give a result between amindmg and amindmg+amaxdmg...
If you made each of the actions into seperate functions that return the result then you should be able to get rid of goto.
Last edited on
JoR: how do I fix that? I used http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ as a reference to using rand().

Computergeek01: ok thanks, I'll look into that more
thanks JoR I understand rand() a little bit more now.
Topic archived. No new replies allowed.