In: Computer Science
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
Description of the game:
Implementation:
Code:
#include<iostream>
#include<string.h>
#include<time.h>
using namespace std;
class Person //base class
{
protected:
int health; //protected member
public:
int getHealth() //public member
{
return health;
}
};
class StrongPerson : public Person // inheritance StrongPerson is derived class & Persone is base class
{
protected:
public:
StrongPerson() //constructor
{
health = 100; //setting health, which is in base class with 100;
}
void decreaseHealth()
{
health = health - 10; //the health will decrease by 10 when this function will call
}
};
class WeakPerson : public Person //// inheritance WeakPerson is derived class & Persone is base class
{
protected:
public:
WeakPerson()
{
health = 100; //setting health, which is in base class with 100;
}
void decreaseHealth()
{
health = health - 20; //the health will decrease by 10 when this function will call
}
};
int main()
{
int choice, lucky_number, guess;
StrongPerson sp;
WeakPerson wp;
while(1)
{
cout<<"================== MENU =================="<<endl;
cout<<"\t1. Take Strong Person"<<endl;
cout<<"\t2. Take Weak Person"<<endl;
cout<<"\t3. Quit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
//taking random value between 0 - 20
srand(time(0));
lucky_number = rand() % 20;
cout<<lucky_number<<" Lucking Number (for debugging purpose)"<<endl; //enable this statement for debugging purpose
cout<<"Your health is: "<<sp.getHealth()<<endl; //showing initial health value, which is 100
cout<<"There is a Lucky Number, Which is hidden, try to guess the Number, If you guess worng then your health will decrease by 10"<<endl;
cout<<"Try to Guess the Lucky Number: ";
cin>>guess;
while(sp.getHealth() > 0) //keep asking the lucky number while health is > 0, then stop
{
if(lucky_number == guess) //if user make correct guess
{
cout<<"Great..!! You got it with your health: "<<sp.getHealth()<<endl;
cout<<"================== END =================="<<endl;
break;
}
else if(lucky_number < guess) //if user make higher guess
{
cout<<"Too High, Try lower...."<<endl;
sp.decreaseHealth(); //decreasing health of StrongPerson with 10
cout<<"Your Health: "<<sp.getHealth()<<endl; //showing current health after wrong guessing
cout<<"Try Again: ";
cin>>guess; //taking new guess from user
}
else //if user make lower guess
{
cout<<"Too Low, Try higher...."<<endl;
sp.decreaseHealth(); //decreasing health of StrongPerson with 10
cout<<"Your Health: "<<sp.getHealth()<<endl; //showing current health after wrong g
cout<<"Try Again: ";
cin>>guess; //taking new guess from user
}
}
break;
case 2:
//taking random value between 0 - 20
srand(time(0));
lucky_number = rand() % 20;
cout<<lucky_number<<" Lucking Number (for debugging purpose)"<<endl; //enable this statement for debugging pur
cout<<"Your health is: "<<wp.getHealth()<<endl;
cout<<"There is a Lucky Number, Which is hidden, try to guess the Number, If you guess worng then your health will decrease by 20"<<endl;
cout<<"Try to Guess the Lucky Number: ";
cin>>guess;
while(sp.getHealth() > 0) //keep asking the lucky number while health is > 0, then stop
{
if(lucky_number == guess) //if user make correct guess
{
cout<<"Great..!! You got it with your health: "<<wp.getHealth()<<endl;
cout<<"================== END =================="<<endl;
break;
}
else if(lucky_number < guess) //if user make higher guess
{
cout<<"Too High, Try lower...."<<endl;
wp.decreaseHealth(); //decreasing health of StrongPerson with 20
cout<<"Your Health: "<<wp.getHealth()<<endl; //showing current health after wrong g
cout<<"Try Again: ";
cin>>guess; //taking new guess from user
}
else //if user make loweer guess
{
cout<<"Too Low, Try higher...."<<endl;
wp.decreaseHealth(); //decreasing health of StrongPerson with 20
cout<<"Your Health: "<<wp.getHealth()<<endl; //showing current health after wrong g
cout<<"Try Again: ";
cin>>guess; //taking new guess from user
}
}
break;
case 3:
exit(1); //exiting the code
break;
default:
cout<<"Wrong Input, Try Again..."<<endl;
break;
}
}
}
Output: