Skip to content

Commit

Permalink
feat: implement game GCD
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 3, 2023
1 parent dd39e90 commit 00944ed
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ brain-even:
./bin/brain-even

brain-calc:
./bin/brain-calc
./bin/brain-calc

brain-gcd:
./bin/brain-gcd
9 changes: 9 additions & 0 deletions bin/brain-gcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env php
<?php

use function BrainGames\Engine\runBrainGame;

require_once __DIR__ . '/../src/bootstrap.php';

$module = 'GcdGame';
runBrainGame($module);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
}
],
"require": {
"wp-cli/php-cli-tools": "^0.11.19"
"wp-cli/php-cli-tools": "^0.11.19",
"ext-gmp": "*"
},
"bin": [
"bin/brain-games",
Expand Down
40 changes: 40 additions & 0 deletions src/Games/GcdGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace BrainGames\Games\EvenGame;

use Exception;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Find the greatest common divisor of given numbers.';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler();

return $module;
};

/**
* Returns a question and its expected answer.
*
* - 0: The question (string)
* - 1: The expected answer (string)
*
* @return array{
* 0: string,
* 1: string
* }
* @throws Exception
*/
function questionHandler(): array
{
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);

$expectedAnswer = gmp_gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
}

0 comments on commit 00944ed

Please sign in to comment.