Created
February 6, 2022 13:00
-
-
Save osteel/dac6ef1e3e96f3956ab98faf68777070 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 | |
namespace Osteel\PhpCliDemo\Commands; | |
use Osteel\PhpCliDemo\Services\InputOutput; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class Play extends Command | |
{ | |
/** | |
* The name of the command (the part after "bin/demo"). | |
* | |
* @var string | |
*/ | |
protected static $defaultName = 'play'; | |
/** | |
* The command description shown when running "php bin/demo list". | |
* | |
* @var string | |
*/ | |
protected static $defaultDescription = 'Play the game!'; | |
/** | |
* Execute the command | |
* | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* @return int 0 if everything went fine, or an exit code. | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$term1 = rand(1, 10); | |
$term2 = rand(1, 10); | |
$result = $term1 + $term2; | |
$io = new InputOutput($input, $output); | |
$answer = (int) $io->question(sprintf('What is %s + %s?', $term1, $term2)); | |
if ($answer === $result) { | |
$io->right('Well done!'); | |
} else { | |
$io->wrong(sprintf('Aww, so close. The answer was %s', $result)); | |
} | |
if ($io->confirm('Play again?')) { | |
return $this->execute($input, $output); | |
} | |
return Command::SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment