Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * This code is just one of many possible solutions to the Weekly Challenge posed by Alex in his email
- * dated 13/Mar/2023. The Challenge was to create an interactive script that generates a random number
- * and asks the user to guess that number.
- *
- * If you are interested in improving your PHP skills, sign up for Alex's newsletter at
- * https://alexwebdevelop.com and join Alex PHP café at https://www.facebook.com/groups/289777711557686
- */
- declare(strict_types=1);
- /*
- * Check environment
- */
- PHP_SAPI === 'cli'
- or throw new RuntimeException('Must be run from the command line');
- extension_loaded('readline')
- or throw new RuntimeException("PHP 'readline' extension not loaded");
- /*
- * Validate the command line arguments
- */
- if ($argc == 3) {
- $max_random = validate_int($argv[1], 2, 65536);
- $max_attempts = validate_int($argv[2], 1, 32);
- }
- if (
- $argc != 3
- || $max_random === false
- || $max_attempts === false
- ) {
- /* arguments are not defined or invalid - display usage information */
- $script_name = basename(__FILE__);
- echo <<<USAGE
- Usage: $script_name max_random max_attempts
- Generates a random number and asks the user to guess that number.
- max_random:
- the maximum random value that can be generated, an integer between 2 and 65536
- max_attempts:
- the maximum number of attempts the user can make, an integer between 1 and 32
- USAGE;
- exit(254); // indicate usage displayed
- }
- $eot = 1; // default end of task code to indicate failure
- /*
- * Generate a random number
- */
- $random = random_int(1, $max_random);
- print_line(PHP_EOL . "I've thought of a number between %d and %d" . PHP_EOL, 1, $max_random);
- /*
- * Loop prompting the user to enter a guess
- */
- $attempts = 0;
- while (true) {
- if (
- ($input = readline('Enter guess: ')) === false
- || trim($input) === ''
- ) {
- break; // no input - terminate loop
- }
- /* validate user input */
- if (($guess = validate_int($input, 1, $max_random)) !== false) {
- switch ($guess <=> $random) { // use the spaceship operator to compare
- case 0:
- print_line('Congratulations!');
- $eot = 0; // set end of task code to indicate success
- break 2; // exit switch and while
- case -1:
- $msg = 'Too low';
- break;
- case 1:
- $msg = 'Too high';
- break;
- }
- /* only count valid attempts */
- if (++$attempts >= $max_attempts) {
- print_line('Too many attempts, the number was %d.', $random);
- break;
- }
- } else {
- $msg = sprintf('Enter an integer between 1 and %d', $max_random);
- }
- print_line('%s, try again.', $msg);
- }
- exit($eot); // indicate whether successful or not
- /*
- * Output a formatted string appending the correct end of line
- *
- * Example of a variable-length argument list using the variadic ('splat') operator
- */
- function print_line(string $format, mixed ...$values): int
- {
- return printf($format . PHP_EOL, ...$values);
- }
- /*
- * Validate a string as an integer within a range
- */
- function validate_int(string $value, int $min, int $max): int|false
- {
- return filter_var($value, FILTER_VALIDATE_INT,
- ['options' => ['min_range' => $min, 'max_range' => $max]]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement