C++ You have 3 monsters – each monster has a health (starts at 100) and a damage
ID: 3771258 • Letter: C
Question
C++ You have 3 monsters – each monster has a health (starts at 100) and a damage potential (starts as a random number between 1 and 3, multiplied by 2.) 2 monsters are randomly chosen to battle: subtract damage potential from the health of the monsters. Report score at the end of the battle. Write all information to a text file sorted by health. Battle again, choosing 2 monsters again. Battle 1: Output to screen - Jabba vs Godzilla – Godzilla Wins! Add to file: Zombie 100 Godzilla 98 Jabba the Hutt 96 ----- Battle 2: Output to screen - Zombie vs Godzilla – Tie! Add to file: Jabba the Hutt 96 Zombie 96 Godzilla 94 ----- After every 10 rounds (if there are 10 rounds) give the user the option to stop play requirements: Proper use of constants Proper and useful variable and function naming, using convention Proper formatting Comments Compiles No Logic Errors Proper function prototyping Random number generation Function to choose battle monsters and run battle Function to setup Damage Function to setup Monsters Function to write file Option to stop play Random chooser for Monsters Output to file as outlined Pseudocode &/or flow chart Sort output Input validation Array for monster Array for damage Array for health please use just function and array. do not use class.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
//playing battle
void battle(){
//monsters names and their healths
string name[3];
int health[3];
std::ifstream readfile("monsters.txt");//reding healths from text file
std::string eachline;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int count = 0;
while (iss >> name[count] >> health[count])//reading each value from file
{
count++;
}
}
//removing potential health
for(int i=0;i<3;i++){
int damage_potential = rand() % 3 + 1;
health[i] = health[i] -damage_potential;
}
//deciding monsters to be play
int monster1 = rand() % 3;
int monster2 = rand() % 3;
if(monster1 == monster2){
while(monster1 == monster2){
monster2 = rand() % 3;
}
}
//printing playing monsters
cout<<" Now Game betwen"<< name[monster1] << "and"<<name[monster2];
if(health[monster1] > health[monster2]){
cout<<" "<<name[monster1] <<"wins";
}
else if(health[monster1] < health[monster2]){
cout<<" "<<name[monster2] <<"wins";
}
else{
cout<<" Its tie";
}
//outputting to file
ofstream out_data("monsters.txt");
for(int i=0;i<3;i++){
out_data<<name[i]<<" "<<health[i];
}
}
int main()
{
//variables declared
int rounds = 1;
while(true){
//playing battle
battle();
//asking userfor quit option after every 10 rounds
if(rounds/10 == 0){
int choice;
cout<<" Do you want to Exit(1): ";
cin >>choice;
if(choice == 1){
break;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.