-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e2c9df
commit b74cbcb
Showing
5 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ brain-even: | |
brain-calc: | ||
./bin/brain-calc | ||
|
||
brain-gcd: | ||
./bin/brain-gcd | ||
|
||
validate: | ||
composer validate | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |