i was coding in visual studio c++ i just got into classes i was writing a basic "text game" in which 2 warriors fight in this code i had 2 classes one was a called a warrior class and the seccond was the battle class. i know the text was gonna be mess because i havent writen endl in most of the output operators but i executed it to figure out if it was working or not when i debugged it and fixed all the syntax errors this came up in visual studio.(in the picture down below here is my code
#include "pch.h"
#include <iostream>
#include<string>
#include<vector>
#include<ctime>
#include<numeric>
#include<math.h>
using namespace std;
class warrior {
private:
int attackMAX;
int blockMAX;
public :
string name;
int health;
warrior(string name, int health, int attackMAX, int blockMAX ) {
this->name = name;
this->health = health;
this->attackMAX = attackMAX;
this->blockMAX = blockMAX;
}
int attack() {
return rand() % this->attackMAX;
}
int block() {
return rand() % this->blockMAX;
}
};
class battle {
public:
static void startfight(warrior& warrior1,
warrior& warrior2) {
while (true) {
if (battle::getattackresult(warrior1, warrior2).compare("game over") == 0) {
cout << "game over" << endl;
break;
}
if (battle::getattackresult(warrior2, warrior1).compare("game over") == 0) {
cout << "game over" << endl;
break;
}
}
}
static string getattackresult(warrior& warriora,
warrior& warriorb) {
int warrioraattackamount = warriora.attack();
int warriorbblockamount = warriorb.block();
int damage2warriorb = ceil(warrioraattackamount - warriorbblockamount);
if (damage2warriorb < 0) {
damage2warriorb = 0;
}
warriorb.health = warriorb.health - damage2warriorb;
cout << warriora.name << "attacks" << warriorb.name << "and deals" << damage2warriorb << "damage";
cout << warriorb.name << "is down to only" << warriorb.health;
if (warriorb.health <= 0) {
cout <<warriorb.name<<"got destroyed by"<<warriora.name<<"and he is now victorious";
return "game over";
}
}
};
int main()
{
srand(time(NULL));
warrior thor("thor", 100, 30, 15);
warrior hulk("hulk", 135, 25, 10);
battle::startfight(thor, hulk);
return 0;
}
User contributions licensed under CC BY-SA 3.0