Question

In: Computer Science

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)

Solutions

Expert Solution

Description of the game:

  1. There is two person, one is strong and other one is weak. user can make a choice to choose either strong person or weak person to guess a hidden lucky number.
  2. The twist is each person have a health, which is initially 100, but weak person lose 20 health after a wrong guess where as strong man lose 10 health.
  3. Both person can make guess till his/her health is greater than 0.

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:


Related Solutions

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...
What are the object oriented concepts and which all object oriented concepts are used in the...
What are the object oriented concepts and which all object oriented concepts are used in the given program? Consider the following code and explain how each of the object oriented concepts are applied in the given program. (CO1) class Vehicle { string brand; public: void honk(); void honk(int); }; void Vehicle::honk() { cout << "Tuut, tuut! \n" ; } void Vehicle::honk(int x) { for(int i=0;i<x;i++) cout << "Tuut, tuut! \n" ; } int main() { Vehicle V1; V1.honk(); V1.honk(3); }
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.
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please provide 7 answer options for EACH question and the select answer for EACH question
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by...
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by writing some programs in Java. The first step will be to install and integrated development environment (IDE) that will be where you will write and compile your programs. You will also write your first program using Java to show that you have correctly installed the IDE. The project instructions and deliverables are as follows: Download and install Java JDK and NetBeans IDE using the...
C++In Object Oriented Programming, classes represent abstractionsof real things in our programs. We must...
C++In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition. The class must have at least three properties/data fields and three functions/behaviors (constructors and get/set functions don’t count). Do not pick examples from the textbook and do not redefine...
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities....
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities. Group of answer choices A. True B. False 2. __________ programming is centered on the procedures or actions that take place in a program. Group of answer choices A. Class B. Object-oriented C. Menu-driven D. Procedural/ Structural 3. __________ programming encapsulates data and functions in an object. Group of answer choices A. Object-oriented B. Class C. Menu-driven D. Procedural 4. The data in an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT