Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

The computer randomly selects a number between 1 and 100. The player has a limited Baseball Bros Io number of attempts (e.g., 7) to guess the number. After each guess, the game provides feedback whether the guess is too low, too high, or correct. If the player guesses correctly within the limit, they win. Otherwise, they lose and the number is revealed.

21 views
ubuntu2204
1
#include <iostream>
2
#include <cstdlib>
3
#include <ctime>
4
5
using namespace std;
6
7
// Function to generate a random number between 1 and 100
8
int generateRandomNumber() {
9
return rand() % 100 + 1;
10
}
11
12
// Function to play one round of the game
13
void playGame() {
14
int secretNumber = generateRandomNumber();
15
int guess;
16
int attempts = 7;
17
18
cout << "šŸŽÆ I have selected a number between 1 and 100.\n";
19
cout << "You have " << attempts << " attempts to guess it!\n\n";
20
21
for (int i = 1; i <= attempts; i++) {
22
cout << "Attempt " << i << ": Enter your guess: ";
23
cin >> guess;
24
25
if (guess == secretNumber) {
26
cout << "šŸŽ‰ Congratulations! You guessed the number correctly!\n";
27
return;
28
} else if (guess < secretNumber) {
29
cout << "šŸ”¼ Too low!\n";
30
} else {
31
cout << "šŸ”½ Too high!\n";
32
}
33
}
34
35
cout << "\n😢 You've used all your attempts. The number was: " << secretNumber << "\n";
36
}
37
38
// Main function
39
int main() {
40
srand(time(0)); // Seed random number generator
41
42
char playAgain;
43
44
cout << "==== Welcome to the Number Guessing Game ====\n";
45
46
do {
47
playGame();
48
cout << "\nWould you like to play again? (y/n): ";
49
cin >> playAgain;
50
cout << "\n";
51
} while (playAgain == 'y' || playAgain == 'Y');
52
53
cout << "Thanks for playing! Goodbye! šŸ‘‹\n";
54
return 0;
55
}
56
57