1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <thread>
#include <random>
using namespace std::chrono_literals;
class Weapon
{
public:
Weapon() :
name_("Unknown"),
verb_("Unknown"),
damage_(0),
accuracy_(0),
fire_rate_(0)
{
}
// damage - 1+
// accuracy - 0 to 100 (higher is better)
// fire_rate - 0 to 100 (lower is faster)
Weapon(std::string name, std::string verb, unsigned damage, unsigned accuracy, unsigned fire_rate) :
name_(name),
verb_(verb),
damage_(damage),
accuracy_(accuracy),
fire_rate_(fire_rate)
{
}
Weapon(const Weapon& other)
{
name_ = other.name_;
verb_ = other.verb_;
damage_ = other.damage_;
accuracy_ = other.accuracy_;
fire_rate_ = other.fire_rate_;
}
bool operator< (const Weapon& other) const
{
return name_ < other.name_;
}
std::string Name() const { return name_; }
std::string Verb() const { return verb_; }
unsigned Damage() const { return damage_; }
unsigned Accuracy() const { return accuracy_; }
unsigned FireRate() const { return fire_rate_; }
private:
std::string name_;
std::string verb_;
unsigned damage_;
unsigned accuracy_;
unsigned fire_rate_;
};
enum WeaponName
{
KNIFE,
REVOLVER,
BERETTA_M9
};
enum DinoWeaponName
{
CLAW,
BITE,
VICIOUS_CLAW
};
std::map<WeaponName, Weapon> Weapons
{
{ KNIFE, {"Knife", "slashes", 2, 70, 40} },
{ REVOLVER, {"Revolver", "shoots", 4, 50, 50} },
{ BERETTA_M9, {"Beretta M9", "shoots", 10, 80, 20} },
};
std::map<DinoWeaponName, Weapon> DinoWeapons
{
{ CLAW, {"Claw", "claws", 1, 50, 40} },
{ BITE, {"Bite", "lunges", 2, 75, 50} },
{ VICIOUS_CLAW, {"Vicious Claw", "viciously slashes", 8, 95, 60} },
};
class Dinosaur
{
public:
Dinosaur(std::string name, Weapon dino_weapon, int health) :
name_(name),
dino_weapon_(dino_weapon),
health_(health)
{
}
void TakeDamage(int damage) { health_ -= damage; }
std::string Name() const { return name_; }
Weapon GetWeapon() const { return dino_weapon_; }
int Health() const { return health_; }
private:
std::string name_;
Weapon dino_weapon_;
int health_;
};
class Human
{
public:
Human(std::string name, Weapon weapon, int health) :
name_(name),
weapon_(weapon),
health_(health)
{
AddWeapon(weapon);
}
void AddWeapon(Weapon weapon)
{
weapons_.insert(weapon);
}
void Equip(const Weapon& weapon)
{
if (weapons_.find(weapon) == weapons_.end())
{
std::cout << "You do not own the weapon "<< weapon.Name() << '\n';
return;
}
weapon_ = weapon;
}
void TakeDamage(int damage) { health_ -= damage; }
void Fight(Dinosaur& dino)
{
if (dino.Health() <= 0)
{
std::cout << dino.Name() << " is already dead.\n";
return;
}
if (health_ <= 0)
{
std::cout << name_ << " is dead and cannot start a Fight.\n";
return;
}
std::cout << name_ << " (" << health_ << " HP), " << weapon_.Name() <<
", prepares to fight\n" <<
dino.Name() << " (" << dino.Health() << " HP), " << dino.GetWeapon().Name() << " !\n\n";
std::cout << "3..." << std::flush;
std::this_thread::sleep_for(1s);
std::cout << "2..." << std::flush;
std::this_thread::sleep_for(1s);
std::cout << "1..." << std::flush;
std::this_thread::sleep_for(1s);
std::cout << "FIGHT!\n\n" << std::flush;
std::this_thread::sleep_for(1s);
const Weapon& dino_weapon = dino.GetWeapon();
long total_time = 0;
int tick = 5; // Some divisor of all possible fire rates
while (true)
{
// Human tries to hit dino if Accuracy RNG favors him
if (total_time % weapon_.FireRate() == 0)
{
if (rd_()%101 <= weapon_.Accuracy())
{
std::cout << name_ << " " << weapon_.Verb() << " with the " <<
weapon_.Name() << " for " << weapon_.Damage() << " damage.\n";
dino.TakeDamage(weapon_.Damage());
if (dino.Health() <= 0)
{
std::cout << name_ << " has defeated " << dino.Name() << "!!!\n";
break;
}
}
else
{
std::cout << name_ << " misses.\n";
}
}
// Dino tries to hit human
if (total_time % dino_weapon.FireRate() == 0)
{
if (rd_()%101 <= dino_weapon.Accuracy())
{
std::cout << dino.Name() << " " << dino_weapon.Verb() << " with the " <<
dino_weapon.Name() << " for " << dino_weapon.Damage() << " damage.\n";
TakeDamage(dino_weapon.Damage());
if (health_ <= 0)
{
std::cout << dino.Name() << " has defeated " << name_ << "!!!\n";
break;
}
}
else
{
std::cout << dino.Name() << " misses.\n";
}
}
total_time+=tick;
}
std::cout << "\nRemaining health: " << health_ << "\n\n\n";
}
private:
std::string name_;
Weapon weapon_; // Currently equipped weapon
std::set<Weapon> weapons_;
int health_;
std::random_device rd_;
};
int main()
{
Human player("Chuck Norris", Weapons[KNIFE], 100);
player.AddWeapon(Weapons[BERETTA_M9]);
Dinosaur raptor1("Raptor", DinoWeapons[CLAW], 20);
Dinosaur raptor2("Raptor", DinoWeapons[CLAW], 22);
Dinosaur boss("T-Rex", DinoWeapons[VICIOUS_CLAW], 100);
Dinosaur super_boss("Ultra T-Rex", DinoWeapons[VICIOUS_CLAW], 500);
Dinosaur raptor3("Raptor", DinoWeapons[CLAW], 19);
player.Fight(raptor1); // Knife
player.Equip(Weapons[BERETTA_M9]); // Finding a better weapon
player.Fight(raptor2);
player.Fight(boss);
player.Fight(super_boss);
player.Fight(raptor3); // Player likely dead and cant fight further
return 0;
}
|