Skip to content

Commit

Permalink
add brain-gcd
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniillGolovin committed May 12, 2024
1 parent 2e2c9df commit b74cbcb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ brain-even:
brain-calc:
./bin/brain-calc

brain-gcd:
./bin/brain-gcd

validate:
composer validate

Expand Down
14 changes: 14 additions & 0 deletions bin/brain-gcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php

<?php

$autoloadPath1 = __DIR__ . '/../../../autoload.php';
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';

if (file_exists($autoloadPath1)) {
require_once $autoloadPath1;
} else {
require_once $autoloadPath2;
}

Brain\Games\Gcd\play();
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"bin": [
"bin/brain-games",
"bin/brain-even",
"bin/brain-calc"
"bin/brain-calc",
"bin/brain-gcd"
],
"description": "Brain game",
"type": "project",
Expand All @@ -19,7 +20,8 @@
"src/Cli.php",
"src/Engine.php",
"src/Games/Even.php",
"src/Games/Calc.php"
"src/Games/Calc.php",
"src/Games/Gcd.php"
]
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

function gameLogic($gameConditionsEven, $generateGameData)
{
line("Welcome to the Brain Games! \n");
line($gameConditionsEven);
line("Welcome to the Brain Games!");
$name = prompt("May I have your name?");
line("Hello, {$name}!");
line($gameConditionsEven);

for ($i = 1; $i <= CORRECT_ANSWER; $i += 1) {
[$question, $answer] = $generateGameData();
Expand Down
39 changes: 39 additions & 0 deletions src/Games/Gcd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Brain\Games\Gcd;

use function Brain\Engine\gameLogic;
use function cli\line;
use function cli\prompt;

const DESCRIPTION = 'Find the greatest common divisor of given numbers.';
const RANDOM_MIN = 1;
const RANDOM_MAX = 20;

function gcd($a, $b)
{
if ($b === 0) {
return abs($a);
}
return gcd($b, $a % $b);
}

function getRandomNumber()
{
return rand(RANDOM_MIN, RANDOM_MAX);
}

function play()
{
$generateGameData = function () {
$firstNumber = getRandomNumber();
$secondNumber = getRandomNumber();
$question = "{$firstNumber} {$secondNumber}";
$answer = (string)(gcd($firstNumber, $secondNumber));

return [$question, $answer];
};

gameLogic(DESCRIPTION, $generateGameData);
return;
}

0 comments on commit b74cbcb

Please sign in to comment.