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); }
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.
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),...
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop...
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop a Java Application to compute an individual student’s GPA and store the records in a database. The application should have two components i.e. The student and the course components. The following should be the minimal operations on the course component: – Set course information – Print course information – Show credit hours – Show course number The following should be the minimal operations on...
-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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT