In: Computer Science
Source code with comments explaining your code in C#
Program 2: 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 the maximum damage the player does, and then subtract that from the monster. A similar thing happens when the monster attacks 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) Sample run 1: Enter the hero's starting hit points: 50 Enter the damage the hero’s weapon does per strike: 20 Enter the monster's starting hit points: 40 Enter the monster's damage per strike: 15 ====== ROUND 1 ====== Hero attacks for: 10 Monster has 30 HP left Monster attacks you for: 1 You have 49 HP left ====== ROUND 2 ====== Hero attacks for: 18 Monster has 12 HP left Monster attacks you for: 7 You have 42 HP left ====== ROUND 3 ====== Hero attacks for: 0 Monster has 12 HP left Monster attacks you for: 14 You have 28 HP left ====== ROUND 4 ====== Hero attacks for: 18 Monster has -6 HP left The monster dies and you earn 5 XP Battle ends...
***no extra text after hero/monster dies!***
PSEUDOCODE:
Main() {
input
heroHP,hPerStrikeDamage,monsterHP,mPerStrikeDamage,round=1;
Object of random class to generate random numbers
Random rnd <-- new Random();
Ask for hero's starting point
heroHP <-- Convert.ToInt32(Console.ReadLine());
Ask for hero's damage per strike
hPerStrikeDamage <-- Convert.ToInt32(Console.ReadLine());
Ask for monster's starting point
monsterHP <-- Convert.ToInt32(Console.ReadLine());
Ask for monster's damage per strike
mPerStrikeDamage=Convert.ToInt32(Console.ReadLine());
while(true){
hero's Damage to monster
heroDamage <-- rnd.Next(hPerStrikeDamage);
monster's updated HP
monsterHP <-- monsterHP-heroDamage;
monster's Damage to hero
monsterDamage=rnd.Next(mPerStrikeDamage);
hero's updated HP
heroHP <-- heroHP-monsterDamage;
if(monsterHP<=0){
print "The monster dies and you earn 5 XP Battle ends..."
break;
}
//Displaing result of the game
if(heroHP<=0){
print "The Hero dies and the monster earns 5 XP Battle ends"
break;
}
round+=1;
}
}
}
CODE FOR THE GIVEN PROGRAM:
using System;
class HelloWorld {
static void Main() {
int
heroHP,hPerStrikeDamage,monsterHP,mPerStrikeDamage,round=1;
//Object of random class to generate random numbers
Random rnd = new Random();
//Asking for hero's starting point
Console.WriteLine(" Enter the hero's starting hit points:");
heroHP = Convert.ToInt32(Console.ReadLine());
//Asking for hero's damage per strike
Console.WriteLine("Enter the damage the hero’s weapon does per
strike:");
hPerStrikeDamage=Convert.ToInt32(Console.ReadLine());
//Asking for monster's starting point
Console.WriteLine(" Enter the monster's starting hit
points:");
//Asking for monster's damage per strike
monsterHP = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the monster's damage per strike:");
mPerStrikeDamage=Convert.ToInt32(Console.ReadLine());
while(true){
//hero's Damage to monster
int heroDamage= rnd.Next(hPerStrikeDamage);
//monster's updated HP
monsterHP=monsterHP-heroDamage;
//monster's Damage to hero
int monsterDamage=rnd.Next(mPerStrikeDamage);
//hero's updated HP
heroHP=heroHP-monsterDamage;
//Displaying HP's after each round
Console.WriteLine("=========ROUND"+round+"=========");
Console.WriteLine("Hero attacks for: "+heroDamage);
if(monsterHP<=0){
Console.WriteLine("The monster dies and you earn 5 XP Battle
ends...");
break;
}
Console.WriteLine("Monster has "+ monsterHP +" HP left");
Console.WriteLine("Monster attacks you for: "+monsterDamage);
Console.WriteLine("You have "+ heroHP +" HP left");
round+=1;
//Displaing result of the game
if(heroHP<=0){
Console.WriteLine("The Hero dies and the monster earns 5 XP Battle
ends");
break;
}
}
}
}
SCREENSHOT OF THE GIVEN PROGRAM AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING