Last active
November 29, 2022 10:08
-
-
Save lucablackwell/299162144bbca4d1655e66b107719e6b to your computer and use it in GitHub Desktop.
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 | |
# Colours | |
function green($text) { | |
return "\e[0;32m$text\e[0m"; | |
} | |
function yellow($text) { | |
return "\e[0;33m$text\e[0m"; | |
} | |
function red($text) { | |
return "\e[0;31m$text\e[0m"; | |
} | |
function white_bold($text) { | |
return "\e[1;37m$text\e[0m"; | |
} | |
$limit = 10; | |
# Assign the script's choices to an array | |
$choices = []; | |
for ($i = 1; $i < $limit ; $i++) { | |
$choices[] = $i; | |
} | |
# Unset the first and last number | |
unset($choices[0]); | |
unset($choices[$limit-2]); | |
$streak = true; | |
# While the user has a streak | |
while ($streak == true) { | |
# Pick a random number from the choices | |
$new = $choices[array_rand($choices)]; | |
$old = $new; | |
# Ensure the new and old numbers don't match | |
while ($new == $old) { | |
$old = $choices[array_rand($choices)]; | |
} | |
# If the new number is higher than the old | |
if ($old < $new) { | |
$correct = 'high'; | |
} else { | |
$correct = 'low'; | |
} | |
# Colour the user's streak depending on how high it is | |
if ($streak < 4) { | |
echo "Streak is: " . red($streak-1) . "\n"; | |
} elseif ($streak >= 4 && $streak < 6) { | |
echo "Streak is: " . yellow($streak-1) . "\n"; | |
} else { | |
echo "Streak is: " . green($streak-1) . "\n"; | |
} | |
echo "Number is: " . white_bold($old) . " (/" . ($limit-1) . ").\nHigher or lower?\n"; | |
$input = readline('> '); | |
$sanitised = false; | |
while (!$sanitised) { | |
# If there are numbers | |
if (preg_match('/[0-9]/', $input)) { | |
echo "No numbers.\n"; | |
$input = readline('> '); | |
# If there are symbols | |
} elseif (preg_match('/[^\p{L}\d\s@#]/u', $input)) { | |
echo "No symbols.\n"; | |
$input = readline('> '); | |
# If there is no input | |
} elseif ($input == null) { | |
echo "Enter 'high' or 'low'.\n"; | |
$input = readline('> '); | |
} else { | |
# If there are just letters (if logic makes this so) | |
if (preg_match('/[a-z]/', $input)) { | |
# If the input is high or low | |
if ($input == 'high' || $input == 'low') { | |
# End the loop | |
$sanitised = true; | |
} else { | |
echo "Enter 'high' or 'low'.\n"; | |
$input = readline('> '); | |
} | |
} | |
} | |
} | |
if ($input == $correct) { | |
echo "Correct!\n"; | |
$streak += 1; | |
} else { | |
echo "Incorrect.\n"; | |
$streak = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment