Question

In: Computer Science

Using Object Oriented Programming method, make a simple game program with C++. Please include fully explanation/diagram(if...

Using Object Oriented Programming method, make a simple game program with C++. Please include fully explanation/diagram(if any) about which parts implement the object oriented programming.

Solutions

Expert Solution

SOLUTION

Description of the game:

  1. It is two-person, one is strong and another one is weak. Users can make a choice to choose either a strong person or a weak person to guess a hidden lucky number.
  2. The twist is each person has health, which is initially 100, but a weak person loses 20 health after a wrong guess whereas a strong man loses 10 health.
  3. Both persons can make guess till his/her health is greater than 0.

Implementation:

INPUT 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:


Related Solutions

Make a simple game using C++ which implements all about Object Oriented Programming (Please make an...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c ++
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
In Object Oriented programming C++ : Write the appropriate functions for Student to neatly display a...
In Object Oriented programming C++ : Write the appropriate functions for Student to neatly display a Student, and then finally GPA. Have items neatly line up in columns. I need help creating a derived class called student that finds GPA (between 0.0 and 4.0) and credits completed (between 0 and 199).
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session. The server should support multiple clients (use telnet as the client in this task). Compile and run the server program. Try and connect to it from multiple other hosts using telnet as...
Why is it more feasible to use Objects and object oriented programming as compared to using...
Why is it more feasible to use Objects and object oriented programming as compared to using method based programs? What are the disadvantages of using only methods in your programs.
This is from Microsoft Visual C#: An Introduction to Object Oriented Programming by Joyce Farrell (7th...
This is from Microsoft Visual C#: An Introduction to Object Oriented Programming by Joyce Farrell (7th Edition) The provided file has syntax and/or logical errors. Determine the problem(s) and fix the program. /* The program requires the user to guess the number of days it takes to make X amount of money when doubling the value every day. The starting value is $0.01. The program indicates if the guess is too high, or too low. */ using System; using static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT