0 follower

Final Class Yiisoft\Yii\Console\Command\Game

InheritanceYiisoft\Yii\Console\Command\Game » Symfony\Component\Console\Command\Command

Protected Methods

Hide inherited methods

Method Description Defined By
execute() Yiisoft\Yii\Console\Command\Game

Method Details

Hide inherited methods

execute() protected method

protected integer execute ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface

                protected function execute(InputInterface $input, OutputInterface $output): int
{
    $steps = 1;
    $number = random_int(0, 100);
    $io = new SymfonyStyle($input, $output);
    $io->title('Welcome to the Guessing Game!');
    /** @var QuestionHelper $helper */
    $helper = $this->getHelper('question');
    $question = new Question('Please enter a number between 0 and 100: ');
    while (true) {
        $answer = (int) $helper->ask($input, $output, $question);
        if ($answer === $number) {
            $io->success('You win! You guessed the number in ' . $steps . ' steps.');
            return 0;
        }
        $steps++;
        if ($answer > $number) {
            $io->warning('Too high!');
        } else {
            $io->warning('Too low!');
        }
    }
}