Skip to content

Instantly share code, notes, and snippets.

@xfbs
Created August 23, 2011 19:28
Show Gist options
  • Save xfbs/1166237 to your computer and use it in GitHub Desktop.
Save xfbs/1166237 to your computer and use it in GitHub Desktop.
Daniel's C++ Army Game
#include <iostream>
//#include <cctype>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::endl;
class Soldier;
class Soldier
{
public:
Soldier(int amount);
void MakeCall(string location);
void Shoot(string gun, string object);
bool Retreat();
bool Attack();
int CheckMorale();
int CheckPhoneBattery();
int CheckAmmo();
//~Soldier(); // not defined, so we don't need to have it in the header
private:
int phone_battery;
int ammo;
bool individual_shoot;
int morale;
int soldier_amount;
};
Soldier::Soldier(int amount)
{
if (amount == 1)
cout << "You have dispatched a soldier on the battlefield";
else if (amount > 1 && amount < 11)
cout << "You have dispatched a squadron of " << amount << " soldiers onto the battlefield";
else if (amount > 11)
cout << "You have dispatched an army of " << amount << "soldiers on the battlefield";
phone_battery = 100;
ammo = 1000;
morale = 100;
soldier_amount = amount;
individual_shoot = false;
}
void Soldier::MakeCall(string location)
{
cout << "The commander called " << location;
phone_battery -= 20;
}
void Soldier::Shoot(string gun, string object)
{
//gun = tolower(gun); // will cause compiler error
cout << "You used the Gun: " << gun << " to shoot " << object;
if (soldier_amount < ammo)
{
cout << "Not enough ammo for everyone...";
individual_shoot = true;
}
if (gun == "pistol")
{
if (!individual_shoot)
{
ammo -= soldier_amount;
}
else
{
ammo = 0;
}
}
else if (gun == "rifle")
{
if (!individual_shoot)
{
ammo -= (soldier_amount*2);
}
else
{
ammo = 0;
}
}
else if (gun == "machine_gun")
{
if (!individual_shoot)
{
ammo -= (soldier_amount*5);
}
else
{
ammo = 0;
}
}
}
bool Soldier::Retreat()
{
morale -= 30;
return true;
}
bool Soldier::Attack()
{
morale += 10;
return true;
}
int Soldier::CheckMorale()
{
return morale;
}
int Soldier::CheckPhoneBattery()
{
return phone_battery;
}
int Soldier::CheckAmmo()
{
return ammo;
}
int main()
{
Soldier sol(3);
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment