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 381839c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
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();
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"bin/brain-games",
"bin/brain-even",
"bin/brain-calc",
"bin/brain-gcd"
"bin/brain-gcd",
"bin/brain-progression"
],
"description": "Brain game",
"type": "project",
Expand All @@ -21,7 +22,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 381839c

Please sign in to comment.