Class Projects/Quizes - Without the Solutions Displayed
//Adventure Game Header File - Functions and Classes
#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
//Game Functions
int GenerateRandomNumber(int Number=6) {
int ResultRandom;
srand(time(NULL));
ResultRandom = (rand()%Number) + 1;
return ResultRandom;
}
void Attack(int *PlayerPoints) {
int Damage;
Damage = GenerateRandomNumber(10);
*PlayerPoints = *PlayerPoints - Damage;
cout << "Ouch! You loose " << Damage << " points.\n";
cout << "You now have " << *PlayerPoints << " points left!";
}
void SetTheScene(char PName[25]) {
// Takes char array (or "string") as an argument.
// Example of using a global value locally inside a function
int Setting;
Setting = GenerateRandomNumber();
switch (Setting) {
case 1 : cout << PName << ", you are in a swamp and sinking!\n";
break;
case 2 : cout << PName << ", you are on a high mountain peak.\n";
break;
case 3 : cout << PName << ", you are on a grassy plane.\n";
break;
case 4 : cout << PName << ", you are in the desert.\n";
break;
case 5 : cout << PName << ", you find yourself in a deserted village.\n";
break;
case 6 : cout << PName << ", you find yourself in a steamy jungle.\n";
break;
default : cout << "Something is definitely wrong here. Should be 1-6.";
} // close switch statement
} // close SetTheScene() function
//CLASSES
class Monster {
public:
//BAse CLASS - 3 Overloaded Constructors
Monster(int MonStrength = 10) {
Strength = MonStrength;
cout << "\nA monster has been created.";}
Monster(int MonStrength, int MonWeight) {
Strength = MonStrength;
Weight = MonWeight;
cout << "\nA monster has been created.";}
Monster(int MonStrength, int MonWeight, char MonName[10]) {
Strength = MonStrength;
Weight = MonWeight;
Name[10] = MonName[10];
cout << "\nA monster has been created.";}
~Monster() { cout << "\nThe monster has been destroyed!\n";}
//Accessor Methods
int getStrength() { return Strength; }
void setStrength(int MonsStrength) { Strength = MonsStrength; }
int getWeight() {return Weight;}
void setWeight(int Wt) {Weight = Wt;}
int getName() {return Name[10];}
void setName(char Nm[10]) {Name[10] = Nm[10];}
void Talk() {
int SayWhat;
SayWhat = GenerateRandomNumber(4);
switch(SayWhat) {
case 1 : cout << "I like flowers. Will you be my friend?";
break;
case 2 : cout << "I would really like to dismember you and eat you...";
break;
case 3 : cout << "Unfortunately, I have had a very bad day and"
<< " you happen to be the first creature I\'ve met"
<< " that I can take it out on...";
break;
case 4 : cout << "How do you taste? I don\'t think I\'ve eaten"
<< " your kind before...";
break;
default : cout << "Uh oh, this should never happen...";
} //closes switch
} //close talk function
protected:
int Strength;
int Weight;
char Name[10];
}; //closes class specification
class Giant : public Monster {
//Derived class of Monster
public:
Giant() {cout << "A giant is afoot...";}
~Giant() {cout << "Bye bye giant...";}
//Accesor Methods
int getFootsize() {return Footsize;}
void setFootSize(int GiantFeet) {Footsize = GiantFeet;}
//Other Functions
void GiantSpeak() {cout << "Fee Fi Fo Fum...";}
void Squash(int * Life) {
cout << "The giant squashes you like a bug!";
*Life = 0;}
private:
int Footsize;
}; //close class specification
class Troll : public Monster {
//Derived class of Monster
public:
Troll() {cout << "Somewhere a troll is in a bad mood...";}
~Troll() {cout << "Bye troll...";}
//Accesor Methods
int getDrool() {return Drool;}
void setDrool(int TrollDrool) {Drool = TrollDrool;}
//Other Functions
void GiantSpeak() {cout << "Ahhhhhhhhhhhhhh...";}
void Eat(int * Life) {
cout << "The dismembers you and boils your carcass in a stew.";
*Life = 0;}
void Drooling() {
setDrool(10);
cout << "\nThe troll salivates, dropping " << getDrool()
<< " gallons of drool onto your shiny new boots.\n";}
private:
int Drool;
}; //close class specification
class BarryManilow : public Monster {
//Derived class of Monster
public:
BarryManilow() {cout << "Run for your life - it\'s Barry!";}
~BarryManilow() {cout << "I write the songs that...";}
//Accesor Methods
int getSong() {return Song[20];}
void setSong(int BarrySong[20]) {Song[20] = BarrySong[20];}
//Other Functions
void BarrySing(int * Life) {
cout << "Barry lifts his voice to sing a ballad...";
cout << "\nYou feel the sudden urge to pull out your own\n"
<< "sword and kill yourself, right then and there.\n"
<< "Barry, for lack of an interested audience, \n"
<< "continues serenading your rotting corpse...\n";
*Life = 0;}
private:
int Song[20];
}; //close class specification
// main() Function .cpp File (Goes with previous Function/Class header file)
// Using srand(), rand(), class structure, while true, functions,
// switch statement, and nested if/else strictures, implementing variables and objects of
// global and local scope, Overriding default constructor and destructor, passing pointers.
#include "Stdafx.h"
#include "GameClasses.h"
void main() {
int ALIVE = 100;
int PlayerPoints = 20; //Global
char conflict;
char conflict2;
char Name[25];
cout << "What is your name, player? ";
cin >> Name;
cout << "Welcome to the game, " << Name << ".\n\n"; //global Name
cout << "You have " << PlayerPoints << " points allocated to you as you start the game.\n";
SetTheScene(Name);
Monster Ogre;
Monster Dragon(20);
while(ALIVE>0) {
Dragon.Talk();
cout << "\nYou encounter a dragon!\n";
cout << "He blasts you with fire! ";
Attack(&PlayerPoints);
cout << " He is of strength ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "\nYou pull out your sword and levy a heavy blow upon his head.\n";
Dragon.setStrength(5);
cout << "That was some move! The dragon is already half dead!\n";
cout << "He now only has the strength of ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "Now what do you choose to do?\n"
<< "r = run away\n"
<< "s = stay and fight\n"
<< "t = talk to the dragon\n";
cin >> conflict;
system("cls");
switch(conflict) {
case 'r' : cout << "You run away and survive!\n"
<< "The dragon inflicts merely a flesh wound.\n"
<< "You loose a mere 15 points. ";
ALIVE = ALIVE - 15;
cout << "You now have " << ALIVE << " points left.";
break;
case 's' : cout << "You stay and fight. You discover that\n"
<< "the dragon is merely a baby dragon. It\'s mother\n"
<< "then shows up, greatly enraged by your attempt to\n"
<< "kill her child. She is really big and really angry.\n";
Dragon.setStrength(200);
cout << "You are now fighting a dragon of strength: ";
cout << Dragon.getStrength() << ".\n";
cout << "The dragon rips off your arms and legs and\n"
<< "fries you to a crispy, crunchy, golden brown.\n";
ALIVE = 0;
break;
case 't' : cout << "\nYou strike up a conversation with the dragon.\n"
<< "You begin telling it your life story and all your woes.\n"
<< "Succumbing to fatal levels of nausea and boredom, the \n"
<< "dragon lapses into a coma and dies...\n";
Dragon.setStrength(0);
cout << "Dragon reduced to strength: ";
cout << Dragon.getStrength() << ".\n";
Dragon.~Dragon();
cout << "You have " << ALIVE << " points left.";
break;
default : cout << "Invalid input. Please press either: r, s, or w.";
} //close switch statement
while(ALIVE>0) {
cout << "\nYou continue traveling eastward. You see something in the distance...\n";
Ogre.Talk();
cout << "It is an ogre!\n";
cout << "What do you do now?\n";
cout << "r = run away\n"
<< "t = talk\n";
cin >> conflict2;
system("cls");
if(conflict2 == 'r') {
cout << "\nThe ogre chases you. You fall. He chops off your head and eats you.\n";
ALIVE = 0;
}
else if(conflict2 == 't') {
cout << "\nYou make friends. The ogre likes you and so kills you.\n";
ALIVE = 0;
}
} //close 2nd while true loop
} //close 1st while true loop - ALIVE no longer > 0
cout << "\nYou fought bravely but died. As the worms devour your flesh\n"
<< "and your soul descends into Hades you vow that you will one day\n"
<< "arise to take vengeance on your foes. Dare you try your luck\n"
<< "and suffer defeat once more? At least for now, the game is over....\n\n";
} //close main function
// AdventureGame3.cpp : Using srand(), rand(), class structure, while true, functions,
// switch statement, and nested if/else strictures, implementing variables and objects of
// global and local scope, Overriding default constructor and destructor, passing pointers.
#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
class Monster {
public:
Monster(int MonStrength = 10) {
Strength = MonStrength;
cout << "\nA monster has been created.";}
~Monster() { cout << "\nThe monster has been destroyed!\n";}
int getStrength() { return Strength; }
void setStrength(int MonsStrength) { Strength = MonsStrength; }
void Talk() { cout << "\nGrrr!! I am a monster... "; }
private:
int Strength;
}; //closes class specification
int GenerateRandomNumber(int Number=6) {
int ResultRandom;
srand(time(NULL));
ResultRandom = (rand()%Number) + 1;
/*
//For debugging purposes only
cout << "\nToday\'s lucky number is: "
<< ResultRandom << ".\n\n";
*/
return ResultRandom;
}
void Attack(int *PlayerPoints) {
int Damage;
Damage = GenerateRandomNumber(10);
*PlayerPoints = *PlayerPoints - Damage;
cout << "Ouch! You loose " << Damage << " points.\n";
cout << "You now have " << *PlayerPoints << " points left!";
} //close attack() function
void SetTheScene(char PName[25]) {
// Takes char array (or "string") as an argument.
// Example of using a global value locally inside a function
int Setting;
Setting = GenerateRandomNumber();
switch (Setting) {
case 1 : cout << PName << ", you are in a swamp and sinking!\n";
break;
case 2 : cout << PName << ", you are on a high mountain peak.\n";
break;
case 3 : cout << PName << ", you are on a grassy plane.\n";
break;
case 4 : cout << PName << ", you are in the desert.\n";
break;
case 5 : cout << PName << ", you find yourself in a deserted village.\n";
break;
case 6 : cout << PName << ", you find yourself in a steamy jungle.\n";
break;
default : cout << "Something is definitely wrong here. Should be 1-6.";
} // close switch statement
} // close SetTheScene() function
void main() {
int ALIVE = 100;
int PlayerPoints = 20; //Global
char conflict;
char conflict2;
char Name[25];
cout << "What is your name, player? ";
cin >> Name;
cout << "Welcome to the game, " << Name << ".\n\n"; //global Name
cout << "You have " << PlayerPoints << " points allocated to you as you start the game.\n";
SetTheScene(Name);
Monster Ogre;
Monster Dragon(20);
while(ALIVE>0) {
Dragon.Talk();
cout << "\nYou encounter a dragon!\n";
cout << "He blasts you with fire! ";
Attack(&PlayerPoints);
cout << " He is of strength ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "\nYou pull out your sword and levy a heavy blow upon his head.\n";
Dragon.setStrength(5);
cout << "That was some move! The dragon is already half dead!\n";
cout << "He now only has the strength of ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "Now what do you choose to do?\n"
<< "r = run away\n"
<< "s = stay and fight\n"
<< "t = talk to the dragon\n";
cin >> conflict;
system("cls");
switch(conflict) {
case 'r' : cout << "You run away and survive!\n"
<< "The dragon inflicts merely a flesh wound.\n"
<< "You loose a mere 15 points. ";
ALIVE = ALIVE - 15;
cout << "You now have " << ALIVE << " points left.";
break;
case 's' : cout << "You stay and fight. You discover that\n"
<< "the dragon is merely a baby dragon. It\'s mother\n"
<< "then shows up, greatly enraged by your attempt to\n"
<< "kill her child. She is really big and really angry.\n";
Dragon.setStrength(200);
cout << "You are now fighting a dragon of strength: ";
cout << Dragon.getStrength() << ".\n";
cout << "The dragon rips off your arms and legs and\n"
<< "fries you to a crispy, crunchy, golden brown.\n";
ALIVE = 0;
break;
case 't' : cout << "\nYou strike up a conversation with the dragon.\n"
<< "You begin telling it your life story and all your woes.\n"
<< "Succumbing to fatal levels of nausea and boredom, the \n"
<< "dragon lapses into a coma and dies...\n";
Dragon.setStrength(0);
cout << "Dragon reduced to strength: ";
cout << Dragon.getStrength() << ".\n";
Dragon.~Dragon();
cout << "You have " << ALIVE << " points left.";
break;
default : cout << "Invalid input. Please press either: r, s, or w.";
} //close switch statement
while(ALIVE>0) {
cout << "\nYou continue traveling eastward. You see something in the distance...\n";
Ogre.Talk();
cout << "It is an ogre!\n";
cout << "What do you do now?\n";
cout << "r = run away\n"
<< "t = talk\n";
cin >> conflict2;
system("cls");
if(conflict2 == 'r') {
cout << "\nThe ogre chases you. You fall. He chops off your head and eats you.\n";
ALIVE = 0;
}
else if(conflict2 == 't') {
cout << "\nYou make friends. The ogre likes you and so kills you.\n";
ALIVE = 0;
}
} //close 2nd while true loop
} //close 1st while true loop - ALIVE no longer > 0
cout << "\nYou fought bravely but died. As the worms devour your flesh\n"
<< "and your soul descends into Hades you vow that you will one day\n"
<< "arise to take vengeance on your foes. Dare you try your luck\n"
<< "and suffer defeat once more? At least for now, the game is over....\n\n";
} //close main function
// Project 3 - AdventureGame1.cpp : Adventure game using functions,
// a while true loop and a switch statement.
#include "stdafx.h"
#include <iostream.h>
void Talk() {
char name;
cout << "Very hospitable of you. You greet the old man. "
<< "He turns towards you and asks your name. Your reply?";
cin >> name;
}
int Attack() {
cout << "You attack. The old man turns into an ogre.\n"
<< "He pulls out an axe and chops off your head.\n"
<< "You are now dead, dead, dead...\n";
return 0;
}
void Give() {
cout << "You give him a sandwhich";
}
void Ignore() {
cout << "You decide to ignore the man";
}
void main()
{
int ALIVE = 1;
char choice1;
while (ALIVE != 0) {
cout << "\nIt\'s twilight, and you can just make out the "
<< "constellatoin Orion as you look North. You are "
<< "walking on towards a mountain range, ascending "
<< "towards the west. You see an elderly man in "
<< "tattered clothes approaching. What do you do?\n";
cout << "Your options are as follows:\n\n"
<< "T - Talk with the old man.\n"
<< "H - Hit the old man in the head with a shovel.\n"
<< "G - Give the old man a sandwhich.\n"
<< "I - Ignore the old man.\n";
cout << "\nYou choose: ";
cin >> choice1;
switch (choice1) {
case 'T' : Talk();
break;
case 't' : Talk();
break;
case 'H' : ALIVE = Attack();
break;
case 'h' : ALIVE = Attack();
break;
case 'G' : Give();
break;
case 'g' : Give();
break;
case 'I' : Ignore();
break;
case 'i' : Ignore();
break;
default : cout << "\nSorry, that is not a valid choice.\n\n";
} //closes switch statement
cout << "\nIf only, if only, if only...\n\n";
} //close while true loop
cout << "\n\nGame ending...\n\n";
} //closes main() function
// AdventureGame2.cpp Now using class structure, functions, random
number generation, and
// implementing variables and objects of global and local scope.
// Overriding default constructor and destructor
#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
class Monster {
public:
Monster(int MonStrength = 10) {
Strength = MonStrength;
cout << "\nA monster has been created.";}
~Monster() { cout << "\nThe monster has been destroyed!\n";}
int getStrength() { return Strength; }
void setStrength(int MonsStrength) { Strength = MonsStrength; }
void Talk() { cout << "\nGrrr!! I am a monster... "; }
private:
int Strength;
}; //closes class specification
int GenerateRandomNumber() {
int ResultRandom;
srand(time(NULL));
ResultRandom = (rand()%6) + 1;
cout << "\nToday\'s lucky number is: "
<< ResultRandom << ".\n\n";
return ResultRandom;
}
void SetTheScene() {
int Setting;
Setting = GenerateRandomNumber();
switch (Setting) {
case 1 : cout << "You are in a swamp.\n";
break;
case 2 : cout << "You are on a high mountain peak.\n";
break;
case 3 : cout << "You are on a grassy plane.\n";
break;
case 4 : cout << "You are in the desert.\n";
break;
case 5 : cout << "You find yourself in a deserted village.\n";
break;
case 6 : cout << "You find yourself in a steamy jungle.\n";
break;
default : cout << "Something is definitely wrong here. Should be 1-6.";
} // close switch statement
} // close SetTheScene() function
void main() {
int ALIVE = 100;
char conflict;
char conflict2;
SetTheScene();
Monster Ogre;
Monster Dragon(20);
while(ALIVE>0) {
Dragon.Talk();
cout << "\nYou encounter a dragon!\n"
<< "He is of strength ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "\nYou pull out your sword and levy a heavy blow upon his head.\n";
Dragon.setStrength(5);
cout << "That was some move! The dragon is already half dead!\n";
cout << "He now only has the strength of ";
cout << Dragon.getStrength();
cout << ".\n\n";
cout << "Now what do you choose to do?\n"
<< "r = run away\n"
<< "s = stay and fight\n"
<< "t = talk to the dragon\n";
cin >> conflict;
system("cls");
switch(conflict) {
case 'r' : cout << "You run away and survive!\n"
<< "The dragon inflicts merely a flesh wound.\n"
<< "You loose a mere 15 points. ";
ALIVE = ALIVE - 15;
cout << "You now have " << ALIVE << " points left.";
break;
case 's' : cout << "You stay and fight. You discover that\n"
<< "the dragon is merely a baby dragon. It\'s mother\n"
<< "then shows up, greatly enraged by your attempt to\n"
<< "kill her child. She is really big and really angry.\n";
Dragon.setStrength(200);
cout << "You are now fighting a dragon of strength: ";
cout << Dragon.getStrength() << ".\n";
cout << "The dragon rips off your arms and legs and\n"
<< "fries you to a crispy, crunchy, golden brown.\n";
ALIVE = 0;
break;
case 't' : cout << "\nYou strike up a conversation with the dragon.\n"
<< "You begin telling it your life story and all your woes.\n"
<< "Succumbing to fatal levels of nausea and boredom, the \n"
<< "dragon lapses into a coma and dies...\n";
Dragon.setStrength(0);
cout << "Dragon reduced to strength: ";
cout << Dragon.getStrength() << ".\n";
Dragon.~Dragon();
cout << "You have " << ALIVE << " points left.";
break;
default : cout << "Invalid input. Please press either: r, s, or w.";
} //close switch statement
while(ALIVE>0) {
cout << "\nYou continue traveling eastward. You see something in the distance...\n";
Ogre.Talk();
cout << "It is an ogre!\n";
cout << "What do you do now?\n";
cout << "r = run away\n"
<< "t = talk\n";
cin >> conflict2;
system("cls");
if(conflict2 == 'r') {
cout << "\nThe ogre chases you. You fall. He chops off your head and eats you.\n";
ALIVE = 0;
}
else if(conflict2 == 't') {
cout << "\nYou make friends. The ogre likes you and so kills you.\n";
ALIVE = 0;
}
} //close 2nd while true loop
} //close 1st while true loop - ALIVE no longer > 0
cout << "\nYou fought bravely but died. As the worms devour your flesh\n"
<< "and your soul descends into Hades you vow that you will one day\n"
<< "arise to take vengeance on your foes. Dare you try your luck\n"
<< "and suffer defeat once more? At least for now, the game is over....\n\n";
} //close main function
|