Created
November 30, 2014 05:53
-
-
Save 1995eaton/f98cd7763c0a7451b384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <time.h> | |
using namespace std; | |
/***************************** Person Class **********************************/ | |
class Person | |
{ | |
// private attributes of a person object | |
private: | |
int health, strength; | |
// public functions | |
public: | |
// default constructor | |
Person() | |
{ | |
health = 100; | |
strength = 10; | |
} | |
// constructor using health and strength inputs | |
Person(int h, int s) | |
{ | |
setHealth(h); | |
setStrength(s); | |
} | |
// set the health of the person making sure health is valid | |
void setHealth(int h) | |
{ | |
if(h < 0) | |
{ | |
cerr << "Invalid Health value"; | |
h = 0; // if invalid, health is zero | |
} | |
health = h; // set health to h | |
} | |
// set the strength of the person making sure strength is valid | |
void setStrength(int s) | |
{ | |
if(s < 0) | |
{ | |
cerr << "Invalid Strength value"; | |
s = 0; // if invalid, strength is zero | |
} | |
strength = s; // set strength to s | |
} | |
/********************************** getters *********************************/ | |
int getHealth(){ return health; } | |
int getStrength() { return strength; } | |
// getType shouldn't be called from the base Person class | |
virtual string getType() = 0; | |
// attack function based on Person's strength | |
virtual int attack(){ return rand() % strength; } | |
}; | |
/***************************** Faculty Class **********************************/ | |
class Faculty : public Person | |
{ | |
public: | |
// Default constructor calling parent class default | |
Faculty() : Person() { /*left blank intentionally*/ } | |
// Argumented constructor calling parent class argumented constructor | |
Faculty(int h, int s) : Person (h, s){ /*left blank intentionally*/ } | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType() { return "Faculty"; } | |
// Child's attack function. It's virtual to all the child to call its | |
// own function and not the parent's attack function | |
virtual int attack() | |
{ | |
int d; // d is for damage | |
d = Person::attack(); // d is assigned the parent's attack value | |
d = int(d * 1.5); // and then 50% is added because apparently | |
// faculty is 50% stronger than a regular | |
// person | |
return d; // return the final damage | |
} | |
}; | |
/***************************** Student Class **********************************/ | |
class Student : public Person | |
{ | |
public: | |
// Default constructor calling parent class default | |
Student() : Person() { /*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
Student(int h, int s) : Person(h, s){ /*left blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Student"; } | |
// Child's attack function. It's virtual to all the child to call its | |
// own function and not the parent's attack function | |
virtual int attack() | |
{ | |
int d; // d is for damage | |
d = Person::attack(); // d is assigned the parent's attack value | |
d = int(d * .75); // and then the damage is reduced by 25% | |
// because students don't sleep and they | |
// are 25% weaker than a regular person | |
return d; // return the final damage | |
} | |
}; | |
/*********************** Chemistry Faculty Class *****************************/ | |
class ChemistryFaculty : public Faculty | |
{ | |
public: | |
// Default constructor calling parent class default | |
ChemistryFaculty() : Faculty(){ cout << "Chemistry Faculty Created " << endl; /*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
ChemistryFaculty(int h, int s) : Faculty(h, s){/*blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Chemistry Faculty"; } | |
// special defense move evaporate snow ball. reduce damage to zero. | |
void evaporateSnowBall(){} | |
}; | |
/*********************** Chemistry Student Class *****************************/ | |
class ChemistryStudent : public Student | |
{ | |
public: | |
// Default constructor calling parent class default | |
ChemistryStudent() : Student(){ /*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
ChemistryStudent(int h, int s) : Student(h, s){/*blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Chemistry Student"; } | |
// special attack move informally called phase change. The snowball | |
// is hardened and the damage is increase by 50%-100% | |
virtual int attack() | |
{ | |
int d; // d is for damage | |
double multiplierOffset; // random offset multiplier for special | |
// attack | |
d = Person::attack(); // call the parent's attack function | |
multiplierOffset = ((rand() % 50) + 50) / 100; // get random | |
// multiplier | |
d = int(d * multiplierOffset); // multiply the damage by the | |
// random multiplier | |
cout << "Chemistry student attacked " << endl; | |
return d; // return the final value | |
} | |
}; | |
/*********************** Psychology Faculty Class *****************************/ | |
class PsychologyFaculty : public Faculty | |
{ | |
public: | |
// Default constructor calling parent class default | |
PsychologyFaculty() : Faculty(){/*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
PsychologyFaculty(int h, int s) : Faculty(h,s){/*blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Psychology Faculty"; } | |
// special defense to talk opponent out of throwing snowball | |
void letsTalkAboutIt(){} | |
}; | |
/*********************** Psychology Student Class *****************************/ | |
class PsychologyStudent : public Student | |
{ | |
public: | |
// Default constructor calling parent class default | |
PsychologyStudent() : Student(){/*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
PsychologyStudent(int h, int s) : Student(h,s){/*intentionally blank*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Psychology Student"; } | |
// special attack talks opponent into throwing snowball at himself while | |
// also throwing his own snowball. Why are you hitting yourself? | |
virtual int attack() | |
{ | |
return 10; | |
} | |
}; | |
/************************* Music Faculty Class *******************************/ | |
class MusicFaculty : public Faculty | |
{ | |
public: | |
// Default constructor calling parent class default | |
MusicFaculty() : Faculty(){/*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
MusicFaculty(int h, int s) : Faculty(h,s){/*blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Music Faculty"; } | |
// increases the defense by creating an impenetrable wall of sound | |
void wallOfSound(){} | |
}; | |
/************************* Music Student Class *******************************/ | |
class MusicStudent : public Student | |
{ | |
public: | |
// Default constructor calling parent class default | |
MusicStudent() : Student(){/*left blank intentionally*/} | |
// Argumented constructor calling parent class argumented constructor | |
MusicStudent(int h, int s) : Student(h,s){/*blank intentionally*/} | |
// getType returns the type of this class that's calling it. | |
// it's virtual to allow for polymorphism. We're interested in the | |
// child's type not the parent | |
virtual string getType(){ return "Music Student"; } | |
// special attack sonic fury. | |
// Student fills his instrument with multiple snowballs and fires | |
// them all by playing the instrument. | |
virtual int attack() | |
{ | |
int d = 0; | |
for(int i = 0; i < 10; i++) | |
{ | |
// add together 10 snowballs at 20% power | |
d += int(Person::attack() * .20); | |
} | |
return d; | |
} | |
}; | |
class SnowBallFight | |
{ | |
private: | |
const static int NUM_PLAYERS = 12; | |
Faculty teamFaculty[NUM_PLAYERS]; | |
Student teamStudents[NUM_PLAYERS]; | |
public: | |
SnowBallFight() | |
{ | |
} | |
void initTeams() | |
{ | |
const int GENERAL = 0; | |
const int CHEMISTRY = 1; | |
const int PSYCHOLOGY = 2; | |
const int MUSIC = 3; | |
int playerType; | |
for(int player = 0; player < NUM_PLAYERS; player++) | |
{ | |
// Init Faculty Team | |
playerType = rand() % 4; | |
switch(playerType) | |
{ | |
case GENERAL: | |
cout << "General Faculty" << endl; | |
teamFaculty[player] = Faculty(); | |
break; | |
case CHEMISTRY: | |
cout << "Chemistry Faculty" << endl; | |
teamFaculty[player] = ChemistryFaculty(); | |
break; | |
case PSYCHOLOGY: | |
cout << "Psychology Faculty" << endl; | |
teamFaculty[player] = PsychologyFaculty(); | |
break; | |
case MUSIC: | |
cout << "Music Faculty" << endl; | |
teamFaculty[player] = MusicFaculty(); | |
break; | |
default: break; | |
} | |
// Init Student Team | |
playerType = rand() % 4; | |
switch(playerType) | |
{ | |
case GENERAL: | |
cout << "General Student" << endl; | |
teamStudents[player] = Student(); | |
break; | |
case CHEMISTRY: | |
cout << "Chemistry Student" << endl; | |
teamStudents[player] = ChemistryStudent(); | |
break; | |
case PSYCHOLOGY: | |
cout << "Psychology Student" << endl; | |
teamStudents[player] = PsychologyStudent(); | |
break; | |
case MUSIC: | |
cout << "Music Student" << endl; | |
teamStudents[player] = MusicStudent(); | |
break; | |
default: break; | |
} | |
} | |
} | |
virtual void battle(Person& p1, Person& p2) | |
{ | |
cout << "Person 1 is: " << p1.getType() << endl; | |
cout << "Attack Value: " << p1.attack() << endl; | |
cout << "Person 2 is: " << p2.getType() << endl; | |
cout << "Attack Value: "<< p2.attack() << endl; | |
} | |
Person& getFaculty(int playerIndex) | |
{ | |
return teamFaculty[playerIndex]; | |
} | |
Person& getStudent(int playerIndex) | |
{ | |
return teamStudents[playerIndex]; | |
} | |
bool isGameOver() | |
{ | |
for(int i = 0; i < NUM_PLAYERS; i++) | |
{ | |
/* | |
if(fac[i].getHealth() < 100 || stu[i].getHealth() < 100) | |
{ | |
return false; | |
} | |
*/ | |
} | |
cout << "Game is Over" << endl; | |
return true; | |
} | |
}; | |
const static int NUM_PLAYERS = 12; | |
int main(); | |
void init(); | |
void initTeams(Person *fac, Person *stu); | |
void printStatus(); | |
bool isGameOver(Person *fac, Person *stu); | |
void battle(Person& p1, Person& p2); | |
/*********************************** main ************************************/ | |
int main() | |
{ | |
bool gameOver; | |
SnowBallFight currentFight; | |
/* | |
Person teamFaculty[NUM_PLAYERS]; | |
Person teamStudents[NUM_PLAYERS]; | |
*/ | |
// seed the random number generator | |
srand(time(NULL)); | |
// print out initalization stuff | |
init(); | |
currentFight.initTeams(); | |
gameOver = false; | |
// run game while there are elligble players | |
while(!gameOver) | |
{ | |
for(int player = 0; player < NUM_PLAYERS; player++) | |
{ | |
currentFight.battle(currentFight.getFaculty(player), | |
currentFight.getStudent(player)); | |
} | |
gameOver = currentFight.isGameOver(); | |
} | |
return 0; | |
} | |
/*********************************** Init ************************************/ | |
void init() | |
{ | |
cout << "\n\n====================================================" << endl; | |
cout << " Charles Davis - CPSC 246.01 - Snow Ball Fight " << endl; | |
cout << "====================================================\n\n" << endl; | |
} | |
/* | |
void initTeams(Person *fac, Person *stu) | |
{ | |
const int GENERAL = 0; | |
const int CHEMISTRY = 1; | |
const int PSYCHOLOGY = 2; | |
const int MUSIC = 3; | |
int playerType; | |
for(int player = 0; player < NUM_PLAYERS; player++) | |
{ | |
// Init Faculty Team | |
playerType = rand() % 4; | |
switch(playerType) | |
{ | |
case GENERAL: | |
cout << "General Faculty" << endl; | |
fac[player] = Faculty(); | |
break; | |
case CHEMISTRY: | |
cout << "Chemistry Faculty" << endl; | |
fac[player] = ChemistryFaculty(); | |
break; | |
case PSYCHOLOGY: | |
cout << "Psychology Faculty" << endl; | |
fac[player] = PsychologyFaculty(); | |
break; | |
case MUSIC: | |
cout << "Music Faculty" << endl; | |
fac[player] = MusicFaculty(); | |
break; | |
default: break; | |
} | |
// Init Student Team | |
playerType = rand() % 4; | |
switch(playerType) | |
{ | |
case GENERAL: | |
cout << "General Student" << endl; | |
stu[player] = Student(); | |
break; | |
case CHEMISTRY: | |
cout << "Chemistry Student" << endl; | |
stu[player] = ChemistryStudent(); | |
break; | |
case PSYCHOLOGY: | |
cout << "Psychology Student" << endl; | |
stu[player] = PsychologyStudent(); | |
break; | |
case MUSIC: | |
cout << "Music Student" << endl; | |
stu[player] = MusicStudent(); | |
break; | |
default: break; | |
} | |
} | |
} | |
*/ | |
/******************************** Print Status *******************************/ | |
void printStatus() | |
{ | |
} | |
/* | |
bool isGameOver(Person *fac, Person *stu) | |
{ | |
for(int i = 0; i < NUM_PLAYERS; i++) | |
{ | |
if(fac[i].getHealth() < 100 || stu[i].getHealth() < 100) | |
{ | |
return false; | |
} | |
} | |
cout << "Game is Over" << endl; | |
return true; | |
} | |
*/ | |
/* | |
void battle(Person& p1, Person& p2) | |
{ | |
cout << "Person 1 is: " << p1.getType() << endl; | |
cout << "Attack Value: " << p1.attack() << endl; | |
cout << "Person 2 is: " << p2.getType() << endl; | |
cout << "Attack Value: "<< p2.attack() << endl; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment