Help with combat system loop

One of the rules here is that you make your post as simple as possible, include only what's needed, and to make your code as readable as possible. Also do not just post hundred of lines of code. Include what is NEEDED. In your case only post the code thats giving you problems.

Edit your post and I'll come back.
Last edited on
Ok.. so i've got my program down to 2 errors.. First error does not give me a description. It links to the 2nd error which is giving me the following description.

 
Error	LNK2019	unresolved external symbol "int __cdecl showPlayerHP(int)" (?showPlayerHP@@YAHH@Z) referenced in function "void __cdecl combatSystem(void)" (?combatSystem@@YAXXZ)


I'm unclear which line it is pointing to.. Here are a couple areas of my code it could be referring to.

1
2
3
4
5
6
7
8
9
10
11
12
13
void combatSystem() {

	int playerHP = 59, enemyHP = 178;
		
	do
	{
		enemyHP = showEnemyHP(enemyHP);
		playerHP = showPlayerHP(playerHP);

		playerHitResult(enemyHP);
		enemyHitResult(playerHP);

	} while (enemyHP > 0 && playerHP > 0);


1
2
3
4
5
6
7
8
using namespace std;

void combatSystem();
int showEnemyHP(int enemyHP);
int showPlayerHP(int playerHP);
int playerHitResult(int enemyHP);
int enemyHitResult(int playerHP);
int playerRegDmg();

Did you write a definition for that function?
yes I believe I did write the definition.
Isn't the definition?

 
void combatSystem();

What you provided was the declaration. All that says is "This function has this signature", but it doesn't provide the implementation. You can do what gentleguy did, however you can also provide a definition below main if you want all the declarations at the top for quick reference. If you do that, the function signatures must match exactly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int showPlayerHP(int playerHP);
int showEnemyHP(int enemyHP);
...
int main()
{
  ...
  return 0;
}

int showPlayerHP(int playerHP)
{
  //Function logic here
}

int showEnemyHP(int enemyHP)
{
  //Function logic here
}
//etc 
I have two definitions and i think theses might be a problem. I might have make 2 more of these and put subtract the dmg from the playerHP and the enemyHP

1
2
3
4
5
6
7
8
9
10
11
12
13
int showEnemyHP(int enemyHP) {
	
        enemyHP = enemyHP;

	return enemyHP;
}

int showPlyerHP(int playerHP) {
	
        playerHP = playerHP;

	return playerHP;
}


Example code of what i'm thinking about doing below.

1
2
3
4
5
6
int showEnemyHP(int enemyHP) {
	
        enemyHP = enemyHP - playerRegDmg();

	return enemyHP;
}
Last edited on
Holy crap. Thank you for catching that. Hours and Hours of staring at code is taking a toll.

And that fixed it.. I have no more errors.. but for some reason. When player hits the monster and lets monster's starting hit points is 50 and the player hits for 15 dmg. It gives me the monsters hp is 35....
But when player hits the monster a second time.. the monster's hit points are refreshing back to 50 hit points..
Last edited on
HP is hard-coded ? Make it a user defineable.

Anyway, how is hitting handled ? Maybe the loop resets mobs HP and will only stop at HitDmg>MobHP ? Can you post the whole code ?
Last edited on
Yeah the loop is resetting the player's and monster's Hit Points.
Here is the code for my combat system loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


void combatSystem() {

	// Stone Golem - HP = 178, AC = 18, +10 to hit, 2x Slam attacks, dmg = 3d8 +6 bludgeoning damage.
	// Player - HP = 59, AC = 21, +8 to hit, 2x attacks, 1d12 + 5 slashing damage.

	int playerHP = 59, enemyHP = 178;
		
	do
	{
		enemyHP = showEnemyHP(enemyHP);
		playerHP = showPlayerHP(playerHP);

		playerHitResult(enemyHP);
		enemyHitResult(playerHP);

	} while (enemyHP > 0 && playerHP > 0);
}



Isn't this supposed to set the hit points at the beginning and then keeping using what hit points are in the do...while loop?
Last edited on
I think HP isn't being updated as it just sits there outside of the loop. Try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
	enemyHP = enemyHP - playerAttack();
	playerHP = playerHP - enemyAttack();

	showEnemyHP(enemyHP);
	showPlayerHP(playerHP);

	playerHitResult(enemyHP);
	enemyHitResult(playerHP);

}
while ((enemyHP > 0) | (playerHP > 0)); //changed to OR so death of a combatand stops the combat 


