Question: [C++] Please pro vide solution as my code is incomplete. // I have three of this same question posted, will rate well on all three
[C++] Please pro vide solution as my code is incomplete. // I have three of this same question posted, will rate well on all three if answered (will provide link)
TEXT FILE
Paper Noah 104 10 9 Rock Liam 102 7 2 Rock Mason 107 9 2 Scissor Jacob 112 7 Rock William 80 2 1 Scissor Ethan 81 8 Scissor James 86 5 Rock Alexander 107 9 1 Rock Michael 80 7 2 Paper Benjamin 80 7 8 Paper Elijah 84 6 2 Paper Daniel 107 1 4
Tournament Example
For example, suppose theres 5 monsters at the beginning of the tournament (monsters A,B,C,D,E).
For the first round, the monsters are randomly shuffled: B, D, A, C, E
Monster B fights monster D
Monster A fights monster C
Monster E has a bye and fights nobody
Now assume that monster D wins, and monster A wins, thus monsters D, A, and E continue to the second round of the tournament.
For the second round, the monsters are randomly shuffled: E D A
Monster E fights monster D
Monster A has a bye and fights nobody.
Now assume that monster E wins, thus monsters E and A continue to the final round of the tournament.
In the final round of the tournament, assume that monster A wins, thus monster A wins the tournament.
Monster Capabilities
Each monster is of type Rock, Paper, or Scissors.
Each monster enters the tournament with a certain amount of hit-points.
A rock monster has properties for mass and velocity. When a rock monster attacks, it deals damage with the following formula: damage = mass * velocity * velocity
A paper monster has properties for length and width. When a paper monster attacks, it deals damage with the following formula: damage = length * width
A scissors monster has a property for sharpness. When a scissors monster attacks, it deals damage with the following formula: damage = sharpness
Battle Format
Monsters will attack each other sequentially.
Each battle continues until one of the monsters has 0 or fewer hit-points.
It is arbitrary which monster attacks first.
For example
Suppose monster A is a paper monster with 100 hit-points, length: 10, and width: 5.
Suppose monster B is a rock monster with 90 hit-points, mass: 5, and velocity: 3.
Monster A attacks first, dealing 10*5 = 50 damage to monster B.
Monster B now has 90 50 = 40 hit-points.
Monster B now attacks monster A, dealing 5*3*3 = 45 damage.
Monster A now has 100 45 = 55 hit-points.
Monster A now attacks monster B, again dealing 50 damage.
Monster B now has 40-50 = -10 hit-points, so monster B dies and monster A has won the battle.
Monster A continues to the next round of the tournament.
At the end of the battle, a monsters hit-points is NOT reset. In the example above, monster A would enter the next round of the tournament with 55 hit-points.
CODING (LINK : https://www.chegg.com/homework-help/questions-and-answers/c-rock-monster-properties-mass-velocity--rock-monster-attacks-deals-damage-following-formu-q25257304 )
//Monster.h
#pragma once
#ifndef MONSTER_H
#define MONSTER_H
#include
#include
using namespace std;
class Monster {
public:
Monster();
string name;
int target;
virtual string getMonsterType() = 0;
virtual bool attack(Monster* target) = 0;
};
#endif // !MONSTER_H
//RockMonster.h
#pragma once
#ifndef ROCKMONSTER_H
#define ROCKMONSTER_H
#include"Monster.h"
class RockMonster : public Monster
{
public:
int mass;
int velocity;
RockMonster(string name,int mass, int velocity,int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif // !ROCKMONSTER_H
//PaperMonster.h
#pragma once
#ifndef PAPERMONSTER_H
#define PAPERMONSTER_H
#include"Monster.h"
class PaperMonster : public Monster
{
public:
int length;
int width;
PaperMonster(string name, int length,int width,int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif // !PAPERMONSTER_H
//Scissors.h
#pragma once
#ifndef SCISSORSMONSTER_H
#define SCISSORSMONSTER_H
#include"Monster.h"
class ScissorsMonster : public Monster
{
public:
int sharpness;
ScissorsMonster(string name, int sharpness, int target);
string getMonsterType();
bool attack(Monster* target);
};
#endif // !SCISSORSMONSTER_H
//Monster.cpp
#include"Monster.h"
Monster::Monster() {
}
//RockMonster.cpp
#include"RockMonster.h"
RockMonster::RockMonster(string name, int mass, int velocity, int target)
{
this->mass = mass;
this->velocity = velocity;
}
string RockMonster::getMonsterType()
{
return name;
}
bool RockMonster::attack(Monster * target)
{
if (target->target >= mass*velocity*velocity)
return true;
else
return false;
}
//PaperMonster.cpp
#include "PaperMonster.h"
PaperMonster::PaperMonster(string name, int length, int width, int target)
{
this->length = length;
this->width = width;
}
string PaperMonster::getMonsterType()
{
return name;
}
bool PaperMonster::attack(Monster * target)
{
if (target->target >= length*width)
return true;
else
return false;
}
//Scissors.cpp
#include "Scissors.h"
ScissorsMonster::ScissorsMonster(string name, int sharpness, int target)
{
this->sharpness = sharpness;
}
string ScissorsMonster::getMonsterType()
{
return name;
}
bool ScissorsMonster::attack(Monster * target)
{
if (target->target >= sharpness)
return true;
else
return false;
}
//main.cpp
#include
#include"Monster.h"
#include"RockMonster.h"
#include"PaperMonster.h"
#include"Scissors.h"
#include
#include
#include
using namespace std;
//tokenizer
vector split(const string &s, char delim) {
stringstream ss(s);
string item;
vector tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
int main()
{
vector monsters;
vector targets;
string line;
ifstream myfile("P6Input.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << ' ';
vector res = split(line,' ');
string name;
if (res[0] == "Rock")
{
name = res[1];
int target = atoi(res[2].c_str());
int mass = atoi(res[3].c_str());
int velocity = atoi(res[4].c_str());
targets.push_back(target);
Monster *r = new RockMonster(name, mass, velocity,target);
monsters.push_back(r);
}
else if (res[0] == "Paper")
{
name = res[1];
int target = atoi(res[2].c_str());
int length = atoi(res[3].c_str());
int width = atoi(res[4].c_str());
targets.push_back(target);
Monster *r = new PaperMonster(name, length, width,target);
monsters.push_back(r);
}
else if (res[0] == "Scissor")
{
name = res[1];
int target = atoi(res[2].c_str());
int sharpness = atoi(res[3].c_str());
targets.push_back(target);
Monster *r = new ScissorsMonster(name, sharpness,target);
monsters.push_back(r);
}
}
myfile.close();
}
else
cout << "Unable to open file";
int i = 0;
for (auto it = monsters.begin(); it != monsters.end(); it++)
{
cout << "Monster Type: " << (*it)->getMonsterType() << endl;
if ((*it)->attack(*it))
cout << " Monster is dead" << endl;
else
cout << " Monster is still alive" << endl;
}
system("pasuse");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
