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"; }
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; }
First, you will need to create the Creature class. Creatures have 2 member variables: a name,...
using C++ Requirements: . Classes you must create o Monster must be an abstract class Each monster has . an attack) o randomly generate a number between 0 and strength for each attack (Strength 10 would generate 0-10) add up all the damage done return total damage o o e a number of attacks . a number for strength used during attacking . a string representing the type » accessor for type o Goblin inherits from Monster type is "Goblin"...
You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...
Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin { /** * Constructor for objects of class...
HELP....ITS CLOSING TONIGHT INFSCI 0017 – Assignment 4 You are doing an internship in a company specialized in create role-‐playing computer games. As a part of the programming team, you are asked to implement and test the first version of a RolePlayer class. In the game, a RolePlayer is a knight with the mission of fight and kill monsters: vampires and werewolves. As the player defeat monsters, he/she gains points. The player is considered trainee if he/she has less than...
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...
Student.h Student.cpp Person.h Person.cpp In this assignment, you need to create a class to keep track of books borrowed bya student. Each book has a name, Student (who borrowed it), and the date that was borrowed (see the .h file below). Use also need to use the Person class and the Student class included in the folder. Student class inherits from the Person class as was shown in the classroom Create a Date class (in the same project of the...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...
I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good to me but it will not run. Please help... due in 24 hr. Problem As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. Currently, there is a test...