Skip to content

Instantly share code, notes, and snippets.

@lucablackwell
Last active November 29, 2022 10:08
Show Gist options
  • Save lucablackwell/299162144bbca4d1655e66b107719e6b to your computer and use it in GitHub Desktop.
Save lucablackwell/299162144bbca4d1655e66b107719e6b to your computer and use it in GitHub Desktop.
<?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