3
\$\begingroup\$

I have created a casino number guessing game in C++:

#include <iostream>
#include <random>

void displayGameRules()
{
    std::cout << "\nGAME RULES: \n"
              << "1.  Choose any numbers between 1 to 100\n"
              << "2.  If you win you will get 10 times the money you bet\n"
              << "3.  If you chose the numbers wrongly you will lose the money you bet\n"
              << "4.  Minimum bet is $10\n"
              << "5.  If your remaining amount is less than $10, the game will automatically end\n\n";
}

void displayRemainingCredit(int remainingCredit)
{
    std::cout << "Your current credit is: $" << remainingCredit << '\n';
}

bool continuePlaying()
{
    std::cout << "Do you wish to continue playing? Select Y or N: ";
    char status;
    std::cin >> status;
    std::cout << "\n";
    return (status=='Y') ? true : false;
}

int promptBettingAmount()
{
    std::cout << "How much do you want to bet? (Minimum bet is $10): $";
    int bettingAmt=0;
    std::cin >> bettingAmt;
    std::cout << '\n';
    return bettingAmt;
}

bool validBettingAmount(int bettingAmt)
{
    if (bettingAmt < 10)
    {
        std::cout << "Your betting amount is less than $10\n";
        return false;
    }
    return true;
}

int promptUserNumberGuess()
{
    std::cout << "Enter the number you want to guess (Between 1 to 100): ";
    int guessedNum;
    std::cin >> guessedNum;
    std::cout << '\n';
    return guessedNum;
}

bool validUserGuess(int userGuess)
{
    if (userGuess < 0 || userGuess > 100)
    {
        std::cout << "Invalid user guess\n";
        return false;
    }
    return true;
}

int getPCRandomNumer()
{
    std::random_device rd;
    std::uniform_int_distribution<int> dist(1, 100);
    return dist(rd);
}

bool userWins(int userNum, int pcRandNum)
{
    return userNum == pcRandNum;
}

void diplayUserChoiceAndPcRandNumber(int userGuess, int pcRandNumber)
{
    std::cout << "Your guess is: " << userGuess << "\n"
              << "PC number is: " << pcRandNumber << "\n";
}

int main()
{
    displayGameRules();
    int remainingCredit = 100;
    displayRemainingCredit(remainingCredit);

    do
    {
        int betAmt = promptBettingAmount();
        if (!validBettingAmount(betAmt)) continue;

        int userGuess = promptUserNumberGuess();
        if (!validUserGuess(userGuess)) continue;

        int pcRandNumber = getPCRandomNumer();

        diplayUserChoiceAndPcRandNumber(userGuess, pcRandNumber);

        if (userWins(userGuess, pcRandNumber))
        {
            remainingCredit += (betAmt*10);
            std::cout << "You win, your current credit amount is now : $" << remainingCredit << "\n"; 
        }
        else
        {
            remainingCredit -= betAmt;
            std::cout << "You lose, your current credit amount is now : $" << remainingCredit << "\n"; 
        }

        if (remainingCredit < 10) break;
    } while (continuePlaying());

    return 0;
}

Appreciate your feedbacks on any improvement that can be made to this program. Thank you.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

seems pretty solid, I only have 2 small things to say: first inside bool continuePlaying() you don't need Ternary Operator you can just write it as return status == 'Y';.
second is really nitpicking but I don't like void diplayUserChoiceAndPcRandNumber(int userGuess, int pcRandNumber) the name is too long but overall I like your code especially how you divided everything into functions very well.

\$\endgroup\$
1
\$\begingroup\$

In your code If the user enters an invalid userNumber through promptUserNumberGuess() method, it will take you to the beginning of the loop which means user will be asked to enter betting amount again. This may not be the ideal user experience. Ideally I as a user would expect to re-enter only the invalid entry.

\$\endgroup\$
1
\$\begingroup\$

Use a multiline string literal to print multiline output:

char const * const g_game_rules = R"(
GAME RULES:
1.  Choose any numbers between 1 to 100
2.  If you win you will get 10 times the money you bet
3.  If you chose the numbers wrongly you will lose the money you bet
4.  Minimum bet is $10
5.  If your remaining amount is less than $10, the game will automatically end
)";

void displayGameRules()
{
    std::cout << g_game_rules << '\n';
}

Since most of you code is user IO processing, I would suggest you also focus on user input validation. Currently, wrong inputs just exit the game. Here is an example of how to write it:

int promptBettingAmount()
{
    while(true){
        std::cout << "How much do you want to bet? (Minimum bet is $10): $";
        int bettingAmt = 0;
        std::cin >> bettingAmt;
        std::cout << '\n';
        if(!std::cin.fail()){
            return bettingAmt;
        }
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

Use a space around operators like =, +, - etc. This makes the code easier to read.

In getPCRandomNumer(), don't use random device to generate random numbers. Use something like std::mt19937 which is a pseudo-random number generator. Seed it once from the random device. std::random_device uses hardware-generated random numbers so is often significantly slower than a PRNG. Though speed is not something that this code needs, it's good to know.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.