This C program implements a number guessing game where Player-1 sets a number and Player-2 tries to guess it within a limited number of attempts.
This program allows two players to play a simple guessing game. Player-1 chooses a number, and Player-2 has a certain number of attempts to guess the number. If Player-2 guesses correctly, they win. If they exhaust all attempts without guessing correctly, Player-1 wins.
- Player-1 inputs a secret number.
- Player-2 is given a number of attempts to guess the secret number.
- Player-2 makes guesses and is informed whether the guess is correct or how many attempts remain.
- The game ends when Player-2 guesses the number correctly or runs out of attempts.
X
: The secret number chosen by Player-1.N
: The number of attempts allowed for Player-2.Guess
: The current guess made by Player-2.Tries_left
: The number of remaining attempts for Player-2.
- The program prompts Player-1 to enter a secret number (
X
). - The program prompts Player-1 to enter the number of attempts allowed for Player-2 (
N
). Tries_left
is initialized with the value ofN
.- A
while
loop runs as long asTries_left
is greater than 0:- Player-2 is prompted to enter a guess (
Guess
). - If
Guess
equalsX
, Player-2 wins, and the program prints a success message and breaks out of the loop. - If
Guess
does not equalX
,Tries_left
is decremented by 1, and the program prints the number of remaining attempts.
- Player-2 is prompted to enter a guess (
- If
Tries_left
reaches 0, Player-1 wins, and the program prints a failure message.
-
Compile the Program
gcc guessing_game.c -o guessing_game
-
Run the Program
./guessing_game
-
Game Flow
- Player-1 enters the secret number and the number of allowed attempts.
- Player-2 makes guesses until they either guess correctly or run out of attempts.
Enter the number given by Player-1: 50
Enter the number of tries given for Player-2: 5
Enter your Guess: 30
Wrong, 4 Choice(s) Left!
Enter your Guess: 40
Wrong, 3 Choice(s) Left!
Enter your Guess: 50
Right, Player-2 wins!
If Player-2 does not guess correctly within the allowed attempts:
Enter your Guess: 30
Wrong, 4 Choice(s) Left!
Enter your Guess: 40
Wrong, 3 Choice(s) Left!
Enter your Guess:
let's break down the code step by step to explain its functionality.
#include <stdio.h>
int main() {
int X, N, Guess, Tries_left;
// Prompt Player-1 to enter the secret number
printf("Enter the number given by Player-1: ");
scanf("%d", &X);
// Prompt Player-1 to enter the number of attempts allowed for Player-2
printf("Enter the number of tries given for Player-2: ");
scanf("%d", &N);
// Initialize the number of tries left for Player-2
Tries_left = N;
// Loop to allow Player-2 to make guesses until they run out of attempts
while (Tries_left > 0) {
// Prompt Player-2 to enter their guess
printf("Enter your Guess: ");
scanf("%d", &Guess);
// Check if the guess is correct
if (Guess == X) {
// If the guess is correct, Player-2 wins and the game ends
printf("Right, Player-2 wins!\n");
break;
} else {
// If the guess is incorrect, decrement the number of tries left
Tries_left--;
printf("Wrong, %d Choice(s) Left!\n", Tries_left);
}
}
// If Player-2 runs out of attempts, Player-1 wins
if (Tries_left == 0) {
printf("Player-1 wins!\n");
}
return 0;
}
#### 1. Include Standard I/O Library
```c
#include <stdio.h>
This line includes the standard input-output library, which allows us to use functions like printf
and scanf
.
int main() {
This is the main function where the execution of the program begins.
int X, N, Guess, Tries_left;
Here, four integer variables are declared:
X
: The secret number that Player-1 will input.N
: The number of attempts that Player-2 will have to guess the secret number.Guess
: The current guess made by Player-2.Tries_left
: The number of attempts remaining for Player-2.
printf("Enter the number given by Player-1: ");
scanf("%d", &X);
printf("Enter the number of tries given for Player-2: ");
scanf("%d", &N);
The program prompts Player-1 to enter the secret number (X
) and the number of tries (N
) allowed for Player-2. These values are read using scanf
.
Tries_left = N;
The variable Tries_left
is initialized with the value of N
, representing the total number of attempts Player-2 has.
while (Tries_left > 0) {
This loop continues as long as Tries_left
is greater than 0.
printf("Enter your Guess: ");
scanf("%d", &Guess);
Player-2 is prompted to enter their guess, which is read into the variable Guess
.
if (Guess == X) {
printf("Right, Player-2 wins!\n");
break;
} else {
Tries_left--;
printf("Wrong, %d Choice(s) Left!\n", Tries_left);
}
- If
Guess
equalsX
, Player-2 has guessed correctly. The program prints a success message and breaks out of the loop. - If
Guess
does not equalX
,Tries_left
is decremented by 1, and the program prints the number of remaining attempts.
if (Tries_left == 0) {
printf("Player-1 wins!\n");
}
If Tries_left
reaches 0 (i.e., Player-2 has used up all attempts without guessing correctly), the program prints a message indicating that Player-1 wins.
return 0;
}
The main function returns 0, indicating that the program has executed successfully.
- Player-1 inputs a secret number and the number of allowed attempts for Player-2.
- Player-2 attempts to guess the number within the given attempts.
- The program informs Player-2 whether their guess is correct and how many attempts are left.
- The game ends either when Player-2 guesses correctly or runs out of attempts, determining the winner.