Number Guessing Game Program in C++ (GAME PROJECT)

Number Guessing Game Program in C++ (GAME PROJECT)

Nowadays people like online games. So, I have decided to write a series of articles where I will describe all the things related to online games. I will also write articles on slot machine games and their terminology with GLI (Gaming Labs International) certification.

Like the other games, we also need to write a program for slot machine games. Every slot machine game consists of a math engine and a reel engine. Math and reel engine are very important parts of slot games.

So, without wasting the time, let’s begin gaming series articles with a small gaming project “Number Guessing”. Here I am using C++ as a programming language and code block as IDE. In a later article, I will use SDL, unity, and unreal engine.

 

What is the number guessing game?

It is an interesting game in which the player will guess a number in the given range. If the chosen number will be matched with a winning number (number generated by a random number generator), then the user will get the price amount either the bet amount will deduct from the wallet amount.

 

How to play a number guessing game?

Suppose Apoorv is a player and he is going to play the “number guessing game”. So first he needs to recharge their wallet with some fixed amount. The amount should be greater than 1 cent because the least bet amount is 1 cent.

After recharging the wallet Apoorv needs to set some betting amount. It is the amount which will be deducted from the wallet if Apoorv will lose the game and if he will win then he will be received 10 times of bet amount.

Now Apoorv can play the game. He needs to select a number between 1 to 10. If the chosen number will be equal to a winning number, he will win. On each play, Apoorv’s wallet will be updated with the current amount.

Now I hope you able to understand the steps that how to play “number guessing game”. So, let’s see the code.

#include <iostream>
#include <string> // Needed to use strings
#include <cstdlib> // Needed to use random numbers
#include <ctime>
#include <string.h>
#include <stdio.h>

using namespace std;

//class for display related methods
class Display
{
public:
    void printMessageCenter(const char* message);
    void headMessage(const char *message);
    void welcomeMessage();
    void drawLine(int n, char symbol);
    void rules();
};


void Display::drawLine(int n, char symbol)
{
    for(int i=0; i<n; i++)
        cout << symbol;
    cout << "\n" ;
}

void Display::rules()
{
    headMessage("RULES OF THE GAME");
    cout << "\n\n\n\n";
    drawLine(80,'-');
    cout << "\t1. Choose any number between 1 to 10\n";
    cout << "\t2. If you win you will get 10 times of money you bet\n";
    cout << "\t3. If you bet on wrong number you will lose your betting amount\n\n";
    drawLine(80,'-');
    cout << "\n\n\n\t\t\t Enter any key to continue.....";
    getchar();
}



//Align the message
void Display::printMessageCenter(const char* message)
{
    int len =0;
    int pos = 0;
    //calculate how many space need to print
    len = (78 - strlen(message))/2;
    cout << "\t\t\t";
    for(pos =0 ; pos < len ; pos++)
    {
        //print space
        cout <<" ";
    }
    //print message
    cout << message;
}

//Head message
void Display::headMessage(const char *message)
{
    system("cls");
    cout << "\t\t\t###########################################################################";
    cout << "\n\t\t\t############                                                   ############";
    cout << "\n\t\t\t############             Casino Game Project in C++            ############";
    cout << "\n\t\t\t############                                                   ############";
    cout << "\n\t\t\t###########################################################################";
    cout << "\n\t\t\t---------------------------------------------------------------------------\n";
    printMessageCenter(message);
    cout << "\n\t\t\t----------------------------------------------------------------------------";
}

//Display message
void Display::welcomeMessage()
{
    headMessage("www.aticleworld.com");
    cout << "\n\n\n\n\n";
    cout << "\n\t\t\t  **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n";
    cout << "\n\t\t\t        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
    cout << "\n\t\t\t        =                                           =";
    cout << "\n\t\t\t        =                    WELCOME                =";
    cout << "\n\t\t\t        =                      TO                   =";
    cout << "\n\t\t\t        =                 CASINO GAME               =";
    cout << "\n\t\t\t        =                                           =";
    cout << "\n\t\t\t        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
    cout << "\n\t\t\t  **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n";
    cout << "\n\n\n\t\t\t Enter any key to continue.....";
    getchar();
}


//Main class of the project
class PlayerInfo:public Display
{
public:
    int getGuessNumber();
    void setGuessNumber();
    void setAmount();
    float getAmount();
    int getdice();
    void updateAmount(const bool isWin);
    void setBettingAmount();
    bool isPlayerWin();
    void init();
    void displayMessageAfterPlay(const bool isWin);
    PlayerInfo():m_amount(0.00),m_bettingAmount(0.00),m_guessNumber(-1)
    {

    }

private:
    float m_amount; //Total balance of player
    float m_bettingAmount; //Betting Amount
    int m_guessNumber; //Number guessed by player
};




