Last active
May 1, 2023 09:08
-
-
Save stungeye/d6c0f179a9295dfe54e8 to your computer and use it in GitHub Desktop.
Number Guessing Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('number_game_functions.php'); | |
$message_for_user = "I've picked a random number between " . | |
RANDOM_NUMBER_MINIMUM . " and " . | |
RANDOM_NUMBER_MAXIMUM . ". Can you guess it?"; | |
$user_has_guessed_correctly = user_guessed_correctly(); | |
if (user_submitted_a_guess()) { | |
increase_the_user_guess_count(); | |
if ($user_has_guessed_correctly) { | |
$message_for_user = "You got it! It took you " . user_guess_count() . " attempts. Guess again?"; | |
} else if (user_has_guessed_too_high()) { | |
$message_for_user = "Sorry, guess again but lower."; | |
} else if (user_has_guessed_too_low()) { | |
$message_for_user = "Sorry, guess again but higher."; | |
} | |
} | |
if (secret_number_has_not_yet_been_set() || user_has_requested_a_reset() || $user_has_guessed_correctly) { | |
reset_secret_number(); | |
reset_user_guess_count(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Number Guessing Game</title> | |
</head> | |
<body> | |
<h1>Guessing Game</h1> | |
<p><?= $message_for_user ?></p> | |
<form method="post"> | |
<label for="user_guess">Your Guess</label> | |
<input id="user_guess" name="user_guess"> | |
<input type="submit" name="guess" value="Guess"> | |
<input type="submit" name="reset" value="Reset"> | |
</form> | |
<!-- Uncomment This When Testing: | |
<p><strong>Secret Number:</strong> <?= secret_number() ?></p> | |
--> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
define("RANDOM_NUMBER_MAXIMUM", 100); | |
define("RANDOM_NUMBER_MINIMUM", 1); | |
function user_submitted_a_guess() { | |
return isset($_POST['guess']); | |
} | |
function user_guessed_correctly() { | |
return user_guess() == secret_number(); | |
} | |
function user_has_guessed_too_high() { | |
return user_guess() > secret_number(); | |
} | |
function user_has_guessed_too_low() { | |
return user_guess() < secret_number(); | |
} | |
function user_has_requested_a_reset() { | |
return isset($_POST['reset']); | |
} | |
// conditional_fetch prevents errors for unset keys. | |
function conditional_fetch($hash, $key) { | |
if (isset($hash[$key])) { | |
return $hash[$key]; | |
} else { | |
return false; | |
} | |
} | |
function user_guess() { | |
return conditional_fetch($_POST, 'user_guess'); | |
} | |
function secret_number() { | |
return conditional_fetch($_SESSION, 'secret_number'); | |
} | |
function secret_number_has_not_yet_been_set() { | |
return !isset($_SESSION['secret_number']); | |
} | |
function reset_secret_number() { | |
$_SESSION['secret_number'] = rand(RANDOM_NUMBER_MINIMUM, RANDOM_NUMBER_MAXIMUM); | |
} | |
function reset_user_guess_count() { | |
$_SESSION['guess_count'] = 0; | |
} | |
function increase_the_user_guess_count() { | |
$_SESSION['guess_count']++; | |
} | |
function user_guess_count() { | |
return conditional_fetch($_SESSION, 'guess_count'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have Missing code for index?