Gussing Game
Gussing Game
Class EC-04
Roll no 24/A14/035
Introduction
The Number Guessing Game is a classic game that has been enjoyed by players for generations due to its
simplicity and potential for strategic thinking. The game involves guessing a hidden number within a certain
range, with players receiving feedback on whether their guess was too high, too low, or correct. This type of
game can be adapted to different difficulty levels and is often used in educational settings to introduce
concepts such as binary search algorithms and logical deduction.
This game is not only fun but also serves as an excellent exercise for teaching mathematical reasoning,
probability, and problem-solving skills. It is versatile and can be implemented as a paper-based game, a
computer program, or a mobile app.
Gameplay Structure
The structure of a standard Number Guessing Game can be outlined as follows:
1. Initial Setup:
The game selects a random number within a predefined range (e.g., 1 to 100).
The number remains hidden from the player.
2. Player Interaction:
The player makes a guess within the specified range.
The game evaluates the guess and provides feedback:
"Too high"
"Too low"
"Correct"
3. Feedback and Adjustment:
Based on the feedback, the player adjusts their next guess.
Name Tanuj
Class EC-04
Roll no 24/A14/035
The process repeats until the player correctly guesses the number or runs out of attempts (in cases where
there is a guess limit).
4. Victory and Defeat Conditions:
Victory: The player correctly guesses the number within the allowed number of attempts.
Defeat: The player exhausts all their guesses without finding the correct number.
Imagine a number guessing game where the number to guess is between 1 and 50:
Strategic Approaches
Players can employ different strategies to maximize their chances of guessing the number quickly:
Binary Search Technique: The player can halve the search space with each guess. For instance, starting
with the middle number of the range (e.g., guessing 25 if the range is 1 to 50) allows the player to
determine which half of the range the correct number is in, then repeating this process.
Random Guessing: Less efficient but still a method where the player makes guesses without strategic
planning.
Range Adjustment: Players can customize the game to different ranges (e.g., 1 to 1000 for more
challenging gameplay).
Limited Attempts: Adding a fixed number of attempts increases the challenge and introduces pressure.
Hint System: The game can include lifelines that provide extra hints, such as “The number is divisible by 5.”
1. Teaches Logical Reasoning: Players develop their deductive reasoning skills by using feedback to make
informed guesses.
2. Engages Strategic Thinking: The game encourages players to plan their guesses to minimize the number
of attempts needed.
3. Fun and Educational: Simple yet educational, making it perfect for all age groups to play and learn from.
4. Adaptable Difficulty: Can be customized to include larger ranges, timed challenges, or multiplayer modes.
Applications
Name Tanuj
Class EC-04
Roll no 24/A14/035
Educational Tools: Used in schools to teach concepts like binary search and probability.
Coding Exercises: A common beginner project for learning programming logic and functions.
Recreational Play: Fun for casual play with friends or as a solo challenge.
Conclusion
The Number Guessing Game remains a popular and timeless activity that combines fun with cognitive
development. Whether implemented as a simple classroom activity or a more complex app, its
straightforward mechanics and the engaging feedback loop make it an effective tool for teaching logic,
improving strategic thinking, and providing entertainment
Step-by-Step Explanation:
1. Include necessary libraries:
c
Copy code
#include <stdio.h> #include <stdlib.h> #include <time.h>
stdio.h : Provides functions for input and output (like printf and scanf).
stdlib.h : Includes functions for generating random numbers ( rand() ).
time.h : Used to get the current time, which helps seed the random number generator.
2. Initialize variables:
c
Copy code
int randomNum, guess, attempts = 0 ;
randomNum : Holds the random number that the computer selects.
guess : Holds the player’s guess.
attempts : Tracks how many times the player has guessed.
3. Seed the random number generator:
c
Copy code
srand(time( 0 ));
srand(time(0)) : Seeds the random number generator using the current time, so the number chosen is
different every time you run the program.
4. Generate a random number:
c
Copy code
randomNum = rand() % 100 + 1 ;
rand() % 100 generates a random number between 0 and 99. Adding 1 makes it between 1 and 100
(inclusive).
5. Print welcome message:
c
Copy code
printf ( "Welcome to the Number Guessing Game!\n" ); printf ( "I have chosen a number between 1 and 100. Can
you guess it?\n" );
These lines display a welcome message to the player, explaining the game.
Name Tanuj
Class EC-04
Roll no 24/A14/035
6. Main game loop:
c
Copy code
while ( 1 ) { printf ( "Enter your guess: " ); scanf ( "%d" , &guess); attempts++;
The while (1) creates an infinite loop that keeps running until the correct number is guessed or the
player chooses to exit.
scanf("%d", &guess) takes the player's guess and stores it in the guess variable.
attempts++ increases the attempt counter by 1 each time the player makes a guess
.
7. Check if the player wants to exit:
if (guess == 0 ) { printf ( "You chose to exit. The correct number was %d. Goodbye!\n" , randomNum); break ;
}
If the player enters 0, the game will show a message and exit the loop using break.
8. Check if the guess is too low or too high:
c
Copy code
else if (guess < randomNum) { printf ( "Too low! Try again.\n" ); } else if (guess > randomNum) {
printf ( "Too high! Try again.\n" ); }
If the player's guess is less than the random number, it prints "Too low! Try again."
If the player's guess is more than the random number, it prints "Too high! Try again."
9. Check if the player guessed correctly:
c
Copy code
else { printf ( "\nCongratulations! You guessed the correct number: %d\n" , randomNum); printf ( "It took you
%d attempts to guess the number. Well done!\n" , attempts); break ; }
If the player's guess matches the random number, it prints a congratulatory message and exits the loop.
10. Program termination:
c
Copy code
return 0 ;
This line indicates that the program has finished running and ends the program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int randomNum, guess, attempts = 0;
srand(time(0));
while (1) {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess == 0) {
printf("You chose to exit. The correct number was %d. Goodbye!\n", randomNum);
break;
} else if (guess < randomNum) {
printf("Too low! Try again.\n");
} else if (guess > randomNum) {
printf("Too high! Try again.\n");
} else {
printf("\nCongratulations! You guessed the correct number: %d\n", randomNum);
printf("It took you %d attempts to guess the number. Well done!\n", attempts);
break;
}
}
return 0;
}