Malcom1986/php-project-lvl1

View on GitHub
src/games/Gcd.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Games\Gcd;

use function BrainGames\Cli\run;

const GAME_RULE = 'Find the greatest common divisor of given numbers.';

function gcd($num1, $num2)
{
    if ($num2 === 0) {
        return $num1;
    }
    return gcd($num2, $num1 % $num2);
}

function runGame()
{
    $roundGenerator = function () {
        $questionPart1 = mt_rand(0, 100);
        $questionPart2 = mt_rand(0, 100);
        $answer = gcd($questionPart1, $questionPart2);
        return [
            "question" => "{$questionPart1}  {$questionPart2}",
            "answer" => $answer
        ];
    };

    run(GAME_RULE, $roundGenerator);
}