Rustem-A/php-project-lvl1

View on GitHub
src/Cli.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Cli;

use function cli\line;
use function cli\prompt;

function run($description, callable $foo, $times = 3)
{
    line('Welcome to the Brain 2ames!');
    line($description);
    $name = prompt('May I have your name?');
    line("Hello, %s! \n", $name);

    for ($i = 0; $i < $times; $i++) {
        [$question, $correct] = $foo();
        line("Question: %s", $question);
        $userAnswer = prompt('Your answer ');

        if ($userAnswer == $correct) {
            line("Correct! \n");
        } else {
            line("'$userAnswer' is wrong answer, correct answer was '$correct'.");
            line("Let's try again, %s!", $name);
            return;
        }
    }
    line('Congratulations, %s!', $name);
}