Question

First, you will need to create the Creature class. Creatures have 2 member variables: a name,...

First, you will need to create the Creature class. Creatures have 2 member variables: a name, and the number of gold coins that they are carrying. Write a constructor, getter functions for each member, and 2 other functions:

- void addGoldCoins(int) adds gold coins to the Creature.

- void identify() prints the Creature information.

The following should run in main: Creature d{"dragon", 50}; d.addGoldCoins(10); d.identify(); // This prints: The dragon is carrying 60 gold coins.

Second, you are going to create the Prisoner class. The Prisoner class inherits from Creature and does not contain any extra functionality. A prisoner has 500 gold coins.

Third, you are going to create the Player class that inherits from Creature. Player has 2 additional members: the player’s level which starts at 1, and the number of lives left. Initially, a player can have up to 3 lives. When the player loses a level, the number of lives decrements by 1. The player has a custom name (entered by the user) and no gold coins to start with. Write a function called levelUp() that increases the player’s level by one. Write an isDead() function that returns true when the number of lives is 0. Finally, write a hasWon() function that returns true if the player reaches level 10. The following should run in main: cout << "Enter your name: "; cin >> playerName; // Assume user enters Joe Player p{playerName}; cout << "Welcome, " << p.getName() << "!" << endl; p.identify(); // This prints: Joe has 3 lives left and is carrying 0 gold coins.

Forth, you will write the Monster class. Monster also inherits from Creature and has an extra attribute which is the type. There are two types of monsters. Each monster type will have a different name and carry a different amount of gold coins. Type is an int which we will use as follows: Type Monster name Gold coins they are carrying 1 sphynx 100 2 dragon 50 In addition, you will need to add a challenge function. Each monster type will have a different challenge that the player will need to take up. - For a sphynx, the challenge will be to ask the player to compute a mathematical equation. If the player gives a correct answer, he/she will defeat the sphynx. You should use random numbers so that you will generate different questions each time a sphynx is encountered. For example, the sphynx might challenge the player to solve: ((x + y) * (z – w)) * t To do so, you will need to generate random numbers between 1 and 100 and assign them to x, y, z, w, t. Each time the challenge function is called, different random numbers will be assigned to these variables. You can of course change the equation. - For a dragon, the challenge will be for the player to try and guess a random number between 1 and 10. The player can have up to 3 chances otherwise they lose. If you want, you can write code to help the player by letting him/her know if their guess is lower or higher than the selected number. You might also need to add more functions to the Monster class, depending on how you implement the main game logic (see below). Game Rules Finally, you will implement the game.

Now for the fun part! Here is how the game is played:

- The player encounters one randomly generated monster at a time.

- For each monster encountered, the player must challenge the monster depending on the monster type.

- If the player wins, the player takes any gold coins the monster is carrying. The player also levels up, increasing his/her level by 1.

- If the player loses, he/she will lose a life and stay at the same level.

- The game ends when the player has died (loss) or has reached level 10 (win).

- If the player dies, the game should tell the player at what level they were and how many gold coins they had.

- If the player wins the game, the prisoner will be very grateful to be freed and will give the player his/her gold coins. The game should then tell the player that they won, and how many gold coins they had.

Here is a suggested but incomplete pseudo-code for your main program:

Create a player p Create a prisoner pr // if the player isn’t dead and hasn’t won yet, the game continues while (!p.isDead() && !p.hasWon()){ create a monster m bool won = fightMonster(m); if (won) advance level and acquire monster gold coins else decrement lives } // at this point, the player is either dead or has won the game if (p.isDead()) cout << p.getName() << " died at level " << p.getLevel() << " with " << p.getGoldCoins() << " gold coins\n"; else { take prisoner cold coins cout << "You won the game with " << p.getGoldCoins() << " gold coins! \n"; }


C++ is the programming language?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the below code I have implemented all the functionalities mentioned above.

Also, there are few lines of code which have been commented out which could be uncommented so that the game programmed will be more user friendly.

The code written will function as required, the comments could be removed if not required.

 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; class Creature{ string name; int goldCoins; public: Creature(string name, int goldCoins){ this->name = name; this->goldCoins = goldCoins; } string getName(){ return this->name; } int getGoldCoins(){ return this->goldCoins; } void addGoldCoins(int add){ this->goldCoins += add; } virtual void identify(){ cout << "The " << this->getName() << " is carrying " << this->getGoldCoins() << " gold coins." << endl; } }; class Prisoner : public Creature{ public: Prisoner(string name): Creature(name, 500){ } }; class Player : public Creature{ public: int level = 1; int lives = 3; Player(string playerName): Creature(playerName, 0){ } int getLevel(){ return this->level; } void levelUp(){ this->level++; } bool isDead(){ return (this->lives==0); } bool hasWon(){ return (this->level==10); } void identify(){ cout << this->getName() << " has " << this->lives << " lives left and is carrying " << this->getGoldCoins() << " gold coins.\n"; } }; class Monster : public Creature{ public: int type; Monster(int type, string name, int goldCoins): Creature(name, goldCoins){ this->type = type; } bool challenge(){ int number, response; if(this->type==1){ int x, y, z, w, t; x = 1 + rand()%100; y = 1 + rand()%100; z = 1 + rand()%100; w = 1 + rand()%100; t = 1 + rand()%100; cout << "Solve : ((" << x << "+" << y << ")*(" << z << "-" << w << "))*" << t << endl; number = ((x+y)*(z-w))*t; cin >> response; return (response == number); }else{ cout << "Guess a number between 1 and 10." << endl; number = 1 + rand()%10; int chances = 3; cin >> response; while(--chances&&response!=number){ // cout << "Wrong guess. Try again." << endl; cin >> response; } if(chances==0) return false; // cout << "Correct Guess!" << endl; return true; } } }; bool fightMonster(Monster m){ // m.identify(); return m.challenge(); } int main(){ srand(time(0)); Creature d("dragon", 50); d.addGoldCoins(10); d.identify(); string playerName; cout << "Enter your name: "; cin >> playerName; Player p(playerName); cout << "Welcome, " << p.getName() << "!" << endl; p.identify(); Prisoner pr("prisoner"); // Main game code starts while(!p.isDead()&&!p.hasWon()){ int monsterType = 1 + rand()%2; string monsterName; int monsterGoldCoins; if(monsterType==1){ monsterName = "sphynx"; monsterGoldCoins = 100; }else{ monsterName = "dragon"; monsterGoldCoins = 50; } Monster m(monsterType, monsterName, monsterGoldCoins); bool won = fightMonster(m); if(won){ p.levelUp(); p.addGoldCoins(m.getGoldCoins()); }else{ // cout << "You lost this level and you lose a life now." << endl; p.lives--; } // if(!p.isDead()) p.identify(); } if(p.isDead()){ cout << p.getName() << " died at level " << p.getLevel() << " with " << p.getGoldCoins() << " gold coins\n"; }else{ p.addGoldCoins(pr.getGoldCoins()); cout << "You won the game with " << p.getGoldCoins() << " gold coins! \n"; } return 0; } 
Add a comment
Know the answer?
Add Answer to:
First, you will need to create the Creature class. Creatures have 2 member variables: a name,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT