|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BrainGames\Progression; |
| 4 | + |
| 5 | +use function BrainGames\Engine\startGame; |
| 6 | + |
| 7 | + //-- Игра - "Арифметическая прогрессия" |
| 8 | +function initGameData(): void |
| 9 | +{ |
| 10 | + $gameParams = []; |
| 11 | + $gameParams["rules"] = 'What number is missing in the progression?'; |
| 12 | + $count = 3; |
| 13 | + for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) { |
| 14 | + $progression = generateProgression(); |
| 15 | + $hiddenElement = hideElement($progression); |
| 16 | + $gameParams["expectedAnswer"][] = $hiddenElement["hidden_value"]; |
| 17 | + $gameParams["questions"][] = implode(" ", $hiddenElement["progression"]); |
| 18 | + } |
| 19 | + |
| 20 | + startGame($gameParams); |
| 21 | +} |
| 22 | +//-- Генерируем прогрессию |
| 23 | +function generateProgression( |
| 24 | + int $length = 10, |
| 25 | + int $minStep = 2, |
| 26 | + int $maxStep = 5, |
| 27 | + int $minStart = 1, |
| 28 | + int $maxStart = 20 |
| 29 | +): array { |
| 30 | + $start = rand($minStart, $maxStart); |
| 31 | + $step = rand($minStep, $maxStep); |
| 32 | + |
| 33 | + $progression = []; |
| 34 | + for ($i = 0; $i < $length; $i++) { |
| 35 | + $progression[] = $start + $i * $step; |
| 36 | + } |
| 37 | + |
| 38 | + return $progression; |
| 39 | +} |
| 40 | +//-- Скрыть элемент |
| 41 | +function hideElement(array $progression): array |
| 42 | +{ |
| 43 | + $hiddenIndex = rand(0, count($progression) - 1); |
| 44 | + $hiddenValue = $progression[$hiddenIndex]; |
| 45 | + $progression[$hiddenIndex] = '..'; |
| 46 | + |
| 47 | + return [ |
| 48 | + 'progression' => $progression, |
| 49 | + 'hidden_index' => $hiddenIndex, |
| 50 | + 'hidden_value' => $hiddenValue |
| 51 | + ]; |
| 52 | +} |
0 commit comments