bezrukov/php-project-lvl1

View on GitHub
src/games/gcd.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Cli\games\gcd;

use function BrainGames\Cli\run;

function gcd($a, $b)
{
    return ($a % $b) ? gcd($b, $a % $b) : $b;
}

function getGameData()
{
    $firstOperand = rand(1, 50);
    $secondOperand = rand(1, 50);
    $question = "{$firstOperand} {$secondOperand}";
    $answer = (string) gcd($firstOperand, $secondOperand);

    return [
        $question,
        $answer,
    ];
}

function game()
{
    $describeGame = 'Find the greatest common divisor of given numbers.';

    run(
        $describeGame,
        function () {
            return getGameData();
        }
    );
}