Tur-4000/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\Game\play;

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

function runGcdGame()
{
    $makeGameData = function () {
        $num1 = rand(1, 99);
        $num2 = rand(1, 99);

        $question = "$num1 $num2";

        $correctAnswer = getGcd($num1, $num2);

        return [$correctAnswer, $question];
    };

    play($makeGameData, INSTRUCTION);
}

function getGcd($a, $b)
{
    return ($b > 0) ? getGcd($b, $a % $b) : $a;
}