Answer to Question #155878 in C++ for shemark

Question #155878

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.


1
Expert's answer
2021-01-17T20:56:05-0500
// monsterbattle.h:

#ifndef MONSTERBATTLE_H
#define MONSTERBATTLE_H

typedef enum TYPE { EMPTY=1, PRIZE, MONSTER } TYPE;

//Character represents monster
typedef struct {
   int hitPoints;
   int experiencePoints;
}Character;

typedef struct {
   int points;
}Prize;

typedef struct Room {
   TYPE type;
   Character monster;
   Prize prize;
} Room;

#endif



// monsterbattle.c

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

#include "monsterbattle.h"
inline int getRandomNumber(int min,int max) {
   time_t t;
   int num;
   srand((unsigned) time(&t));
   num = rand()%(max-min+1) + min;
   //printf("\nMin: %d, Max:%d, Rand generated: %d\n",min,max, num);
   return num;
  
}

void fillRooms(Room* rooms, int length){
   for(int i=0;i<length;++i) {
       int r = getRandomNumber(0,9);
       // r<1 for 10%
       if(r<1) { rooms[i].type = EMPTY;}
       // 5 because 10% + 40% = 50%
       else if(r<5) {
       rooms[i].type = PRIZE;
       (rooms[i].prize).points = getRandomNumber(5,10);
       }
       else {
           rooms[i].type = MONSTER;
           (rooms[i].monster).hitPoints = getRandomNumber(10,30);
           (rooms[i].monster).experiencePoints = (((rooms[i].monster).hitPoints %3 == 0)?1:0 );
          
       }
   }
}

void enterRoom (Room room, Character* player, int *resultHP, int *resultXP ) {
   if (room.type == EMPTY) {
       printf("\nRoom is empty");
       *resultHP = 0;
       *resultXP = 0;
   }
   else if(room.type == PRIZE) {
       printf("\nRoom has a prize. It has %d points", room.prize.points);
       *resultHP = room.prize.points;
       *resultXP = 0;
       player->hitPoints += *resultHP;
   }
   else {
       printf("\nRoom has a monster.");

       *resultHP = (room.monster).hitPoints;
       *resultXP = room.monster.experiencePoints;
       player->hitPoints -= *resultHP;
   }
  
   player->experiencePoints += *resultXP;
}

void printCharacter(Character c) {
   printf("\nPlayer(HP:%d,XP:%d)",c.hitPoints, c.experiencePoints);
}

int main() {
      /* 1. Declare your variables here */
    unsigned int numRooms=0;
   Room* roomArr;
   int resultHP=0, resultXP=0;
    /* 2. change the seed for random */
   time_t tm;
   srand((unsigned) time(&tm));
  
   /* 3. Initialize the player struct object, to have 50 hitpoints and 0 experience points by default */
   Character player = {50,0};
    /* 4. Dynamically allocate an array of Room structs with the number the user
       entered. Assume that the user has entered a positive number */
    printf("Enter the number of rooms:");
   scanf("%d",&numRooms);
  
   roomArr = (Room*) malloc (numRooms * sizeof(Room));
    /* 5. Call the fillRooms function() to create the rooms */
   fillRooms(roomArr,numRooms);
  
    /* 6. Print out a message to say how many rooms there are and that the challenge has started. */
   printf("\nNumber of rooms: %d",numRooms);
   printf("\nChallenge has started");

    /* 7. Call printPlayer() to print out the beginning stats */
   printCharacter(player);
    // 8. Taverse the array of rooms, for each room:
    for(int i=0;i<numRooms;++i) {
     
    //        8.1 Call enterRoom()
       enterRoom(roomArr[i],&player,& resultHP,&resultXP);
      //      8.2 Print out a message about how many hitpoints were lost or gained while in this room. Print out this value in a positive number (You lost 5 hitpoints while in this room).
       if (resultHP>0) {printf("\nYou gained %d hit-points in this room.",resultHP);}
       else {printf("\nYou lost %d hit-points in this room:%d",resultHP);}
        //    8.3 Determine if entering the room resulted in a gain in experience points, and print out an appropriate message stating as such. If not experience points were gained, do not print out a message.
       if (resultXP > 0) {       printf("\nYou gained %d experience-points in this room.",resultXP); }
              
          // 8.4 Update the player's hitpoints with the loss or gain. Never allow for the player's hitpoints to be negative. If it goes below zero, simply set the player's hitpoints to 0. If the player's hitpoints is zero, break the loop as the game is over.
      
       if (player.hitPoints < 0) {
           player.hitPoints = 0;
       }
       else if(player.hitPoints == 0) {
           printf ("\nGAME OVER !!");
           break;
       }
//            8.5 Update the player's experience points
      
//          8.6 Call printPlayer() to print out the updated player's stats.
       printCharacter(player);
       player.hitPoints = player.hitPoints + player.experiencePoints;
   }
    // 9. Outside the loop, print out a message of congratulations if the player has any hitpoints left. If a player has zero hitpoints, print out a message of regret.
    player.hitPoints = player.hitPoints + player.experiencePoints;
    if(player.hitPoints > 0) { printf("\nCongratulations!! You have won!!");}
   else { printf("\nSorry!! You lost!!");}
      // 10. deallocate the array of rooms.
   free(roomArr);
  
   return 0;
}

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
APPROVED BY CLIENTS