-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlow.php
42 lines (30 loc) · 863 Bytes
/
highlow.php
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
41
42
<?php
// cpu picks a random number
// prompt to pick a number
// user sees higher/lower according to guess
// correct guess leads to game over
if ($argc == 3) {
$low = $argv[1];
$high = $argv[2];
$answer = mt_rand($low, $high);
} else {
$answer = mt_rand(1, 100);
}
$guesses = 1;
fwrite(STDOUT, "Let's play a game. Guess what number I am thinking of.\n");
$user_guess = trim(fgets(STDIN));
while ($user_guess != $answer) {
// prompt user for another answer
// capture stdin
$guesses++; //tracks guesses for use later
if ($user_guess > $answer) {
echo "Lower.";
} elseif ($user_guess < $answer) {
echo "Higher.";
}
$user_guess = trim(fgets(STDIN));
// fwrite(STDOUT, "Guess again.\n");
} // end while statement
echo "Good guess!\n";
echo "It took you $guesses tries to figure me out.";
?>