In using C++ Inheritance, you need to
Create a game program using C++. The game specifications are the following:
1. The user has 3 types of player against a monster.
Master:
Life = 10
Super Power = Double attack (1 time)
Damage = Life to 0 if double attack was used
Healer:
Life = 10
Super Power = Restore life of warrior (1 time)
Damage = attack / 2 if healing power was used
Warrior:
Life = 10
No super power
2. Every attack of monster cause equal damage to player’s life. Deduct monster’s
attack to player’s life
3. Effect of player attack to monster depends on how player attack
4. Player’s super power (master and healer) can only be used once throughout the
game.
5. Player with 0 life is not allowed to attack
6. Monster has an initial life of 30
7. The game will continue until monster is defeated or all players are dead.
Required:
1. Use inheritance to create the program.
2. Display current life status in the given format (see testcase)
3. Ask user to choose a player ‘m’ for master, ‘h’ for healer and ‘w’ for warrior.
If user selects master or healer, ask user if they want use the super power if yes,
apply proper attack value to monster else perform a simple attack.
Note: They can only use each super power once.
Current life = previous life – attack
4. Generate random number from 1 to 6 as the attack value of both players and
monster.
Note: remove srand() on monster attack
5. At the end of a single round, display current status after the attack (see test
case)
6. Player with zero life is not allowed to attack thus inform user to choose another
player. Note: if user selects dead player, the round will still be the same.
7. Use looping statement to generate rounds of the game. The program will only
terminate if the total life of the team (life of master + life of Healer + life of warrior)
is equal or less than zero thus, monster won the game or the life of monster is
equal or less than zero.
8. Do not display negative life. If current life is less than 0 after the attack then
display zero
#include <iostream>
#include <cstdlib>
using namespace std;
//Base class for all players
class GameCharacter
{
protected:
int initialLife;
bool hasSuperPower = false;
int damage;
int attackVal;
//Attack monster
//Initialize initial life for character
public:
void promptSuperPowerOption()
{
char superPowerOption;
cout<<"Do you want to user superpower? y-yes, n-no: ";
cin>>superPowerOption;
if(superPowerOption == 'y')
{
hasSuperPower = true;
}
else if(superPowerOption == 'n')
{
hasSuperPower = false;
}
else
{
cout<<"Please enter a valid option"<<endl;
}
}
void setSuperPowerOption(bool spPowerOption)
{
hasSuperPower = spPowerOption;
}
bool getSuperPowerOption()
{
return hasSuperPower;
}
int getAttackPowerUsed()
{
return attackVal;
}
void setInitialLife(int lifeToInitialize)
{
initialLife = lifeToInitialize;
}
int getInitialLife()
{
return initialLife;
}
int playerAttack()
{
srand((unsigned) time(0));
int attackVal = (rand() % 6) + 1;
return attackVal;
}
int monsterAttack()
{
attackVal = rand() % 6 + 1;
return attackVal;
}
//Deduct player life
void deductLife(int lifeToDeduct)
{
initialLife -= lifeToDeduct;
}
int getCurrentLife()
{
return initialLife;
}
void setHasSuperPower(bool characterSuperPowerStatus)
{
hasSuperPower = characterSuperPowerStatus;
}
};
class Master: public GameCharacter
{
public:
int superPower;
};
//Healer player class
class Healer: public GameCharacter
{
public:
int superPower;
};
//Warrior player class
class Warrior: public GameCharacter
{
};
class Monster: public GameCharacter
{
};
int main()
{
char player;
char superPowerOption;
bool useSuperPower;
int round = 1;
//Create the game objects and init their values
Monster monster;
monster.setInitialLife(30);
//Master player
Master master;
master.setInitialLife(10);
master.superPower = 1;
//Healer player
Healer healer;
healer.setInitialLife(10);
healer.superPower = 1;
//Warrior player
Warrior warrior;
warrior.setInitialLife(10);
//Total life
int totalLife = 30;
//If monster is not dead
while(monster.getInitialLife() > 0 | totalLife > 0 )
{
cout<<"Round "<<round<<endl;
//Prompt user to choose a player
cout<<"Player Selection Menu"<<endl;
cout<<"m - master \nh- healer \nw-warrior "<<endl;
cout<<"Enter a letter to select the player:";
cin>>player;
switch(player)
{
case 'm':
cout<<"You have chosen master. ";
//prompt user for superpower option
master.promptSuperPowerOption();
if(master.getCurrentLife() > 0)
{
for( int i = 0; i < master.getCurrentLife(); i++)
{
//Attack monster
cout<<"Master attacking..."<<endl;
//Deduct master life based on master attack
master.deductLife(master.playerAttack());
cout<<"Attack power used by master: "<<master.playerAttack()<<endl;
//Deduct total life
totalLife -= master.playerAttack();
cout<<"Master current life: "<<master.getCurrentLife()<<endl;
cout<<"Monster attacking..."<<endl;
//Deduct monster life based on master attack
monster.deductLife(master.playerAttack());
cout<<"Attack power used by monster: "<<monster.monsterAttack()<<endl;
cout<<"Monster current life: "<<monster.getCurrentLife()<<endl;
while(master.getSuperPowerOption() == true)
{
cout<<"Restoring Life"<<endl;
master.setInitialLife(10);
//Add to total life
totalLife += 10;
master.setSuperPowerOption(false);
}
}
}
else
{
cout<<"Master is dead. Select another player."<<endl;
}
break;
case 'h':
cout<<"You have chosen healer. ";
//prompt user for superpower option
healer.promptSuperPowerOption();
//If uses superpower
if(healer.getCurrentLife() > 0)
{
for( int i = 0; i < healer.getCurrentLife(); i++)
{
//Attack monster
cout<<"Healer attacking..."<<endl;
//Deduct healer life based on healer attack
healer.deductLife(healer.playerAttack());
cout<<"Attack power used by healer: "<<healer.playerAttack()<<endl;
//Deduct total life
totalLife -= healer.playerAttack();
cout<<"Healer current life: "<<healer.getCurrentLife()<<endl;
cout<<"Monster attacking..."<<endl;
//Deduct monster life based on healer attack
monster.deductLife(healer.playerAttack());
cout<<"Attack power used by monster: "<<monster.monsterAttack()<<endl;
cout<<"Monster current life: "<<monster.getCurrentLife()<<endl;
while(healer.getSuperPowerOption() == true)
{
cout<<"Restoring Life"<<endl;
healer.setInitialLife(10);
healer.setSuperPowerOption(false);
}
}
}
else
{
cout<<"Healer is dead. Select another player."<<endl;
}
break;
case 'w':
cout<<"You have chosen warrior. ";
//prompt user for superpower option
if(warrior.getCurrentLife() > 0)
{
for( int i = 0; i < warrior.getCurrentLife(); i++)
{
//Attack monster
cout<<"Healer attacking..."<<endl;
//Deduct warrior life based on warrior attack
warrior.deductLife(warrior.playerAttack());
cout<<"Attack power used by healer: "<<warrior.playerAttack()<<endl;
totalLife -= warrior.playerAttack();
cout<<"Master current life: "<<warrior.getCurrentLife()<<endl;
cout<<"Monster attacking..."<<endl;
//Deduct monster life based on warrior attack
monster.deductLife(healer.playerAttack());
cout<<"Attack power used by monster: "<<monster.monsterAttack()<<endl;
cout<<"Monster current life: " <<monster.getCurrentLife()<<endl;
}
}
else
{
cout<<"Warrior is dead. Select another player."<<endl;
}
break;
default:
cout<<"Invalid choice please try again";
}
round += 1;
}
//Output reason for game over
if(monster.getInitialLife() <= 0)
{
cout<<"GAME OVER"<<endl;
cout<<"Monster Eliminated"<<endl;
}
else
{
cout<<"GAME OVER"<<endl;
cout<<"All players eliminated"<<endl;
}
return 0;
}
Comments
Leave a comment