Combat Between 2 Characters using Classes

How exactly can I modify the healthpoints of my two characters (mHealthPoints and mHealthPoints1)? I get syntax error for putting mHealhPoints under the Goblin attack moves since mHealthPoints is not under the Goblin class.

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
  #include "Header.h"
Ninja::Ninja(int healthpoints)
{
	srand((unsigned int)time(0));
	mHealthPoints = healthpoints;
}
Goblin::Goblin(int healthpoints1)
{
	srand((unsigned int)time(0));
	mHealthPoints1 = healthpoints1;
}
void Goblin::GetGoblinStats()
{
	cout << "Goblin has " << mHealthPoints1 << " health points." << endl;
}
void Ninja::GetNinjaStats()
{
	cout << "Ninja has " << mHealthPoints << " health points." << endl;
}
void Ninja::swordAttack()
{
	cout << endl << "Ninja attacks with his sword!" << endl;
	int damage = 2 + rand() % (5 - 2 + 1);
	mHealthPoints1 -= damage;
	cout << "Ninja inflicts " << damage << " onto Goblin." << endl;
	cout << "Goblin now has " << mHealthPoints1 << " health points." << endl;
}
void Ninja::throwKnife()
{
	cout << endl << "Ninja throws his knife!" << endl;
	int damage = 3 + rand() % (4 - 3 + 1);
	mHealthPoints1 -= damage;
	cout << "Ninja in flicts " << damage << " onto Goblin." << endl;
	cout << "Goblin now has " << mHealthPoints1 << " health points." << endl;
}
void Ninja::tossFire()
{
	cout << endl << "Ninja tosses incendiary!" << endl;
	int damage = 3 + rand() % (6 - 3 + 1);
	mHealthPoints1 -= damage;
	cout << "Ninja inflicts " << damage << " onto Goblin." << endl;
	cout << "Goblin now has " << mHealthPoints1 << " health points." << endl;
}
void Goblin::daggerAttack()
{
	cout << endl << "Goblin attacks with his dagger!" << endl;
	int damage = 2 + rand() % (5 - 2 + 1);
	mHealthPoints -= damage;
	cout << "Goblin inflicts " << damage << " onto Goblin." << endl;
	cout << "Ninja now has " << mHealthPoints << " health points." << endl;
}
void Goblin::throwSpear()
{
	cout << endl << "Goblin throws his spear!" << endl;
	int damage = 2 + rand() % (5 - 2 + 1);
	mHealthPoints -= damage;
	cout << "Goblin inflicts " << damage << " onto Goblin." << endl;
	cout << "Ninja now has " << mHealthPoints << " health points." << endl;
}
void Ninja::NinjaMove()
{
	int randBehavior = 1 + rand() % 3;
	switch (randBehavior) {
	case 1:
		swordAttack();
		break;
	case 2:
		throwKnife();
		break;
	case 3:
		tossFire();
		break;
	}
}

void Goblin::GoblinMove()
{
	int randBehavior = 1 + rand() % 2;
	switch (randBehavior) {
	case 1:
		daggerAttack();
		break;
	case 2:
		throwSpear();
		break;
	}
}
your character class (the base class?) should have a value for current health, and you need a method to set this. it should probably set it to the max of maxhealth/inputvalue or the min of input and 0 or something to be safe (?). you probably also need a get for the value.

I would see something like this...

damage = ninja.attack()
tmp = goblin.getcurrenthealth();
goblin.setcurrenthealth(tmp-damage);
tmp = goblin.getcurrenthealth();
if(tmp == 0)
goblin.died();

Last edited on
Topic archived. No new replies allowed.