In: Computer Science
Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and up to the maximum damage the player does, and then subtract that from the monster. The same thing happens when the monster attacks the hero, but damage is to the hero. The program should display rounds and the HP of the hero and monster each round. If the hero or monster dies, it should print that this happened and should NOT continue (i.e. no extra text). To learn how to create random numbers, see the appendix.
Source Code :
#include <iostream>
#include<random>
using namespace std;
int attack(int maxVal){
//simulates an attack
random_device d;
mt19937 rng(d());
uniform_int_distribution<mt19937::result_type>
dist6(0,maxVal); // distribution in range [0, maxVal]
return dist6(rng); //returns a random number
between 0 and maxVal (both included)
}
int main() {
int heroHp , monsterHp , heroDmg, monsterDmg;
cout << "Enter hero's HP" << endl;
cin >> heroHp;
cout << "Enter hero's maximum damage" <<
endl;
cin >> heroDmg;
cout << "Enter monster's HP" <<
endl;
cin >> monsterHp;
cout << "Monster's maximum damage" <<
endl;
cin >> monsterDmg;
while(true){
//RPG game start
if(heroHp <= 0){
cout <<
"Hero Died" << endl;
break;
}
if(monsterHp <= 0){
cout <<
"Monster Died" << endl;
break;
}
cout << "Hero's HP = "
<< heroHp << endl;
cout << "Monster's HP = "
<< monsterHp << endl;
heroHp -= attack(monsterDmg); //
subtract monster's damage from hero's Hp
monsterHp -= attack(heroDmg); //
subtract hero's damage from monster's Hp
}
return 0;
}
Input :
Output :
Source Code (with syntax highlighted) :
If you have trouble compiling, please switch to C++ 11.
If you liked this solution, please give a thumbs up rating. Feel free to comment for any explanation/clarification.