Not sure what playerHitResult() does, so i made up playerAttack() and enemyAttack() to make it clearer.
Last edited on
Here is function for player hitting the enemy doing dmg to enemy or missing.

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


int playerHitResult(int enemyHP) {				

	int hit_result, d20;
	int enemyAC = 18;
	int playerPlusHit = 8;

	d20 = rand() % 20 + 1;

	hit_result = playerPlusHit + d20;

	if (d20 == 20) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "CRITICAL HIT! Player rolls weapon dice twice!" << endl;
		cout << endl;
				
		enemyHP = enemyHP - playerCritDmg();
		cout << "Stone Golem's HP is now " << showEnemyHP(enemyHP) << endl;
		return enemyHP;
	}
	else if (d20 == 1) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Critical FAIL !!! Player MISSES!" << endl;
		cout << endl;
	}
	else if (hit_result >= enemyAC) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "Player Hits!" << endl;
		cout << endl;

		enemyHP = enemyHP - playerRegDmg();
		cout << "Stone Golem's HP is now " << showEnemyHP(enemyHP) << endl;
		return enemyHP;
	}
	else {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "Player Missed..." << endl;
		cout << endl;
	}
	return 0;
}

Umm here is my main function. And damage dealing functions.

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

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int combatSystem(int playerHP, int enemyHP);
int showEnemyHP(int enemyHP);
int showPlayerHP(int playerHP);
int playerHitResult(int enemyHP);
int enemyHitResult(int playerHP);
int playerRegDmg();
int playerCritDmg();
int enemyRegDmg();
int enemyCritDmg();
int d20();

int playerHP = 59, int enemyHP = 178;

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

	int choice;
	

	cout << endl;
	cout << "A Stone Golem claws up through the road in front of you." << endl;
	cout << "What do you do???" << endl;
	cout << endl;
	system("PAUSE");
	cout << endl;

	cout << "Do you Attack or Run away?" << endl;
	cout << "Attack = 1 Run = 2 " << endl;
	cin >> choice;

	if (choice == 1)
	{
		combatSystem(playerHP, enemyHP);
	}
	else if (choice == 2)
	{
		cout << "You chose to run away from the Stone Golem." << endl;
		cout << endl;
	}
	else
	{
		cout << "That is not a valid choice." << endl;
		cout << endl;
		return main();
	}

	system("PAUSE");
	return 0;
}


And this is my damage dealing function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int playerRegDmg() {		//dmg = d12 + 5 slashing damage

	int reg_dmg, d12;
	int playerStrMod = 5;

	d12 = rand() % 12 + 1;

	reg_dmg = playerStrMod + d12;
	cout << "Player's Greataxe damage is " << reg_dmg << endl;

	return reg_dmg;
}


Here's my whole code. Sorry in advance.

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


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int combatSystem(int playerHP, int enemyHP);
int showEnemyHP(int enemyHP);
int showPlayerHP(int playerHP);
int playerHitResult(int enemyHP);
int enemyHitResult(int playerHP);
int playerRegDmg();
int playerCritDmg();
int enemyRegDmg();
int enemyCritDmg();
int d20();

int playerHP = 59, int enemyHP = 178;

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

	int choice;
	

	cout << endl;
	cout << "A Stone Golem claws up through the road in front of you." << endl;
	cout << "What do you do???" << endl;
	cout << endl;
	system("PAUSE");
	cout << endl;

	cout << "Do you Attack or Run away?" << endl;
	cout << "Attack = 1 Run = 2 " << endl;
	cin >> choice;

	if (choice == 1)
	{
		combatSystem(playerHP, enemyHP);
	}
	else if (choice == 2)
	{
		cout << "You chose to run away from the Stone Golem." << endl;
		cout << endl;
	}
	else
	{
		cout << "That is not a valid choice." << endl;
		cout << endl;
		return main();
	}

	system("PAUSE");
	return 0;
}

