Skip to content

Commit

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

brain-progression:
./bin/brain-progression

validate:
composer validate

Expand Down
14 changes: 14 additions & 0 deletions bin/brain-progression
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\Progression\play();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"src/Engine.php",
"src/Games/Even.php",
"src/Games/Calc.php",
"src/Games/Gcd.php"
"src/Games/Gcd.php",
"src/Games/Progression.php"
]
},
"scripts": {
Expand Down
45 changes: 45 additions & 0 deletions src/Games/Progression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Brain\Games\Progression;

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

const DESCRIPTION = 'What number is missing in the progression?';
const PROGRESSION_LENGTH = 10;
const RANDOM_MIN = 1;
const RANDOM_MAX = 10;

function generateProgression($start, $step, $progressionLength)
{
$progression = [];
for ($i = 0; $i < $progressionLength; $i += 1) {
$progression[] = $start + $step * $i;
}
return $progression;
}

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

function play()
{
$generateGameData = function () {
$start = getRandomNumber();
$step = getRandomNumber();
$progression = generateProgression($start, $step, PROGRESSION_LENGTH);
$progressionWithHiddenElement = array_slice($progression, 0);
$hiddenElementIndex = rand(0, PROGRESSION_LENGTH - 1);
$progressionWithHiddenElement[$hiddenElementIndex] = '..';
$question = implode(' ', $progressionWithHiddenElement);
$answer = $progression[$hiddenElementIndex];

return [$question, "{$answer}"];
};

gameLogic(DESCRIPTION, $generateGameData);
return;
}

0 comments on commit 307ab18

Please sign in to comment.