-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_Guessing_Game.php
More file actions
40 lines (33 loc) · 996 Bytes
/
Number_Guessing_Game.php
File metadata and controls
40 lines (33 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
// Welcome message
echo "Welcome to the Number Guessing Game!\n";
echo "I have picked a random number between 1 and 100.\n";
echo "Can you guess what it is?\n";
// Generate a random number between 1 and 100
$randomNumber = rand(1, 100);
$attempts = 0;
while (true) {
echo "Enter your guess: ";
// Get user input
$guess = trim(fgets(STDIN));
// Increase attempt count
$attempts++;
// Check if the input is a valid number
if (!is_numeric($guess)) {
echo "Please enter a valid number.\n";
continue;
}
// Convert guess to an integer
$guess = (int)$guess;
// Check if the guess is correct
if ($guess === $randomNumber) {
echo "Congratulations! You guessed the number correctly.\n";
echo "It took you $attempts attempts to guess the number.\n";
break;
} elseif ($guess < $randomNumber) {
echo "Too low! Try again.\n";
} else {
echo "Too high! Try again.\n";
}
}
?>