int combatSystem(int playerHP, int enemyHP) {

	// Stone Golem - HP = 178, AC = 18, +10 to hit, 2x Slam attacks, dmg = 3d8 +6 bludgeoning damage.
	// Player - HP = 59, AC = 21, +8 to hit, 2x attacks, 1d12 + 5 slashing damage.

	//int playerHP = 59, enemyHP = 178;
		
	do
	{
		enemyHP = showEnemyHP(enemyHP);
		playerHP = showPlayerHP(playerHP);

		playerHitResult(enemyHP);
		enemyHitResult(playerHP);

	} while (enemyHP > 0 && playerHP > 0);
	return 0;
}


int showEnemyHP(int enemyHP) {
	enemyHP = enemyHP;

	return enemyHP;
}

int showPlayerHP(int playerHP) {
	playerHP = playerHP;

	return playerHP;
}


int playerHitResult(int enemyHP) {				

	int hit_result, d20;
	int enemyAC = 18;
	int playerPlusHit = 8;

	d20 = rand() % 20 + 1;

	hit_result = playerPlusHit + d20;

	if (d20 == 20) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "CRITICAL HIT! Player rolls weapon dice twice!" << endl;
		cout << endl;
				
		enemyHP = enemyHP - playerCritDmg();
		cout << "Stone Golem's HP is now " << showEnemyHP(enemyHP) << endl;
		return enemyHP;
	}
	else if (d20 == 1) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Critical FAIL !!! Player MISSES!" << endl;
		cout << endl;
	}
	else if (hit_result >= enemyAC) {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "Player Hits!" << endl;
		cout << endl;

		enemyHP = enemyHP - playerRegDmg();
		cout << "Stone Golem's HP is now " << showEnemyHP(enemyHP) << endl;
		return enemyHP;
	}
	else {
		cout << endl;
		cout << "Player rolled a " << d20 << endl;
		cout << "Player's score to hit is " << hit_result << " vs AC " << enemyAC << endl;
		cout << "Player Missed..." << endl;
		cout << endl;
	}
	return 0;
}

int enemyHitResult(int playerHP) {

	int hit_result, d20;
	int playerAC = 21;
	int enemyPlusHit = 10;
		
	d20 = rand() % 20 + 1;

	hit_result = enemyPlusHit + d20;

	if (d20 == 20) {
		cout << endl;
		cout << "Stone Golem rolls a " << d20 << endl;
		cout << "Stone Golem score to hit is " << hit_result << " vs AC " << playerAC << endl;
		cout << "CRITICAL HIT! Stone Golem rolls his weapon dice twice!" << endl;
		cout << endl;

		playerHP = playerHP - enemyCritDmg();
		cout << "Player's HP is now " << showPlayerHP(playerHP) << endl;
		return playerHP;
	}
	else if (d20 == 1) {
		cout << endl;
		cout << "Stone Golem rolls a " << d20 << endl;
		cout << "Stone Golem score to hit is " << hit_result << " vs AC " << playerAC << endl;
		cout << "Critical FAIL !!! Stone Golem MISSES!" << endl;
		cout << endl;
	}
	else if (hit_result >= playerAC) {
		cout << endl;
		cout << "Stone Golem rolls a " << d20 << endl;
		cout << "Stone Golem score to hit is " << hit_result << " vs AC " << playerAC << endl;
		cout << "Stone Golem Hits!" << endl;
		cout << endl;

		playerHP = playerHP - enemyRegDmg();
		cout << "Player's HP is now " << showPlayerHP(playerHP) << endl;
		return playerHP;
	}
	else {
		cout << endl;
		cout << "Stone Golem rolls a " << d20 << endl;
		cout << "Stone Golem score to hit is " << hit_result << " vs AC " << playerAC << endl;
		cout << "Stone Golem Missed..." << endl;
		cout << endl;
	}
	return 0;
}

