Answer to Question #157179 in C++ for Mayma

Question #157179

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.


OUTPUT SHOULD BE THE SAME WITH THIS:

======== ROUND 1 ==========

Choose a player (m,h,w): w

**Warrior Attack = 3

** Monster Attack = 1

Life Status: M = 10 H = 10 W= 9; Monster = 27


========= ROUND 2 ======


UNTIL ROUND 8 , YOU WILL WON THE GAME


1
Expert's answer
2021-01-23T10:19:23-0500
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main () {
    srand(time(NULL));
    int lifeM = 10, lifeH = 10, lifeW = 10;
    int lifeMonster = 30;
    int i = 1, attack, attackM;
    char player;
    string super_power;
    cout << "At the beginning life status of plyers and monster: \n";
    cout << "Life status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
    while ((lifeM > 0 && lifeH > 0 && lifeW) || lifeMonster > 0) {
        attackM = 1 +  rand() % 6;
        cout << "\n\n=========Round " << i++ << " ======";
        cout << "\noose a player(m, h, w): ";
        cin >> player;
        if (player == 'm' && lifeM > 0) {
            cout << "Dou you use super power(yes/no): ";
            cin >> super_power;
            
            if (super_power == "no") {
                attack = 1 +  rand() % 6; 
                cout << "**Master attack = " << attack;
                cout << "\n**Monster attack = " << attackM;
                lifeM -= attackM;
                lifeMonster -= attack;
                cout << "\nLife status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
            }
            if (super_power == "yes") {
                attack = 2 * (1 +  rand() % 6);
                cout << "**Master attack = " << attack;
                cout << "\n**Monster attack = " << attackM;
                lifeM = 0;
                lifeM -= attackM;
                lifeMonster -= attack*2;
                cout << "\nLife status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
            }
        }
        else if (player == 'h' && lifeH > 0) {
            cout << "Dou you use super power(yes/no): ";
            cin >> super_power;
            
            if (super_power == "no") {
                attack = 1 +  rand() % 6; 
                cout << "**Master attack = " << attack;
                cout << "\n**Monster attack = " << attackM;
                lifeH -= attackM;
                lifeMonster -= attack;
                cout << "\nLife status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
            }
            if (super_power == "yes") {
                cout << "**Master attack = " << attack;
                cout << "\n**Monster attack = " << attackM;
                lifeW = 10;
                lifeH -= attackM;
                lifeMonster -= attack;
                cout << "\nLife status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
            }
        }
        else if (player == 'w' && lifeW > 0) {
            attack = 1 +  rand() % 6; 
            cout << "**Master attack = " << attack;
            cout << "\n**Monster attack = " << attackM;
            lifeW -= attackM;
            lifeMonster -= attack;
            cout << "\nLife status: M = " << lifeM  << " H = " << lifeH << " W = " << lifeW << "; Monster = " << lifeMonster;
        }
        else {
            cout << "Choose another player: \n";
        }
    }
    if (lifeMonster < 0) {
        cout << "You Won.";
    }
    else {
        cout << "You lose.";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog