Skip to content

Commit

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

brain-gcd:
./bin/brain-gcd
./bin/brain-gcd

brain-progression:
./bin/brain-progression
9 changes: 9 additions & 0 deletions bin/brain-progression
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 = 'ProgressionGame';
runBrainGame($module);
43 changes: 43 additions & 0 deletions src/Games/ProgressionGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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] = 'What number is missing in the progression?';

$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
{
$lengthProgression = 10;
$step = random_int(2, 10);
$shift = random_int(1, 10);
$progression = range($shift, $shift + (($lengthProgression - 1) * $step), $step);
$guessIndex = random_int(0, 8);
$expectedAnswer = $progression[$guessIndex];
$progression[$guessIndex] = '..';
$question = implode(' ', $progression);
return [$question, $expectedAnswer];
}

0 comments on commit aab2663

Please sign in to comment.