int playerRegDmg() {		//dmg = d12 + 5 slashing damage

	int reg_dmg, d12;
	int playerStrMod = 5;

	d12 = rand() % 12 + 1;

	reg_dmg = playerStrMod + d12;
	cout << "Player's Greataxe damage is " << reg_dmg << endl;

	return reg_dmg;
}

int playerCritDmg() {

	int crit_dmg, d12a, d12b;
	int playerStrMod = 5;

	d12a = rand() % 12 + 1;
	d12b = rand() % 12 + 1;

	crit_dmg = d12a + d12b + playerStrMod;
	cout << "Player's Greataxe damage is " << crit_dmg << endl;

	return crit_dmg;
}

int enemyRegDmg() {		//dmg = 3d8 + 6 bludgeoning damage

	int reg_dmg, d8a, d8b, d8c;
	int enemyStrMod = 6;

	d8a = rand() % 8 + 1;
	d8b = rand() % 8 + 1;
	d8c = rand() % 8 + 1;

	reg_dmg = enemyStrMod + d8a + d8b + d8c;
	cout << "Stone Golem's Slam damage is " << reg_dmg << endl;

	return reg_dmg;
}

int enemyCritDmg() {

	int crit_dmg, d8a, d8b, d8c, d8d, d8e, d8f;
	int enemyStrMod = 6;

	d8a = rand() % 8 + 1;
	d8b = rand() % 8 + 1;
	d8c = rand() % 8 + 1;
	d8d = rand() % 8 + 1;
	d8e = rand() % 8 + 1;
	d8f = rand() % 8 + 1;

	crit_dmg = d8a + d8b + d8c + d8d + d8e + d8f + enemyStrMod;
	cout << "Stone Golem's Critical Slam damage is " << crit_dmg << endl;

	return crit_dmg;
}

int d20() {
	int d20_result;

	d20_result = rand() % 20 + 1;

	return d20_result;
}

I see you passed the int playerHP and enemyHP in by reference?
Thank you so much for your help.
Also you added #include <windows.h>

I just got home and tried what you posted gentleguy.
Unfortunately it didn't work for me.

I wonder what i'm still missing.. its giving me 2 errors..

The first error is before my main function.

Error C2062 type 'int' unexpected

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

using namespace std;

int combatSystem(int playerHP, int enemyHP);
int showEnemyHP(int enemyHP);
int showPlayerHP(int playerHP);
int playerHitResult(int& enemyHP);
int enemyHitResult(int& playerHP);
int playerRegDmg();
int playerCritDmg();
int enemyRegDmg();
int enemyCritDmg();
int d20();

int playerHP = 59, int enemyHP = 178;      <


Then the second error is in the main function.

Error C2065 'enemyHP': undeclared identifier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int main() {

	srand((unsigned)time(NULL));

	int choice;

	cout << endl;
	cout << "A Stone Golem claws up through the road in front of you." << endl;
	cout << "What do you do???" << endl;
	cout << endl;
	system("PAUSE");
	cout << endl;

	cout << "Do you Attack or Run away?" << endl;
	cout << "Attack = 1 Run = 2 " << endl;
	cin >> choice;

	if (choice == 1)
	{
		combatSystem(playerHP, enemyHP);  <


Last edited on
Still getting two errors...

With those couple changes gentlguy the program is working for you?
yeah i copied and pasted your code...

BUT !!! I got it !!! IT WORKS !!! HAHAHAHHAA !!!

I found that below was wrong..

 
int playerHP = 59, int enemyHP = 178;  


it should be

 
int playerHP = 59, enemyHP = 178;  


and i added #include <windows.h>.. but i don't need it
and i passed in the playerHP and enemyHP by reference
Thank you again for the help! :) :) :)

Now to rewrite the program using classes. :)
Topic archived. No new replies allowed.