Hi everyone,
So I'm currently taking a class for programming and can't figure out how to solve one of the assignments. Note that all five downloads are together in the code section - I've separated them with their titles above. Also, I know the code I have is incomplete but that's cause I can't figure this out. The assignment is as follows:
In this problem we are going to use the Character class from Homework 6 to develop a more interesting
game. Download the starter code main.cpp, Character.h and Character.cpp, Combat.cpp and Combat.h
contained in the zip file Combat.zip from CCLE. We have provided main.cpp to give you an idea of how we
intend to use the Combat class. You may not use global variables.
Complete the implementations for the Combat class, which simulates a text combat game with multiple
monsters, in the file Combat.cpp. You should only modify Combat.cpp and submit only Combat.cpp to
CCLE.
The private data members:
• int NumberOfMonsters represents the number of monsters in the game;
• vector<Character> monsterList stores a list of monsters;
• Character hero represents the hero you play;
• int tokens represents the number of tokens the hero has, which would allow the hero to attack all
the monsters at the same time.
A default constructor has been included in Combat.cpp.
The other constructor Combat(int newNumberOfMonsters) creates a hero named “Hero” and a list of
newNumberOfMonsters monsters. The monsters are named “Monster0”, “Monster1”, “Monster2”, etc. The
other data fields are initialized as
- For the hero: health = 30, damage = 3, arrows = 5;
- For all the monsters: health = 5, damage = 2, arrows = 0;
- tokens = 1.
Hint: the function to string might be helpful. ¡
http://www.cplusplus.com/reference/string/to string/.
The public methods:
• void attackAll() lets the hero do a regular attack to ALL the monsters in the monsterlist. Every
attackAll costs one token. If the hero has no tokens left, print something like
Hero is out of tokens!
• start() defines the game process and has been provided in Combat.cpp;
• void checkAliveMonster() checks if every monster in the monsterList is still alive. For any monster
with negative or zero health, remove that monster from the monsterList, add 5 points to the hero’s
health, and add 1 tokens as a bonus. Print to the console something like
You killed a monster!
Hero gets 5 health bonus points!
Hero gets 1 bonus tokens!
• void printMonsterList() const prints the name and health of every existing monster in the monsterList.
A sample print may look like
Monster0 health: 3
Monster1 health: 2
Monster2 health: 4
main.cpp
1 2 3 4 5 6 7 8 9 10
|
#include "Combat.h"
using namespace std;
int main()
{
Combat game(3);
game.start();
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
|
//Character.h
#ifndef Character_h
#define Character_h
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Character
{
public:
Character();
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
|
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 47 48 49 50 51 52 53 54 55 56 57 58
|
#include "Character.h"
Character::Character()
{
name = " ";
health = 0;
damage = 0;
arrows = 0;
}
Character::Character(string newName, int newHealth, int newDamage, int newArrows)
{
name = newName;
health = newHealth;
damage = newDamage;
arrows = newArrows;
}
void Character::set_health(int newHealth)
{
health = newHealth;
}
void Character::attack(Character& target)
{
target.health -= damage;
cout << name << " attacks " << target.name << " doing " << damage << " damage!" << endl;
cout << target.name << " health: " << target.health << endl;
}
void Character::rangedAttack(Character& target)
{
if (arrows == 0)
cout << name << " is out of arrows!" << endl;
else
{
int rangedDamage = rand() % 5 + 1;
target.health -= rangedDamage;
arrows--;
cout << name << " shoots " << target.name << " doing " << rangedDamage << " damage!" << endl;
cout << target.name << " health: " << target.health << endl;
}
}
void Character::display() const
{
cout << name << " health: " << health << " arrows: " << arrows << endl;
}
string Character::get_name() const
{
return name;
}
int Character::get_health() const
{
return health;
}
|
Combat.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
|
//Combat.h
#ifndef COMBAT_h
#define COMBAT_h
#include <iostream>
#include <vector>
#include "Character.h"
using namespace std;
class Combat
{
public:
Combat();
Combat(int newNumberOfMonsters);
void attackAll();
void start();
void checkAliveMonster();
void printMonsterList() const;
private:
int NumberOfMonsters;
vector<Character> monsterList;
Character hero;
int tokens;
};
#endif
|
Combat.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 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
|
//Combat.cpp
#include "Combat.h"
Combat::Combat()
:hero("Hero", 30, 3, 5), NumberOfMonsters(1), tokens(2)
{
Character M("Monster", 5, 2, 0);
monsterList.push_back(M);
}
void Combat::start()
{
int choice, iMonster;
srand(time(0));
while (NumberOfMonsters>0 && hero.get_health()>0)
{
for (int i = 0; i<NumberOfMonsters; i++)
monsterList[i].attack(hero);
cout << "----------------------\n";
if (hero.get_health() <= 0)
break;
hero.display();
cout << hero.get_name() << " has " << tokens << " tokens left.\n";
cout << "----------------------\n";
printMonsterList();
cout << "What do you do? 1 attack, 2 fire arrow, 3 attack all, Q exit: ";
cin >> choice;
if (choice == 1 || choice == 2)
{
cout << "Which monster do you want to attack? (0-" << NumberOfMonsters - 1 << "): " << endl;
cin >> iMonster;
if (cin.fail() || iMonster >= NumberOfMonsters)
{
cout << "Input Error!\n";
break;
}
}
cout << "----------------------\n";
switch (choice)
{
case 1:
hero.attack(monsterList[iMonster]);
break;
case 2:
hero.rangedAttack(monsterList[iMonster]);
break;
case 3:
attackAll();
break;
default:
cout << "Input error!\n";
break;
}
checkAliveMonster();
cout << "----------------------\n";
}
if (NumberOfMonsters == 0)
cout << "Congratulations! You have killed all the monster!" << endl;
if (hero.get_health() <= 0)
cout << "You have died! GAME OVER! " << endl;
cout << "Thanks for playing!" << endl;
}
// Put your code below:
//IMPLEMENTATIONS
Combat::Combat(int newNumberOfMonsters) {
NumberOfMonsters = newNumberOfMonsters;
}
void Combat::attackAll() {
if (tokens != 0) {
vector<int> monsterList{ 0, 1, 2 };
for (int i = 0; i < NumberOfMonsters - 1; i++) {
cout << hero.get_name() << " attacks " << monsterList[i] << endl;
}
--tokens;
}
else if (tokens == 0) {
cout << "Hero is out of tokens!";
}
}
void Combat::checkAliveMonster() {
for (int i = 0; i < NumberOfMonsters - 1; i++) {
if (monsterList[i].get_health <= 0) {
--NumberOfMonsters;
cout << "You killed a monster!" << endl;
hero.set_health((hero.get_health() + 5));
cout << "Hero gets 5 health bonus points!" << endl;
++tokens;
cout << "Hero gets 1 bonus tokens!" << endl;
}
}
}
void Combat::printMonsterList() const {
for (int i = 0; i < NumberOfMonsters - 1; i++) {
cout << "Monster" << monsterList[i].get_name() << " health: " << monsterList[i].get_health();
}
}
|
Any assistance would be much appreciated.