Language C Original Question httpwwwcheggcomhomeworkhelpques
Language: C++
Original Question: http://www.chegg.com/homework-help/questions-and-answers/project-using-c-create-simple-class-hierarchy-basis-fantasy-combat-game-universe-contains--q18290244
Looking for code to answer the original question linked that will fill in the missing code provided (pasted below): Vampire.h; Functions.cpp, Barbarian.h, , BlueMen.h, and Creature.cpp
main.cpp
#include \"Functions.h\"
#include \"Creature.h\" // base class
#include \"Vampire.h\"
#include \"Barbarian.h\"
#include \"BlueMen.h\"
#include \"Medusa.h\"
#include \"HarryPotter.h\"
#include <iostream>
#include <time.h> // for rand
#include <cstdlib> // used for the rand() function
using namespace std;
int main()
{
srand(time(0)); //seed the random number with clock time to get a different set of random numbers each time
int choice;
cout << \"\ \\t~*~ Fantasy Combat Game ~*~\" << endl;
// This is the main loop to display the menu and run the specific parts of the program based on user input
do {
showMenu(); // display menu to user
choice = getChoice(); // get the input choice of the user from the menu
cin.ignore();
switch (choice) {
case 1: // Case 1 is just an output of the game rules and description of each creature
showRules();
break;
case 2: // Case 2 is the actual game play where two players can select their creatures and attack each other
{
int player1, player2;
int damage;
Creature *creature1;
Creature *creature2;
player1 = getCreature1();
player2 = getCreature2();
// create the creature for player 1
if (player1 == 1){
creature1 = new Vampire;
//cout << \"creature1 is now a Vampire\" << endl;
}else if (player1 == 2){
creature1 = new Barbarian;
//cout << \"creature1 is now a Barbarian\" << endl;
}else if (player1 == 3) {
creature1 = new BlueMen;
//cout << \"creature1 is now Blue Men\" << endl;
}else if (player1 == 4) {
creature1 = new Medusa;
//cout << \"creature1 is now a Medusa\" << endl;
}else if (player1 == 5) {
creature1 = new HarryPotter;
//cout << \"creature1 is now Harry Potter\" << endl;
}
// create the creature for player 2
if (player2 == 1) {
creature2 = new Vampire;
//cout << \"creature2 is now a Vampire\" << endl;
}else if (player2 == 2){
creature2 = new Barbarian;
//cout << \"creature2 is now a Barbarian\" << endl;
}else if (player2 == 3) {
creature2 = new BlueMen;
//cout << \"creature2 is now Blue Men\" << endl;
}else if (player2 == 4) {
creature2 = new Medusa;
//cout << \"creature2 is now a Medusa\" << endl;
}else if (player2 == 5) {
creature2 = new HarryPotter;
//cout << \"creature2 is now Harry Potter\" << endl;
}
// initialize the data members for the creatures...could have used constructor instead? These functions do the same thing...
creature1->setData();
creature2->setData();
// confirm each players creature
cout << \"Player 1 is: \" << creature1->getName() << endl;
cout << \"Starting armor: \" << creature1->getArmor() << endl;
cout << \"Starting strength: \" << creature1->getStrength() << endl;
cout << endl;
cout << \"Player 2 is: \" << creature2->getName() << endl;
cout << \"Starting armor: \" << creature2->getArmor() << endl;
cout << \"Starting strength: \" << creature2->getStrength() << endl;
//Code here to Randomly pick and display which player will go first
int x = rand() % 2 + 1;
if (x == 1)
cout << \"\ Player 1 was randomly selected to start first!\" << endl;
else
cout << \"\ Player 2 was randomly selected to start first!\" << endl;
// hit enter to continue
cout << \"\ Press ENTER to begin the attacks...\";
cin.ignore();
cin.get();
cin.sync();
// clear the screen
//cout <<\"\\033[2J\\033[1;1H\"; // clear the screen FLIP
system(\"CLS\"); // clear the screen WINDOWS
// bool variables used to determine if each creature died or not
bool p1Dead = false;
bool p2Dead = false;
// PLAYER 1 GOES FIRST - main loop to simulate the attacks and defense
if (x ==1){
while (p1Dead == false && p2Dead == false){
cout << \"Player 1 attack:\" << endl;
damage = creature1->attack(); // creature1 attack
cout << \"\ Player 2 defense:\" << endl;
creature2->defense(damage); // creature2 defend
p2Dead = creature2->isDead(); // check to see if creature2 died
if (p2Dead == false){
cout << \"\ Player 2 attack:\" << endl;
damage = creature2->attack(); // creature2 attack
cout << \"\ Player 1 defense:\" << endl;
creature1->defense(damage); // creature1 defend
cout << endl;
}
p1Dead = creature1->isDead(); // check to see if creature1 died
}
}else{ // PLAYER 2 GOES First
while (p1Dead == false && p2Dead == false){
cout << \"Player 2 attack:\" << endl;
damage = creature2->attack(); // creature 2 attack
cout << \"\ Player 1 defense:\" << endl;
creature1->defense(damage); // creature 1 defend
p1Dead = creature1->isDead(); // check to see if creature1 died
if (p1Dead == false){
cout << \"\ Player 1 attack:\" << endl;
damage = creature1->attack(); // creature2 attack
cout << \"\ Player 2 defense:\" << endl;
creature2->defense(damage); // creature1 defend
cout << endl;
}
p2Dead = creature2->isDead(); // check to see if creature1 died
}
}
// Game over, display winner/loser
if (p1Dead == true){
cout << \"\ *** Battle over - Player 2 Wins with \" << creature2->getName() << \"!!! ***\" << endl;
}else if(p2Dead == true){
cout << \"\ *** Battle over - Player 1 Wins with \" << creature1->getName() << \"!!! ***\" << endl;
}
// hit enter to return to main menu
cout << \"\ Press ENTER to return to the main menu...\";
//cin.ignore();
cin.get();
// clear the screen
//cout <<\"\\033[2J\\033[1;1H\"; // clear the screen FLIP
system(\"CLS\"); // clear the screen WINDOWS
// deallocate memory
delete creature1;
delete creature2;
}
break;
case 3: // Case 3 is a testing suite that the user can automatically run test attacks with each creature match up
showTestMenu(); // show testing menu
int tChoice;
tChoice = getTestChoice(); // get choice of testing menu
cin.clear();
cin.ignore(1000, \'\ \');
// if statements here to run each test possibility
runTests(tChoice);
cout << \"\ Press ENTER to return to the main menu...\";
cin.get();
// clear the screen
//cout <<\"\\033[2J\\033[1;1H\"; // clear the screen FLIP
system(\"CLS\"); // clear the screen WINDOWS
break;
case 4: // Case 4 just exits the program
break;
}
} while (choice != 4); // if user enters 4 in the main menu then the program exits
cout << \"\ Exiting Fantasy Combat Game, Goodbye! \" << endl;
return 0;
}
Barbarian.cpp
#include \"Barbarian.h\"
void Barbarian::setData(){
name = \"Barbarian\";
armor = 0;
strength = 12;
dead = false;
}
int Barbarian::attack(){
int damage = attackRoll();
cout << \"Barbarian attacks! Barbarian generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void Barbarian::defense(int d){
int defense = defenseRoll();
int damage;
if (d != 999){
cout << \"Barbarian attempts to defend! Barbarian generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0){
cout << \"Barbarian defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == 999){
cout << \"Barbarian Dies! Barbarian looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true;
}else if (damage > armor){
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0){
cout << \"Barbarian gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if(strength <= 0){
cout << \"Barbarian Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // barbarian dies!
}
}
}
string Barbarian::getName(){
return name;
}
bool Barbarian::isDead(){
return dead;
}
int Barbarian::attackRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Barbarian::defenseRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Barbarian::getArmor(){
return armor;
}
int Barbarian::getStrength(){
return strength;
}
Barbarian.h
BlueMen.cpp
#include \"BlueMen.h\"
void BlueMen::setData(){
name = \"Blue Men\";
armor = 3;
strength = 12;
dead = false;
loseDie = 0; // SPECIAL ABLITY
}
int BlueMen::attack(){
int damage = attackRoll();
cout << \"Blue Men attack! Blue Men generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void BlueMen::defense(int d){
int defense = defenseRoll();
int damage;
if (d != 999){
cout << \"Blue Men attempt to defend! Blue Men generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0){
cout << \"Blue Men defend! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage > 0 && damage <= armor){
armor = armor - damage;
cout << \"Blue Men\'s armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == 999){
cout << \"Blue Men Die! Blue Men all looked into Medusa\'s eyes and all turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true; // vampire dies!
}else if (damage > armor){
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0){
cout << \"Blue Men get hurt but survive! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if(strength <= 0){
cout << \"Blue Men Die! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
// SPECIAL ABILITY - Mob - for every 4 points of damage they lose one defense die
if (strength <= 8 && strength > 4)
loseDie = 1; // lose 1 die
else if (strength <= 4 && strength > 0)
loseDie = 2; // lose 2 die
}
string BlueMen::getName(){
return name;
}
bool BlueMen::isDead(){
return dead;
}
int BlueMen::attackRoll(){
int roll = rand() % 10 + 1; // generate random number within 10
roll = roll + (rand() % 10 + 1); // 2nd dice roll, add together
return roll;
}
int BlueMen::defenseRoll(){
int roll;
if (loseDie == 0){
roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
roll = roll + (rand() % 6 + 1); // 3rd dice roll, add together
}else if (loseDie == 1){
roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
}else if (loseDie == 2){
roll = rand() % 6 + 1; // generate random number within 6
}
return roll;
}
int BlueMen::getArmor(){
return armor;
}
int BlueMen::getStrength(){
return strength;
}
BlueMen.h
Creature.cpp
#include \"Creature.h\"
Creature.h
#ifndef CREATURE_H
#define CREATURE_H
#include <iostream>
#include <string>
#include <time.h> // for rand
#include <cstdlib> // used for the rand() function
using namespace std;
class Creature
{
protected:
string name;
int armor;
int strength;
bool dead;
public:
// all virtual functions
virtual void setData()=0;
virtual int attack()=0;
virtual void defense(int d)=0;
virtual string getName()=0;
virtual bool isDead()=0;
virtual int attackRoll()=0;
virtual int defenseRoll()=0;
virtual int getArmor()=0;
virtual int getStrength()=0;
virtual ~Creature() {}
};
#endif // CREATURE_H
Functions.h
#include \"Creature.h\" // abstract base class
#include \"Vampire.h\"
#include \"Barbarian.h\"
#include \"BlueMen.h\"
#include \"Medusa.h\"
#include \"HarryPotter.h\"
#include <iostream>
#include <time.h> // for rand
#include <cstdlib> //Delete when compiling on FLIP? This is used for clear screen in windows
using namespace std;
void showMenu(); // This function prints the opening menu to user
int getChoice(); // This function gets the input choice of the user from the menu
void showRules(); // This function displays the game rules and the descriptions of the creatures
int getCreature1(); // This function prompts user on which creature to use for player 1
int getCreature2(); // This function prompts user on which creature to use for player 2
void showTestMenu(); // This function prints out the menu for testing option
int getTestChoice(); // This function gets and verifies input for the testing suite menu
void runTests(int t); // This function runs test fights between characters
HarryPotter.cpp
#include \"HarryPotter.h\"
void HarryPotter::setData(){
name = \"Harry Potter\";
armor = 0;
strength = 10;
dead = false;
hogwarts = 1; // hogwarts variable used for SPECIAL ABILITY
}
int HarryPotter::attack(){
int damage = attackRoll(); // call attackRoll function
cout << \"Harry Potter attacks! Harry Potter generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void HarryPotter::defense(int d){
int defense = defenseRoll(); // call defense roll function
int damage;
if (d != 999){
cout << \"Harry Potter attempts to defend! Harry Potter generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0){
cout << \"Harry Potter defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == 999 && hogwarts == 1){ // SPECIAL ABILITY
cout << \"Harry Potter Dies! Harry Potter looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"~Hogwarts~ Harry Potter immediately recovered and now has 20 strength points!\" << endl;
hogwarts = 0;
strength = 20;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
}else if (damage == 999 && hogwarts == 0){
cout << \"Harry Potter Dies! Harry Potter looked into Medusa\'s eyes and turned to stone!\" << endl;
strength = 0;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // Harry dies!
}else if (damage > armor){
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0){
cout << \"Harry Potter gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if(strength <= 0 && hogwarts == 1){ // SPECIAL ABILITY
cout << \"Harry Dies! Defense not strong enough!\" << endl;
cout << \"~Hogwarts~ Harry Potter immediately recovered and now has 20 strength points!\" << endl;
hogwarts = 0;
strength = 20;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
}else if(strength <= 0 && hogwarts == 0){
cout << \"Harry Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // Harry dies!
}
}
}
string HarryPotter::getName(){
return name;
}
bool HarryPotter::isDead(){
return dead;
}
int HarryPotter::attackRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int HarryPotter::defenseRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int HarryPotter::getArmor(){
return armor;
}
int HarryPotter::getStrength(){
return strength;
}
HarryPotter.h
#ifndef HARRYPOTTER_H
#define HARRYPOTTER_H
#include \"Creature.h\"
class HarryPotter : public Creature
{
protected:
int hogwarts; // for SPECIAL ABILITY
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~HarryPotter() {}
};
#endif // HARRYPOTTER_H
Medusa.cpp
#include \"Medusa.h\"
void Medusa::setData(){
name = \"Medusa\";
armor = 3;
strength = 8;
dead = false;
}
int Medusa::attack(){
int damage = attackRoll();
if (damage == 12){ // SPECIAL ABLITY
cout << \"Medusa rolled a 12! Opponent has looked in Medusa\'s eyes and turned to stone!\" << endl;
damage = 999;
}else if(damage != 12){
cout << \"Medusa attacks! Medusa generated \" << damage << \" damage points to opponent...\" << endl;
}
return damage;
}
void Medusa::defense(int d){
int defense = defenseRoll();
int damage;
if (d != 999){
cout << \"Medusa attempts to defend! Medusa generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0){
cout << \"Medusa defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage > 0 && damage <= armor){
armor = armor - damage;
cout << \"Medusa\'s armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == 999){
cout << \"Medusa Dies! Medusa looked into other Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true;
}else if (damage > armor){
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0){
cout << \"Medusa gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if(strength <= 0){
cout << \"Medusa Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
}
string Medusa::getName(){
return name;
}
bool Medusa::isDead(){
return dead;
}
int Medusa::attackRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Medusa::defenseRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
return roll;
}
int Medusa::getArmor(){
return armor;
}
int Medusa::getStrength(){
return strength;
}
Medusa.h
#ifndef MEDUSA_H
#define MEDUSA_H
#include \"Creature.h\"
class Medusa : public Creature
{
protected:
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~Medusa() {}
};
#endif // MEDUSA_H
Vampire.cpp
#include \"Vampire.h\"
void Vampire::setData(){
name = \"Vampire\";
armor = 1;
strength = 18;
dead = false;
}
int Vampire::attack(){
int damage = attackRoll();
cout << \"Vampire attacks! Vampire generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void Vampire::defense(int d){
int charm = rand() % 2 + 1; // determine if vampire charmed opponent into not attacking, 50% chance opponent does not attack
if (charm == 1){ // SPECIAL ABILITY
cout << \"Vampire charmed opponent into not attacking! Vampire takes no damage.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else{
int defense = defenseRoll();
int damage;
if (d != 999){
cout << \"Vampire attempts to defend! Vampire generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0){
cout << \"Vampire defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == armor){
armor = armor - damage;
cout << \"Vampire armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if (damage == 999){
cout << \"Vampire Dies! Vampire looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true; // vampire dies!
}else if (damage > armor){
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0){
cout << \"Vampire gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}else if(strength <= 0){
cout << \"Vampire Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
}
}
string Vampire::getName(){
return name;
}
bool Vampire::isDead(){
return dead;
}
int Vampire::attackRoll(){
int roll = rand() % 12 + 1; // generate random number within 12
return roll;
}
int Vampire::defenseRoll(){
int roll = rand() % 6 + 1; // generate random number within 6
return roll;
}
int Vampire::getArmor(){
return armor;
}
int Vampire::getStrength(){
return strength;
}
Solution
Note: Due to characters limit, File Functions. cpp and Sample Outputs are available at the link:
http://www.chegg.com/homework-help/questions-and-answers/project-using-c-create-simple-class-hierarchy-basis-fantasy-combat-game-universe-contains--q18290244
main.cpp
***********************************************************
#include \"stdafx.h\"
#include \"Functions.h\"
// base class
#include \"Creature.h\"
#include \"Vampire.h\"
#include \"Barbarian.h\"
#include \"BlueMen.h\"
#include \"Medusa.h\"
#include \"HarryPotter.h\"
#include <iostream>
// for rand
#include <time.h>
// used for the rand() function
#include <cstdlib>
using namespace std;
int main()
{
/*seed the random number with clock time
to get a different set of random numbers
each time*/
srand(time(0));
int choice;
cout << \"\ \\t~*~ Fantasy Combat Game ~*~\" << endl;
/* This is the main loop to display the menu
and run the specific parts of the program
based on user input*/
do {
// display menu to user
showMenu();
// get the input choice of the user from the menu
choice = getChoice();
cin.ignore();
switch (choice)
{
// Case 1 is just an output of the game
//rules and description of each creature
case 1:
showRules();
break;
/* Case 2 is the actual game play where two
players can select their creatures and
attack each other*/
case 2:
{
int player1, player2;
int damage;
Creature *creature1=0;
Creature *creature2=0;
player1 = getCreature1();
player2 = getCreature2();
// create the creature for player 1
if (player1 == 1) {
creature1 = new Vampire;
//cout << \"creature1 is now a Vampire\"
//<< endl;
}
else if (player1 == 2) {
creature1 = new Barbarian;
//cout << \"creature1 is now a Barbarian\"
//<< endl;
}
else if (player1 == 3) {
creature1 = new BlueMen;
//cout << \"creature1 is now Blue Men\"
//<< endl;
}
else if (player1 == 4) {
creature1 = new Medusa;
//cout << \"creature1 is now a Medusa\"
//<< endl;
}
else if (player1 == 5) {
creature1 = new HarryPotter;
//cout << \"creature1 is now Harry Potter\"
//<< endl;
}
// create the creature for player 2
if (player2 == 1) {
creature2 = new Vampire;
//cout << \"creature2 is now a Vampire\"
//<< endl;
}
else if (player2 == 2) {
creature2 = new Barbarian;
//cout << \"creature2 is now a Barbarian\"
//<< endl;
}
else if (player2 == 3) {
creature2 = new BlueMen;
//cout << \"creature2 is now Blue Men\"
//<< endl;
}
else if (player2 == 4) {
creature2 = new Medusa;
//cout << \"creature2 is now a Medusa\"
//<< endl;
}
else if (player2 == 5) {
creature2 = new HarryPotter;
//cout << \"creature2 is now Harry Potter\"
//<< endl;
}
/*initialize the data members for
the creatures...could have used
constructor instead?
These functions do the same thing...*/
creature1->setData();
creature2->setData();
// confirm each players creature
cout << \"Player 1 is: \"
<< creature1->getName() << endl;
cout << \"Starting armor: \"
<< creature1->getArmor() << endl;
cout << \"Starting strength: \"
<< creature1->getStrength() << endl;
cout << endl;
cout << \"Player 2 is: \"
<< creature2->getName() << endl;
cout << \"Starting armor: \"
<< creature2->getArmor() << endl;
cout << \"Starting strength: \"
<< creature2->getStrength() << endl;
//Code here to Randomly pick and display which player will go first
int x = rand() % 2 + 1;
if (x == 1)
cout << \"\ Player 1 was randomly \"
<<\"selected to start first!\" << endl;
else
cout << \"\ Player 2 was randomly\"
<<\" selected to start first!\" << endl;
// hit enter to continue
cout << \"\ Press ENTER to begin the attacks...\";
cin.ignore();
cin.get();
cin.sync();
// clear the screen
// clear the screen FLIP
system(\"CLS\");
// clear the screen WINDOWS
// bool variables used to determine
//if each creature died or not
bool p1Dead = false;
bool p2Dead = false;
/* PLAYER 1 GOES FIRST - main loop to
simulate the attacks and defense*/
if (x == 1) {
while (p1Dead == false && p2Dead == false) {
cout << \"Player 1 attack:\" << endl;
damage = creature1->attack(); // creature1 attack
cout << \"\ Player 2 defense:\" << endl;
creature2->defense(damage); // creature2 defend
p2Dead = creature2->isDead(); // check to see if creature2 died
if (p2Dead == false) {
cout << \"\ Player 2 attack:\" << endl;
damage = creature2->attack(); // creature2 attack
cout << \"\ Player 1 defense:\" << endl;
creature1->defense(damage); // creature1 defend
cout << endl;
}
p1Dead = creature1->isDead(); // check to see if creature1 died
}
}
else { // PLAYER 2 GOES First
while (p1Dead == false && p2Dead == false) {
cout << \"Player 2 attack:\" << endl;
damage = creature2->attack(); // creature 2 attack
cout << \"\ Player 1 defense:\" << endl;
creature1->defense(damage); // creature 1 defend
p1Dead = creature1->isDead(); // check to see if creature1 died
if (p1Dead == false) {
cout << \"\ Player 1 attack:\" << endl;
damage = creature1->attack(); // creature2 attack
cout << \"\ Player 2 defense:\" << endl;
creature2->defense(damage); // creature1 defend
cout << endl;
}
p2Dead = creature2->isDead(); // check to see if creature1 died
}
}
// Game over, display winner/loser
if (p1Dead == true) {
cout << \"\ *** Battle over - Player 2 Wins with \" << creature2->getName() << \"!!! ***\" << endl;
}
else if (p2Dead == true) {
cout << \"\ *** Battle over - Player 1 Wins with \" << creature1->getName() << \"!!! ***\" << endl;
}
// hit enter to return to main menu
cout << \"\ Press ENTER to return to the main menu...\";
//cin.ignore();
cin.get();
// clear the screen
//cout <<\"\\033[2J\\033[1;1H\"; // clear the screen FLIP
system(\"CLS\"); // clear the screen WINDOWS
// deallocate memory
delete creature1;
delete creature2;
}
break;
case 3: // Case 3 is a testing suite that the user can automatically run test attacks with each creature match up
showTestMenu(); // show testing menu
int tChoice;
tChoice = getTestChoice(); // get choice of testing menu
cin.clear();
cin.ignore(1000, \'\ \');
// if statements here to run each test possibility
runTests(tChoice);
cout << \"\ Press ENTER to return to the main menu...\";
cin.get();
// clear the screen
//cout <<\"\\033[2J\\033[1;1H\"; // clear the screen FLIP
system(\"CLS\"); // clear the screen WINDOWS
break;
case 4: // Case 4 just exits the program
break;
}
} while (choice != 4); // if user enters 4 in the main menu then the program exits
cout << \"\ Exiting Fantasy Combat Game, Goodbye! \" << endl;
return 0;
}
Creature.h
#pragma once
#ifndef CREATURE_H
#define CREATURE_H
#include <iostream>
#include <string>
#include <time.h> // for rand
#include <cstdlib> // used for the rand() function
using namespace std;
class Creature
{
protected:
string name;
int armor;
int strength;
bool dead;
public:
// all virtual functions
virtual void setData() = 0;
virtual int attack() = 0;
virtual void defense(int d) = 0;
virtual string getName() = 0;
virtual bool isDead() = 0;
virtual int attackRoll() = 0;
virtual int defenseRoll() = 0;
virtual int getArmor() = 0;
virtual int getStrength() = 0;
virtual ~Creature() {}
};
#endif // CREATURE_H
Creature.cpp
#include \"stdafx.h\"
#include \"Creature.h\"
Barbarian.h
#pragma once
#ifndef BARBARIAN_H
#define BARBARIAN_H
#include \"Creature.h\"
class Barbarian : public Creature
{
protected:
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~Barbarian() {}
};
#endif // BARBARIAN_H
Barbarian.cpp
#include \"stdafx.h\"
#include \"Barbarian.h\"
void Barbarian::setData() {
name = \"Barbarian\";
armor = 0;
strength = 12;
dead = false;
}
int Barbarian::attack() {
int damage = attackRoll();
cout << \"Barbarian attacks! Barbarian generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void Barbarian::defense(int d) {
int defense = defenseRoll();
int damage;
if (d != 999) {
cout << \"Barbarian attempts to defend! Barbarian generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0) {
cout << \"Barbarian defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == 999) {
cout << \"Barbarian Dies! Barbarian looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true;
}
else if (damage > armor) {
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0) {
cout << \"Barbarian gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (strength <= 0) {
cout << \"Barbarian Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // barbarian dies!
}
}
}
string Barbarian::getName() {
return name;
}
bool Barbarian::isDead() {
return dead;
}
int Barbarian::attackRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Barbarian::defenseRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Barbarian::getArmor() {
return armor;
}
int Barbarian::getStrength() {
return strength;
}
BlueMen.h
#pragma once
#ifndef BLUEMEN_H
#define BLUEMEN_H
#include \"Creature.h\"
class BlueMen : public Creature
{
protected:
int loseDie; // for SPECIAL ABILITY
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~BlueMen() {}
};
#endif // BLUEMEN_H
BlueMen.cpp
#include \"stdafx.h\"
#include \"BlueMen.h\"
void BlueMen::setData() {
name = \"Blue Men\";
armor = 3;
strength = 12;
dead = false;
loseDie = 0; // SPECIAL ABLITY
}
int BlueMen::attack() {
int damage = attackRoll();
cout << \"Blue Men attack! Blue Men generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void BlueMen::defense(int d) {
int defense = defenseRoll();
int damage;
if (d != 999) {
cout << \"Blue Men attempt to defend! Blue Men generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0) {
cout << \"Blue Men defend! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage > 0 && damage <= armor) {
armor = armor - damage;
cout << \"Blue Men\'s armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == 999) {
cout << \"Blue Men Die! Blue Men all looked into Medusa\'s eyes and all turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true; // vampire dies!
}
else if (damage > armor) {
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0) {
cout << \"Blue Men get hurt but survive! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (strength <= 0) {
cout << \"Blue Men Die! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
// SPECIAL ABILITY - Mob - for every 4 points of damage they lose one defense die
if (strength <= 8 && strength > 4)
loseDie = 1; // lose 1 die
else if (strength <= 4 && strength > 0)
loseDie = 2; // lose 2 die
}
string BlueMen::getName() {
return name;
}
bool BlueMen::isDead() {
return dead;
}
int BlueMen::attackRoll() {
int roll = rand() % 10 + 1; // generate random number within 10
roll = roll + (rand() % 10 + 1); // 2nd dice roll, add together
return roll;
}
int BlueMen::defenseRoll() {
int roll;
if (loseDie == 0) {
roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
roll = roll + (rand() % 6 + 1); // 3rd dice roll, add together
}
else if (loseDie == 1) {
roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
}
else if (loseDie == 2) {
roll = rand() % 6 + 1; // generate random number within 6
}
return roll;
}
int BlueMen::getArmor() {
return armor;
}
int BlueMen::getStrength() {
return strength;
}
HarryPotter.h
#pragma once
#ifndef HARRYPOTTER_H
#define HARRYPOTTER_H
#include \"Creature.h\"
class HarryPotter : public Creature
{
protected:
int hogwarts; // for SPECIAL ABILITY
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~HarryPotter() {}
};
#endif // HARRYPOTTER_H
HarryPotter.cpp
#include \"stdafx.h\"
#include \"HarryPotter.h\"
void HarryPotter::setData() {
name = \"Harry Potter\";
armor = 0;
strength = 10;
dead = false;
hogwarts = 1; // hogwarts variable used for SPECIAL ABILITY
}
int HarryPotter::attack() {
int damage = attackRoll(); // call attackRoll function
cout << \"Harry Potter attacks! Harry Potter generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void HarryPotter::defense(int d) {
int defense = defenseRoll(); // call defense roll function
int damage;
if (d != 999) {
cout << \"Harry Potter attempts to defend! Harry Potter generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0) {
cout << \"Harry Potter defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == 999 && hogwarts == 1) { // SPECIAL ABILITY
cout << \"Harry Potter Dies! Harry Potter looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"~Hogwarts~ Harry Potter immediately recovered and now has 20 strength points!\" << endl;
hogwarts = 0;
strength = 20;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
}
else if (damage == 999 && hogwarts == 0) {
cout << \"Harry Potter Dies! Harry Potter looked into Medusa\'s eyes and turned to stone!\" << endl;
strength = 0;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // Harry dies!
}
else if (damage > armor) {
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0) {
cout << \"Harry Potter gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (strength <= 0 && hogwarts == 1) { // SPECIAL ABILITY
cout << \"Harry Dies! Defense not strong enough!\" << endl;
cout << \"~Hogwarts~ Harry Potter immediately recovered and now has 20 strength points!\" << endl;
hogwarts = 0;
strength = 20;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
}
else if (strength <= 0 && hogwarts == 0) {
cout << \"Harry Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // Harry dies!
}
}
}
string HarryPotter::getName() {
return name;
}
bool HarryPotter::isDead() {
return dead;
}
int HarryPotter::attackRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int HarryPotter::defenseRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int HarryPotter::getArmor() {
return armor;
}
int HarryPotter::getStrength() {
return strength;
}
Medusa.h
#pragma once
#ifndef MEDUSA_H
#define MEDUSA_H
#include \"Creature.h\"
class Medusa : public Creature
{
protected:
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~Medusa() {}
};
#endif // MEDUSA_H
Medusa.cpp
#include \"stdafx.h\"
#include \"Medusa.h\"
void Medusa::setData() {
name = \"Medusa\";
armor = 3;
strength = 8;
dead = false;
}
int Medusa::attack() {
int damage = attackRoll();
if (damage == 12) { // SPECIAL ABLITY
cout << \"Medusa rolled a 12! Opponent has looked in Medusa\'s eyes and turned to stone!\" << endl;
damage = 999;
}
else if (damage != 12) {
cout << \"Medusa attacks! Medusa generated \" << damage << \" damage points to opponent...\" << endl;
}
return damage;
}
void Medusa::defense(int d) {
int defense = defenseRoll();
int damage;
if (d != 999) {
cout << \"Medusa attempts to defend! Medusa generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0) {
cout << \"Medusa defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage > 0 && damage <= armor) {
armor = armor - damage;
cout << \"Medusa\'s armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == 999) {
cout << \"Medusa Dies! Medusa looked into other Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true;
}
else if (damage > armor) {
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0) {
cout << \"Medusa gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (strength <= 0) {
cout << \"Medusa Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
}
string Medusa::getName() {
return name;
}
bool Medusa::isDead() {
return dead;
}
int Medusa::attackRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
roll = roll + (rand() % 6 + 1); // 2nd dice roll, add together
return roll;
}
int Medusa::defenseRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
return roll;
}
int Medusa::getArmor() {
return armor;
}
int Medusa::getStrength() {
return strength;
}
Vampire.h
#pragma once
#ifndef VAMPIRE_H
#define VAMPIRE_H
#include \"Creature.h\"
#include <time.h>
class Vampire : public Creature
{
protected:
public:
void setData();
int attack();
void defense(int d);
string getName();
bool isDead();
int attackRoll();
int defenseRoll();
int getArmor();
int getStrength();
~Vampire() {}
};
#endif // VAMPIRE_H
Vampire.cpp
// ConsoleApplication30.cpp : Defines the entry point for the console application.
//
#include \"stdafx.h\"
#include \"Vampire.h\"
void Vampire::setData() {
name = \"Vampire\";
armor = 1;
strength = 18;
dead = false;
}
int Vampire::attack() {
int damage = attackRoll();
cout << \"Vampire attacks! Vampire generated \" << damage << \" damage points to opponent...\" << endl;
return damage;
}
void Vampire::defense(int d) {
int charm = rand() % 2 + 1; // determine if vampire charmed opponent into not attacking, 50% chance opponent does not attack
if (charm == 1) { // SPECIAL ABILITY
cout << \"Vampire charmed opponent into not attacking! Vampire takes no damage.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else {
int defense = defenseRoll();
int damage;
if (d != 999) {
cout << \"Vampire attempts to defend! Vampire generated \" << defense << \" defense points. \" << endl;
damage = d - defense;
}
else
damage = d;
if (damage <= 0) {
cout << \"Vampire defends! No change in armor and strength.\" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == armor) {
armor = armor - damage;
cout << \"Vampire armor absorbed the attack! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (damage == 999) {
cout << \"Vampire Dies! Vampire looked into Medusa\'s eyes and turned to stone!\" << endl;
cout << \"Armor points: 0\" << endl;
cout << \"Strength points: 0\" << endl;
dead = true; // vampire dies!
}
else if (damage > armor) {
damage = damage - armor;
strength = strength - damage;
armor = 0;
if (strength > 0) {
cout << \"Vampire gets hurt but survives! \" << endl;
cout << \"Remaining armor points: \" << armor << endl;
cout << \"Remaining strength points: \" << strength << endl;
}
else if (strength <= 0) {
cout << \"Vampire Dies! Defense not strong enough!\" << endl;
cout << \"Armor points: \" << armor << endl;
cout << \"Strength points: \" << strength << endl;
dead = true; // vampire dies!
}
}
}
}
string Vampire::getName() {
return name;
}
bool Vampire::isDead() {
return dead;
}
int Vampire::attackRoll() {
int roll = rand() % 12 + 1; // generate random number within 12
return roll;
}
int Vampire::defenseRoll() {
int roll = rand() % 6 + 1; // generate random number within 6
return roll;
}
int Vampire::getArmor() {
return armor;
}
int Vampire::getStrength() {
return strength;
}
Functions.h
#pragma once
#pragma once
#include \"Creature.h\" // abstract base class
#include \"Vampire.h\"
#include \"Barbarian.h\"
#include \"BlueMen.h\"
#include \"Medusa.h\"
#include \"HarryPotter.h\"
#include <iostream>
#include <time.h> // for rand
#include <cstdlib> //Delete when compiling on FLIP? This is used for clear screen in windows
using namespace std;
void showMenu(); // This function prints the opening menu to user
int getChoice(); // This function gets the input choice of the user from the menu
void showRules(); // This function displays the game rules and the descriptions of the creatures
int getCreature1(); // This function prompts user on which creature to use for player 1
int getCreature2(); // This function prompts user on which creature to use for player 2
void showTestMenu(); // This function prints out the menu for testing option
int getTestChoice(); // This function gets and verifies input for the testing suite menu
void runTests(int t); // This function runs test fights between characters



























