Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Game.h #pragma once #include using namespace std; namespace game { struct Hero { string name = ; int healthMax = 5 ; int

Game.h
#pragma once
#include
using namespace std;
namespace game {
struct Hero{
string name ="";
int healthMax =5;
int health =5;
int defence =2;
int attack =0;
};
void play();
bool fight(Hero* player);
void levelup(Hero* player);
void attack(Hero* a, Hero* b);
void showstats(Hero hero);
string nameGenerator();
}
Game.cpp
#include "Game.h"
#include
void game::play(){
Hero player;
cout "Please enter your name: ";
cin.ignore(1);
getline(cin, player.name);
int fightsWon =0;
while (fight(&player)== true){
fightsWon++;
if (fightsWon 5){
levelup(&player);
player.health = player.healthMax;
}
else {
cout " You've defeated 5 opponents and won the championship!" endl;
}
}
}
void game::levelup(Hero* player){
int input;
cout "You've won a fight! Choose an attribute to increase. " endl endl;
cout "1) Increase Health by 2(currently " player->healthMax ")" endl;
cout "2) Increase Attack by 1(currently " player->attack ")" endl;
cout "3) Increase Defence by 1(currently " player->defence ")" endl;
cin >> input;
switch (input){
case 1:
player->healthMax +=2;
break;
case 2:
player->attack++;
break;
case 3:
player->defence++;
break;
}
}
void game::showstats(Hero hero){
cout hero.name endl;
cout "Health: " hero.health endl;
cout "Attacks: " hero.attack endl;
cout "Defence: " hero.defence endl endl;
}
bool game::fight(Hero *player){
Hero enemy;
enemy.name = nameGenerator();
enemy.health =(rand()%8+1);
enemy.defence = rand()%4;
enemy.attack = rand()%4;
cout endl player->name " faces " enemy.name "!" endl;
showstats(*player);
showstats(enemy);
}
Main.cpp
#include
#include
#include
#include "Game.h"
using namespace std;
int main(){
srand(time(NULL));
enum State {
QUIT ,
PLAY ,
HELP ,
MAINMENU
};
State currentState = MAINMENU;
int input;
while (currentState != QUIT){
switch (currentState){
case MAINMENU:
cout "Welcome What would you like to do " endl endl;
cout "1) Play " endl endl;
cout "2) Help " endl endl;
cout "3) Quit game " endl endl;
cin >> input;
if (input 0|| input >2){
cout "Invalid input." endl;
}
else {
currentState = static_cast(input);
}
break;
case PLAY:
game::play();
currentState = MAINMENU;
break;
case HELP:
cout "How to play" endl;
cout " You, and many other heroes, have been selected to compete " endl;
cout " against each other for sport and glory in a compition " endl;
cout " for the ages! Name your champion and attempt to defeat five " endl;
cout " advesaries to win the tournoment. After each victory you " endl;
cout " will get to enhance one of your abilities. Choose wilsey! " endl;
currentState = MAINMENU;
break;
}
}
return 0;
}
bool game::fight(Hero* player){
Hero enemy;
enemy.name = "Opponent";
enemy.health =(rand()%8+1);
enemy.defence = rand()%4;
enemy.attack = rand()%4;
cout endl player->name "faces" enemy.name "!" endl;
while (player->health >0 && enemy.health >0)
{
attack(player, &enemy);
if (enemy.health >0){
attack(&enemy, player);
}
cout endl;
}
if (player->health >0){
cout player->name "was defeatedd by" enemy.name "!" endl endl;
return true;
}
else {
cout player->name " was defeated by " enemy.name "." endl endl;
return false;
}
}
void game::attack(Hero* a, Hero* b){
int roll = rand()%6+1+ a->attack;
cout endl a->name " attacks " b->name " for " roll;
if (roll > b->health ){
b->health -= roll - b->defence;
if (b->health 0){
cout ", vanquishing them!";
} else {
cout ", bringing " b->name "'s health down to " b->health "!";
}
}
else {
cout ", but they managed to dodge out of the way.";
}
}
string game::nameGenerator(){
string preflix[2]={
};
string adjective[11]={
"Ultimate" ,
"Steel" ,
"Iron" ,
"Wicked" ,
"Striking" ,
"Azure" ,
"Pink" ,
"Emerald" ,
"Crimson" ,
"Siland" ,
"Keen" ,
};
string noun[10]={
"Brick" ,
"Tower" ,
"One" ,
"Advercery" ,
"Warrior" ,
"Wanderer" ,
"Fists" ,
"Professor" ,
"Contendor" ,
"Warlock" ,
};
string name = preflix[rand()%2];
name += adjective[rand()%11];
name += noun[rand()%10];
return name;
}
this is my code fix it to match the display in the picture madith 5
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions