How do I format the accessors and mutators in this Hero and Monster problem?

Pls help me. I'm struggling with how to implement the change in health into the respective Monster and Hero. I'm only supposed to modify character.cpp. get_name is supposed to get the character's name while get_health and set_health are supposed to get the health of the character, but how do I implement the difference between the Hero and Monster and the changes to their health in these variables?
main.cpp:
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
  #include "Character.h"
using namespace std;

int main() {

	int monster_health = 10;
	int monster_damage = 3;

	srand(time(0));

	//This is the character you play
	Character C("Hero");

	// This is the monster you try to defeat, the monster has 0 arrow
	Character M("Monster", monster_health, monster_damage, 0);

	while (M.get_health() > 0 && C.get_health() > 0)
	{
		M.attack(C);
		cout << "----------------------\n";
		if (C.get_health() <= 0)
			break;
		C.display();
		cout << "What do you do? 1 attack, 2 fire arrow, Q exit: ";
		int choice;
		cin >> choice;
		if (cin.fail())
			break;
		switch (choice)
		{
		case 1:
			C.attack(M);
			break;

		case 2:
			C.rangedAttack(M);
			break;
		}

	}
	if (M.get_health() <= 0)
		cout << "Congratulations! You killed the monster!" << endl;
	if (C.get_health() <= 0)
		cout << "You have died! GAME OVER! " << endl;

	cout << "Thanks for playing!" << endl;

	return 0;
}


Character.h:
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
  //Character.h
#ifndef Character_h
#define Character_h

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

using namespace std;

class Character
{
public:
	Character(string newName);
	Character(string newName, int newHealth, int newDamage, int newArrows);

	void attack(Character& target);
	void rangedAttack(Character& target);
	void set_health(int newHealth);

	int get_health() const;
	string get_name() const;
	void display() const;

private:
	string name;
	int health;
	int damage;
	int arrows;
};
#endif#pragma once 


This is what I have so far
Character.cpp
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
  #include "Character.h"

Character::Character(string newName)
{
	name = newName;
	health = 20;
	damage = 2;
	arrows = 2;
}

/* Put your code below */
Character::Character(string newName, int newHealth, int newDamage, int newArrows) {
	name = newName; 
	health = newHealth; 
	damage = newDamage; 
	arrows = 0; 
}
int Character::get_health() const {

}
string Character::get_name() const {

}

void Character::display() const {
	cout << "Hero Health: " << health << " arrows: " << arrows; 
	cout << endl; 
}

void Character::attack(Character& target) {
	
}

void Character::rangedAttack(Character& target) {
	if (arrows == 0) {
		cout << "Hero is out of arrows!" << endl; 

	}
	damage = rand() % 5 + 1;  
	cout << "Hero shoots Monster doing " << damage << " damage" << endl; 
	cout << "Monster health: " << endl; //unfinished 

}
void Character::set_health(int newHealth) {

}
Last edited on
Wouldn't set_health just be doing health = newHealth;? And get_health should just return the health.

If the character is attacking the other character, you'd want to reduce the other character's health by some amount
e.g.
1
2
3
void Character::attack(Character& target) {
    target.set_health(target.get_health() - damage);	
}

Something like that.
Last edited on
Hi thanks for the reply. This is my character.cpp now. A lot of things work, but a few are not. If I use the ranged attack first, the regular attack for the hero also becomes the randomized ranged attack (even though the damage for the hero is set at 2). How do I fix this without declaring a new variable within rangedAttack?
My second issue is that my debugger just closes when either the monster or the hero health goes below 0. The -health isn't printed and final cout statements are not printed by the main.

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 "Character.h"

Character::Character(string newName)
{
	name = newName;
	health = 20;
	damage = 2;
	arrows = 2;
}

/* Put your code below */
Character::Character(string newName, int newHealth, int newDamage, int newArrows) {
	name = newName; 
	health = newHealth; 
	damage = newDamage; 
	arrows = 0; 
}
int Character::get_health() const {
	
	return health; 
	
}
string Character::get_name() const {
	return name; 
}

void Character::display() const {
	cout << name << " Health: " << health << " arrows: " << arrows; 
	cout << endl; 
}

void Character::attack(Character& target) {
	cout << name << " attacks " << target.get_name() << " doing " << damage << " damage!" << endl;
	target.set_health(target.get_health() - damage); 
	cout << target.get_name() << " health: " << target.get_health() << endl;
	
}

void Character::rangedAttack(Character& target) {
	if (arrows == 0) {
		cout << "Hero is out of arrows!" << endl; 
		arrows = 0; 

	}
	else {
		damage = rand() % 5 + 1;
		cout << "Hero shoots Monster doing " << damage << " damage" << endl;
		target.set_health(target.get_health() - damage);
		cout << "Monster health: " << target.get_health() << endl;
		arrows--;
	}
	
}
void Character::set_health(int newHealth) {
	health = newHealth; 
}

How do I fix this without declaring a new variable within rangedAttack?

What's wrong with declaring a new variable?

Presumable your damage data member is intended for melee damage, so you shouldn't be using it at all here. Just declare a local variable for the ranged damage here.

If you're really desperate to avoid declaring a local variable, you could use the expression for calculating it directly in the call to set_health(), but I don't see much point. Trying to cram as much as possible into a single line just makes your code harder to read.

My second issue is that my debugger just closes when either the monster or the hero health goes below 0. The -health isn't printed and final cout statements are not printed by the main.

Presumably this is just your program ending naturally, and your debugger closing the shell window that it's running the program in?
Topic archived. No new replies allowed.