PHP – Simple guessing game

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:
page2starting_page

page3win

 

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:

play.php:

The code in GitHub.

That’s all folks! 🙂

About

VBA Developer

Tagged with: , ,