Created
September 11, 2023 17:06
-
-
Save me-shaon/10b9164badc98e013cb1f750f065e29a to your computer and use it in GitHub Desktop.
This is a simple CLI app to guess a number, written as a demo for my students
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
#! /usr/bin/env php | |
<?php | |
// Guessing game CLI app | |
$options = getopt('h::', ["min::", "max::"]); | |
if (isset($options['h'])) { | |
printf("This is a guessing game application"); | |
} else { | |
$min = (int) ($options['min'] ?? 1); | |
$max = (int) ($options['max'] ?? 100); | |
$number = rand($min, $max); | |
while(true) { | |
$user_input = (int) readline("Enter a number: "); | |
if ($user_input > $number) { | |
printf("Try a lower number.\n"); | |
} else if ($user_input < $number) { | |
printf("Try a bigger number.\n"); | |
} else { | |
printf("Congrats! You guessed it right!"); | |
break; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment