- Code: Select all
#include "stdafx.h"
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <cmath>
#include <iostream> // for cin() and cout()
using namespace std;
int GetRand(int rangeMin, int rangeMax)
{
return (int)floor(rand() / (RAND_MAX + 1.0) * ((rangeMax - rangeMin) + 1) + rangeMin);
}
class Player
{
private:
//Declare Player Values
int MaxHealth;
int Health;
int MinDamage;
int MaxDamage;
int DamageResistance;
int Damage;
public:
Player():
MaxHealth(100),
Health(100),
MinDamage(0),
MaxDamage(10),
DamageResistance(2)
{
}
int GetMaxHealth() { return MaxHealth; }
int GetHealth() { return Health; }
int GetDamageResistance() { return DamageResistance; }
int GetMaxDamage() { return MaxDamage; }
int GetDamage()
{Damage = GetRand(MinDamage, MaxDamage);
return Damage;
}
};
class Enemy
{
private:
//Declare Enemy Values
int MaxHealth;
int Health;
int MinDamage;
int MaxDamage;
int DamageResistance;
int Damage;
public:
Enemy():
MaxHealth(100),
Health(100),
MinDamage(0),
MaxDamage(10),
DamageResistance(2)
{
}
int GetMaxHealth() { return MaxHealth; }
int GetHealth() { return Health; }
int GetDamageResistance() { return DamageResistance; }
int GetMaxDamage() { return MaxDamage; }
int GetDamage()
{Damage = GetRand(MinDamage, MaxDamage);
return Damage;
}
};
int main()
{
// set initial seed value to system clock
srand((long)time(0));
Player Player;
Enemy Enemy;
int Player_Health = Player.GetHealth();
int Player_MaxHealth = Player.GetMaxHealth();
int Player_DamageResistance = Player.GetDamageResistance();
int Player_Damage = Player.GetDamage();
int Player_MaxDamage = Player.GetMaxDamage();
int Enemy_Health = Enemy.GetHealth();
int Enemy_MaxHealth = Enemy.GetMaxHealth();
int Enemy_DamageResistance = Enemy.GetDamageResistance();
int Enemy_Damage = Enemy.GetDamage();
int Enemy_MaxDamage = Enemy.GetMaxDamage();
for(int CombatOver = 0;CombatOver = 1;)
{
Player_Damage = Player.GetDamage();
Enemy_Damage = Enemy.GetDamage();
if (Player_Damage > Player_MaxDamage)
{
cout << "Error, Players's Damage was more than its maximum value!";
Player_Damage = Player_MaxDamage;
}
if (Enemy_Damage > Enemy_MaxDamage)
{
cout << "Error, Enemy's Damage was more than its maximum value!";
Enemy_Damage = Enemy_MaxDamage;
}
if (Enemy_Health <= 1)
{
cout << "You killed the Enemy";
break;
}
if (Player_Health <= 1)
{
cout << "You Died";
break;
}
if (Player_Health > Player_MaxHealth)
{
Player_Health = Player_MaxHealth;
cout << "Error, Players's health was more than its maximum value!";
}
if (Enemy_Health > Enemy_MaxHealth)
{
Enemy_Health = Enemy_MaxHealth;
cout << "Error, Enemy's health was more than its maximum value!";
}
if (Player_Damage > Enemy_DamageResistance)
{
if ((Player_Damage + Enemy_DamageResistance) < (Player_MaxDamage + 1))
{
cout << "Player is attacking! \n";
Enemy_Health = Enemy_Health - (Player_Damage + Enemy_DamageResistance);
cout << "Player hits Enemy for ";
cout << Player_Damage + Enemy_DamageResistance;
cout << " Damage! \n";
cin.get();
}
}
else
{
cout << "Player Misses Enemy! \n\n";
cin.get();
}
if (Enemy_Damage > Player_DamageResistance)
{
if ((Enemy_Damage + Player_DamageResistance) < (Enemy_MaxDamage + 1))
{
cout << "Enemy is attacking! \n";
Player_Health = Player_Health - (Enemy_Damage + Player_DamageResistance);
cout << "Enemy hits Player for ";
cout << Enemy_Damage + Player_DamageResistance;
cout << " Damage! \n";
cin.get();
}
}
else
{
cout << "Enemy Misses Player! \n\n";
cin.get();
}
}
cin.get();
}