//Set Wallet Amount
void PlayerInfo::setAmount()
{
    headMessage("Set Amount");
    do
    {
        cout << "\n\nEnter Deposit amount to play game : $";
        cin >> m_amount;
        if(m_amount < 0.01f)
        {
            cout << "\n\nPlease Enter valid amount to play the Game!!";
        }
    }
    while(m_amount < 0.01f);
}


//Get wallet Amount
float PlayerInfo::getAmount()
{
    return m_amount;
}

int PlayerInfo::getGuessNumber()
{
    return m_guessNumber;
}


//Get number from player
void PlayerInfo::setGuessNumber()
{
    headMessage("Set Guess Number");
    do
    {
        cout << "\n\nGuess your number to bet between 1 to 10 :";
        cin >> m_guessNumber;
        if(m_guessNumber <= 0 || m_guessNumber > 10)
            cout << "\n\nPlease check the number!! should be between 1 to 10\n"
                 <<"\nRe-enter the number\n ";
    }
    while(m_guessNumber <= 0 || m_guessNumber > 10);
}


//Update wallet amount
void PlayerInfo::updateAmount(const bool isWin)
{
    m_amount = isWin ? (m_amount + (m_bettingAmount *10)): (m_amount - m_bettingAmount);
}


//Set betting amount
void PlayerInfo::setBettingAmount()
{
    headMessage("Set Bet Amount");
    do
    {
        cout <<"\n\nEnter money to bet : $";
        cin >> m_bettingAmount;
        if(m_bettingAmount > m_amount)
        {
            cout << "\n\nYour wallet amount is $" << m_amount;
            cout << "\n\nYour betting amount is more than your current balance";
        }
    }
    while(m_bettingAmount > m_amount);
}

//Check is player w
bool PlayerInfo::isPlayerWin()
{
    // Will hold the randomly generated integer between 1 and 10
    const int dice = rand()%10 + 1;

    return ((dice == getGuessNumber())? true:false);
}

//Init the game.
void PlayerInfo::init()
{
    welcomeMessage();
    // "Seed" the random generator
    srand(time(0));
    rules();
}


//Display message after each play
void PlayerInfo::displayMessageAfterPlay(const bool isWin)
{
    if(isWin)
    {
        cout << "\n\nGood Luck!! You won $" << m_bettingAmount * 10;
        cout << "\n\nNow update Amount is $" << m_amount;
    }
    else
    {
        cout << "\n\nBad Luck this time !! You lost $"<< m_bettingAmount <<"\n";
        cout << "\n\nNow update Amount is $" << m_amount;
    }
}



int main()
{
    class PlayerInfo obj_player ;
    char choice;

    //init game
    obj_player.init();

    //Set wallet amount
    obj_player.setAmount();
    do
    {
        cout << "\n\nYour current balance is $" << obj_player.getAmount() << "\n";

        //Set bet amount
        obj_player.setBettingAmount();

        //Set guess number
        obj_player.setGuessNumber();

        //Check whether player lose or win the game
        const bool isPlayerWin = obj_player.isPlayerWin();

        //Update wallet amount
        obj_player.updateAmount(isPlayerWin);

        //Display the result after each play
        obj_player.displayMessageAfterPlay(isPlayerWin);

        //Check wallet amount and accordingly ask the player
        //to play again
        if(obj_player.getAmount() == 0.00f)
        {
            cout << "You have no money to play, Good Bye..";
            break;
        }

        //Ask use choice for replay
        cout << "\n\n-->Do you want to play again (y/n)? ";
        cin >> choice;
    }
    while(choice =='Y'|| choice=='y');

    cout << "\n\n\n";
    obj_player.drawLine(70,'=');
    cout << "\n\nThanks for playing game. Your balance amount is $" << obj_player.getAmount() << "\n\n";
    obj_player.drawLine(70,'=');

    return 0;
}

Output:

Welcome screen:

number guessing game: casino game

 

Now when you will press any key then the game will rule display.

Game Rule number guessing

 

Next step to add the amount in the wallet to play the game.

EnteredAmount Number Guessing

 

Now times to select a bet amount, it should be less than the wallet amount.

Entered Bet Amount Number guessing game

 

Now its time to guess the number. The below screen asks the user to choose a number between 1 to 10.

slot game

 

Now its time to result, you can win or lose it’s up to your luck. If you have a remaining amount in the wallet then it asks to play again.

casino game

 

Recommended posts:

Leave a Reply

Your email address will not be published. Required fields are marked *