Indeed simple. These are the rules (from a homework of the SoftUni):
Implement a “Guess Number Game” in PHP. Keep the secret number in the user’s session and indicate “Up” or “Down” after each guess.
- At the start page of the game (php) the user enters his name and clicks the [Start Game] button. The server generates a random secret number in range [1..100]. The user’s goal is to guess the secret number. After each guess, the server says either “Congratulations” or “Up” or “Down”.
- After the game is started, the browser is redirected to the game play page:php. The page asks the user for a number in range [1..100]. After the number is sent, the server responds with:
- “Congratulations, <name>“ – when the user’s guess is the secret number. A button [Play Again] is shown to redirect the user to the start page.
- “Up” – when the secret number is greater than the entered number. The user can enter a new guess again.
- “Down” – when the secret number is less than the entered number. The user can enter a new guess again.
- “Invalid Number” – when the entered value is not a number or is out of the range [1..100]. The user can enter a new guess again.
What have I done? This is the interface:
Pretty much the secret is in the usage of the cookies. They are the one to remember the username, the random number and the given one. The deletion of the cookies is something tricky, probably I should have used a function for this, but it is probably ok the way it is.
Here comes the code! 🙂
index.php:
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <body> <? unset($_COOKIE['my_login']); setcookie('my_login', '', time() - 3600, '/'); ?> <form action="play.php" method="GET"><br> Name: <input type="text" name="login"><br><hr /> <input type="submit" value="Start Game"> </form> </body> </html> |
play.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<html> <body> <?php function compare_two_numbers($number_A, $number_B){ if ($number_A == $number_B){ echo "Congratulations! You won!"; $GLOBALS['b_match'] = true; } if ($number_A > $number_B){ echo "You should go lower."; } if ($number_A < $number_B){ echo "You should go higher."; } } ?> <?php $GLOBALS['b_match'] = false; if(isset($_GET['number_guess'])){ setcookie('my_guess',$_GET['number_guess']); $_COOKIE['my_guess'] = $_GET['number_guess']; }else{ unset($_COOKIE['my_guess']); setcookie('my_guess', '', time() - 3600, '/'); } if(isset($_GET['login'])){ setcookie('my_login',$_GET['login']); setcookie('my_rand',rand(1,100)); $_COOKIE['my_login'] = $_GET['login']; $GLOBALS['b_match'] = false; } echo "Welcome " . $_COOKIE['my_login'] . "!"; if(isset($_COOKIE['my_guess'])){ echo "</br>"; if (is_numeric($_COOKIE['my_guess'])){ echo "You have chosen " . $_COOKIE['my_guess'] . ".</br>"; }else { echo "Please, give me numeric values! :)" . ".</br>"; } echo "The random is " . $_COOKIE['my_rand'] . ".</br>"; compare_two_numbers($_COOKIE['my_guess'], $_COOKIE['my_rand']); echo "</br>"; } else { echo "</br>"; echo "Guessing is not difficult! :)"; } if (!$GLOBALS['b_match']){ echo "</br>"; echo "Try to guess:"; echo "</form>"; echo "<form action='play.php' method='get'>"; echo "Number: <input type = 'text' name = 'number_guess'>"; echo "<input type='submit'>"; echo "</form>"; } else { echo "<hr />"; echo "<iframe width='420' height='315' src='https://www.youtube.com/embed/UWLIgjB9gGw' frameborder='0' allowfullscreen></iframe>"; echo "<hr />"; echo "<form action='index.php'><br>"; echo "<input type='submit' value='Dare to play again?'>"; } ?> </body> </html> |
The code in GitHub.
That’s all folks! 🙂