Final Class Yiisoft\Yii\Console\Command\Game
| Inheritance | Yiisoft\Yii\Console\Command\Game » Symfony\Component\Console\Command\Command |
|---|
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| execute() | Yiisoft\Yii\Console\Command\Game |
Method Details
| 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!');
}
}
}
Signup or Login in order to comment.