Casino Number Guessing Game in C++

1. Introduction

This blog post guides you through developing a simple Casino Number Guessing Game in C++. The game generates a random number within a specified range, and the player attempts to guess this number. If the player's guess is too high or too low, the game provides hints until the player guesses the number correctly or runs out of attempts.

Casino Number Guessing Game in C++

2. Program Steps

1. Include necessary headers.

2. Generate a random number within a specified range.

3. Take the player's guess as input.

4. Compare the player's guess with the generated number and provide feedback.

5. Repeat the guessing process until the correct number is guessed or attempts are exhausted.

6. Display a message indicating whether the player has won or lost.

3. Code Program

#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()

using namespace std;

int main() {
    srand(time(0)); // Seed the random number generator

    int randomNumber = rand() % 100 + 1; // Generates a random number between 1 and 100
    int guess;
    int attempts = 5;

    cout << "Welcome to the Casino Number Guessing Game!" << endl;
    cout << "Guess the number between 1 and 100. You have 5 attempts." << endl;

    while (attempts > 0) {
        cout << "Enter your guess: ";
        cin >> guess;

        if (guess < 1 || guess > 100) {
            cout << "Please guess a number within the range 1 to 100." << endl;
            continue; // Skip the rest of the loop iteration
        }

        if (guess == randomNumber) {
            cout << "Congratulations! You've guessed the number correctly." << endl;
            break; // Exit the loop
        } else if (guess < randomNumber) {
            cout << "Too low. Try again." << endl;
        } else {
            cout << "Too high. Try again." << endl;
        }

        attempts--;

        if (attempts == 0) {
            cout << "You've run out of attempts. The correct number was " << randomNumber << "." << endl;
        } else {
            cout << "You have " << attempts << " attempts left." << endl;
        }
    }

    return 0;
}

Output:

Welcome to the Casino Number Guessing Game!
Guess the number between 1 and 100. You have 5 attempts.
Enter your guess: 50
Too low. Try again.
You have 4 attempts left.
Enter your guess: 90
Too low. Try again.
You have 3 attempts left.
Enter your guess: 99
Congratulations! You've guessed the number correctly.

Explanation:

1. Random Number Generation: The game begins by generating a random number between 1 and 100 using rand() seeded with the current time to ensure different numbers in each game session.

2. Taking Input: The player is prompted to enter their guess, which is compared to the generated number.

3. Providing Feedback: After each guess, the game informs the player if their guess was too high, too low, or correct.

4. Winning Condition: The player wins if they guess the number correctly within their available attempts.

5. Losing Condition: If the player exhausts all attempts without guessing correctly, the game ends, and the correct number is revealed.

This game is a fun way to learn basic C++ programming concepts, such as loops, conditional statements, input handling, and working with random numbers.

Comments