Number Guessing Game
Gameplay
Player guesses numbers between 1 and 100, and is prompted to enter lower or higher numbers until the correct number is guessed. The scoring is based on the number of incorrect guesses the user entered before winning.
Scoring
Once the user guesses the correct number and the game is over, their score is stored in a database.
A page displaying the top 10 scores for the game is shown upon game completion, and the player can choose to play again by clicking ‘New Game’. If the user scored among the top 10, their name is highlighted when this page is displayed.
Logic
Coded in PHP, this game uses random number generation to generate a target guess.
` $_SESSION[“target”] = rand(1, 100);`
Gameplay validation functionality is achieved through the use of session variables, and storing and referencing those values in a table in a MySQL database over a remote connection.
$sql = "INSERT INTO Games (player, target, score)
VALUES ('" . $_SESSION["username"] . "', '"
. $_SESSION["target"] . "', '"
. $_SESSION["score"] . "')";
$sqlquery = $conn->query($sql);
…
if($_SESSION["target"] > $_SESSION["guess"]) {
// higher
$_SESSION["prompt"] = "Higher";
} else if($_SESSION["target"] < $_SESSION["guess"]) {
// lower
$_SESSION["prompt"] = "Lower";
}