- Code: Select all
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>
using namespace std;
//prototypes
void typerPrint(int, string);
void noGold();
void printDisplay(int gold, int health, int playerDef, int playerAtt, string strWeapon, string playerName);
void welcomeDisplay();
void gameOver(int gold, int kills);
int toShop(int, int, int, int);
int toFight();
int toRest(int, int, int);
class Enemy {
int health;
int attackLvl;
int defence;
int gold;
string name;
bool isAlive, doesCritical;
public:
Enemy(int, int, int, string); //set health, attack, defence and name
int getHealth()
{
if (health > 0) return health;
else if (health <= 0) return 0;
else return 0;
}
int giveGold() {return gold;}
void kill() {isAlive = false;}
string getName() {return name;}
int attack(int playerAttLvl)
{
if (playerAttLvl > defence)
{
int hpLoss;
hpLoss = rand() % (playerAttLvl - defence) + 1;
if(rand() % 100 < 3)
{
doesCritical = true;
cout << "\t\tCRITICAL HIT!!!\n";
hpLoss *= 2;
}
health -= hpLoss;
return hpLoss;
}
else return 0;
}
int defend (int playerDef)
{
int healthLoss;
healthLoss = rand() % (attackLvl - (playerDef + 1));
return healthLoss;
}
void setHealth(int amount)
{
health = amount;
}
bool getIsAlive() {return isAlive;}
void bringToLife()
{
isAlive = true;
}
void takeLife()
{
isAlive = false;
}
};
Enemy::Enemy(int health, int attack, int defence, string name) // Enemy constructor
{
this -> health = health;
this -> attackLvl = attack;
this -> defence = defence;
gold = (health * attack)/4;
isAlive = true;
doesCritical = false;
}
int main()
{
//create enemy objects
Enemy drDegreaser(100, 10, 10, "Dr Degreaser");
Enemy marleyMop(200, 15, 15, "Marley Mop");
Enemy ninjaGlove(300, 20, 20, "Ninja Glove");
//shop variables
int priceofPeaShooter = 200, priceofSlingShot = 500, priceofNailGun = 1000, priceofFullHealth = 50;
int priceof1DayRest = 10, priceof2DayRest = 20, priceof1WeekRest = 30;
//player variables
int playerAttLvl = 30, playerGold = 0, playerDef = 30, playerHealth = 200, playerHealthLoss, killCount = 0;
bool hasPeaShooter = false, hasSlingShot = false, hasNailGun = false;
string strWeapon = "your bare hands", playerName;
//menu variables
char menuChoice= '1';
char quit = 'a';
int activity;
string attackAgain = "yes";
//Misc variables
int sleepTime = 2000;
//enter players name
cout << "Welcome to the game.\n\nPlease enter your name >> ";
getline(cin, playerName);
//game loop
while (playerHealth > 0) // game ends when player dies
{
//set the string for the equipped weapon
if (hasNailGun) strWeapon = "a nailgun";
else if(hasSlingShot) strWeapon = "a sling shot";
else if (hasPeaShooter) strWeapon = "a pea shooter";
drDegreaser.bringToLife();
marleyMop.bringToLife();
ninjaGlove.bringToLife();
do {
system("cls");
printDisplay(playerGold, playerHealth, playerDef, playerAttLvl, strWeapon, playerName); // implement stats display
cout << "\n\t\t\tWhere would you like to go?\n\n1.To Fight!\n2.To Ye Olde Shoppe\n3.To Rest >>> ";
cin >> menuChoice;
} while (menuChoice != '1' && menuChoice != '2' && menuChoice != '3');
switch (menuChoice)
{
case '1': activity = toFight(); break;
case '2': activity = toShop(priceofPeaShooter, priceofSlingShot, priceofNailGun, priceofFullHealth); break;
case '3': activity = toRest(priceof1DayRest, priceof2DayRest, priceof1WeekRest); break;
}
//Main Switch statment
switch (activity)
{
//error
case 0:
cout << "\n\n\t\tError";
return 0;
//fighting
case 1:
{
while (drDegreaser.getIsAlive() && playerHealth > 0)
{
system("cls");
cout << "You attack Dr Degreaser with " << strWeapon << " and deal " << drDegreaser.attack(playerAttLvl) << " damage";
playerHealthLoss = drDegreaser.defend(playerDef);
cout << "\nDr Degreaser attacks you back, dealing " << playerHealthLoss << " damage";
playerHealth -= playerHealthLoss;
if (playerHealth < 0) playerHealth = 0;
cout << "\n\nDr Degreaser has " << drDegreaser.getHealth() << " health left";
cout << "\nYou have " << playerHealth << " health left";
if (!playerHealth)
{
cout << "\n\nYou Lose!\n\n";
gameOver(playerGold, killCount);
Sleep (sleepTime*2);
return 0;
}
if (drDegreaser.getHealth())
{
do
{
cout << "\n\n\nWould you like to attack again?\n[type YES to continue attacking, or RUN to run away] >>> ";
cin >> attackAgain;
for (unsigned int a = 0; a <= attackAgain.length(); a++) attackAgain[a] = toupper(attackAgain[a]);
} while (attackAgain != "YES" && attackAgain != "RUN");
}
for (unsigned int a = 0; a <= attackAgain.length(); a++) attackAgain[a] = toupper(attackAgain[a]);
if (attackAgain == "RUN")
{
attackAgain = "YES";
break;
}
if (!drDegreaser.getHealth())
{
cout << "\n\n\t\tYou killed Dr Degreaser gaining " << drDegreaser.giveGold() << " gold";
playerGold += drDegreaser.giveGold();
killCount++;
drDegreaser.takeLife();
drDegreaser.setHealth(100);
Sleep(sleepTime);
}
}
break;
}
case 2:
{
while(marleyMop.getIsAlive() && playerHealth > 0)
{
system("cls");
cout << "You attack Marley Mop with " << strWeapon << " and deal " << marleyMop.attack(playerAttLvl) << " damage";
playerHealthLoss = marleyMop.defend(playerDef);
cout << "\nMarley Mop attacks you back, dealing " << playerHealthLoss << " damage";
playerHealth -= playerHealthLoss;
if (playerHealth < 0) playerHealth = 0;
cout << "\n\nMarley Mop has " << marleyMop.getHealth() << " health left";
cout << "\nYou have " << playerHealth << " health left";
if (!playerHealth)
{
cout << "\n\nYou Lose!\n\n";
gameOver(playerGold, killCount);
Sleep (sleepTime*2);
return 0;
}
if (marleyMop.getHealth())
{
do
{
cout << "\n\n\nWould you like to attack again?\n[type YES to continue attacking, or RUN to run away] >>> ";
cin >> attackAgain;
for (unsigned int a = 0; a <= attackAgain.length(); a++) attackAgain[a] = toupper(attackAgain[a]);
}while (attackAgain != "YES" && attackAgain != "RUN");
}
for (unsigned int a = 0; a <=3; a++) attackAgain[a] = toupper(attackAgain[a]);
if (attackAgain == "RUN")
{
attackAgain = "YES";
break;
}
if (!marleyMop.getHealth())
{
cout << "\nYou killed Marley Mop gaining " << marleyMop.giveGold() << " gold";
playerGold += marleyMop.giveGold();
killCount++;
marleyMop.takeLife();
marleyMop.setHealth(200);
Sleep(sleepTime);
}
}
break;
}
case 3:
{
while(ninjaGlove.getIsAlive() && playerHealth > 0 )
{
system("cls");
cout << "You attack Ninja Glove with " << strWeapon << " and deal " << ninjaGlove.attack(playerAttLvl) << " damage";
playerHealthLoss = ninjaGlove.defend(playerDef);
cout << "\nNinja Glove attacks you back, dealing " << playerHealthLoss << " damage";
playerHealth -= playerHealthLoss;
if (playerHealth < 0) playerHealth = 0;
cout << "\n\nNinja Glove has " <<ninjaGlove.getHealth() << " health left";
cout << "\nYou have " << playerHealth << " health left";
if (!playerHealth)
{
cout << "\n\nYou Lose!\n\n";
gameOver(playerGold, killCount);
Sleep (sleepTime*2);
return 0;
}
if (ninjaGlove.getHealth())
{
do
{
cout << "\n\n\nWould you like to attack again?\n[type YES to continue attacking, or RUN to run away] >>> ";
cin >> attackAgain;
for (unsigned int a = 0; a <= attackAgain.length(); a++) attackAgain[a] = toupper(attackAgain[a]);
} while (attackAgain != "YES" && attackAgain != "RUN");
}
for (unsigned int a = 0; a <= attackAgain.length(); a++) attackAgain[a] = toupper(attackAgain[a]);
if (attackAgain == "RUN")
{
attackAgain = "YES";
break;
}
if(!ninjaGlove.getHealth())
{
cout << "\nYou killed Ninja Glove gaining " << ninjaGlove.giveGold() << " gold";
playerGold += ninjaGlove.giveGold();
killCount++;
ninjaGlove.takeLife();
ninjaGlove.setHealth(300);
Sleep(sleepTime);
}
}
break;
}
//shopping
case 11: // buy pea shooter
if(playerGold >= priceofPeaShooter && playerGold != 0)
{
hasPeaShooter = true;
playerGold -= priceofPeaShooter;
playerAttLvl +=10;
break;
}
else
{
noGold();
break;
}
case 12://buy sling shot
if(playerGold >= priceofSlingShot && playerGold != 0)
{
hasSlingShot = true;
playerGold -= priceofPeaShooter;
playerAttLvl +=20;
break;
}
else
{
noGold();
break;
}
case 13:// buy nail gun
if (playerGold >= priceofNailGun && playerGold != 0)
{
hasNailGun = true;
playerGold -= priceofNailGun;
playerAttLvl += 30;
break;
}
else
{
noGold();
break;
}
case 14:// buy health restore
if (playerGold >= priceofFullHealth)
{
playerHealth = 200;
playerGold -= priceofFullHealth;
break;
}
else
{
noGold();
break;
}
case 15: break;
//rest
case 21://one day rest
if (playerGold >= priceof1DayRest)
{
playerHealth+=10;
playerGold-=priceof1DayRest;
cout << "You regain 10 health";
typerPrint(500, "..........");
break;
}
else
{
noGold();
break;
}
case 22://two days rest
if (playerGold >= priceof2DayRest)
{
playerHealth+=30;
playerGold-=priceof2DayRest;
cout << "You regain 30 health";
typerPrint(500, "..........");
break;
}
else
{
noGold();
break;
}
case 23://one week
if (playerGold >= priceof1WeekRest)
{
playerHealth+=50;
playerGold-=priceof1WeekRest;
cout << "You regain 50 health";
typerPrint(500, "..........");
break;
}
else
{
noGold();
break;
}
}//end of Main switch statement
}// end of game loop
char f;
cin >> f;
}
void welcomeDisplay()
{
system("cls");
}
int toShop(int priceofPeaShooter, int priceofSlingShot, int priceofNailGun, int priceofFullHealth)
{
char shopChoice;
do
{
system("cls");
cout << "What would you like to buy?\n\n1.A Pea Shooter\t("<<priceofPeaShooter<<" gold)\n2.A Sling Shot\t("<<priceofSlingShot<<" gold)";
cout << "\n3.A Nailgun\t("<<priceofNailGun<<" gold)";
cout << "\n4.A full health refil\t("<<priceofFullHealth<<" gold)\n\n5.Nothing >>> ";
cin >> shopChoice;
} while (shopChoice != '1' && shopChoice != '2' && shopChoice != '3' && shopChoice != '4' && shopChoice != '5');
switch (shopChoice)
{
case '1': return 11;
case '2': return 12;
case '3': return 13;
case '4': return 14;
case '5': return 15;
}
return 0;
}
int toFight()
{
char fightWho;
do{
system("cls");
cout << "Who will you fight?\n\n1.Dr Degreaser\n2.Marley Mop\n3.Ninja Glove >>> ";
cin >> fightWho;
} while (fightWho != '1' && fightWho != '2' && fightWho != '3');
switch (fightWho)
{
case '1': return 1;
case '2': return 2;
case '3': return 3;
}
return 0;
}
int toRest(int priceof1DayRest, int priceof2DayRest, int priceof1WeekRest)
{
char howLong;
do
{
system("cls");
cout << "How long would you like to rest for?\n\n1.\t1day (heals 10 health, costs "<<priceof1DayRest<<" gold)\n\n2.\t2days (heals 30 health, costs "<<priceof2DayRest<<" gold)";
cout << "\n\n3.\t1 week (heals 50 health, costs "<<priceof1WeekRest<<" gold)\n\n4.No rest (go back)";
cin >> howLong;
}
while (howLong != '1' && howLong != '2' && howLong != '3' && howLong != '4');
switch (howLong)
{
case '1': return 21;
case '2': return 22;
case '3': return 23;
case '4': return 15;
}
return 0;
}
void typerPrint(int time, string toPrint)
{
for (unsigned int a = 0; a < toPrint.length(); a++)
{
cout << toPrint[a];
Sleep(time);
}
}
void gameOver(int gold, int kills)
{
typerPrint(500, "Game Over");
cout << "\n\nYou finished with " << gold << " gold";
cout << "\nand killed " << kills << " enemies";
}
void noGold()
{
cout << "\n\n\t\tYou dont have enough gold";
Sleep(1000);
}
void printDisplay(int gold, int health, int playerDef, int playerAtt, string strWeapon, string playerName)
{
for (int a=0; a<20; a++) cout << "\\**/";
cout << "Name: " << playerName << setw(60) << right << "Health: " << health;
cout << "\nWeapon: " << strWeapon << setw(50) << right << "Attack lvl: " << playerAtt;
cout << "\nGold: " << gold << setw(65) << right << "Defence lvl: " << playerDef << "\n";
for (int a=0; a<20; a++) cout << "\\**/";
}
version 1.2
