From ad996ed11001f855ab3eb3a3546f8539082a804a Mon Sep 17 00:00:00 2001 From: gkdev Date: Wed, 6 Sep 2023 22:58:41 +0300 Subject: [PATCH] refactor: simplify architecture --- composer.json | 1 - src/Engine.php | 41 +++++++++++++++++-------- src/Games/CalcGame.php | 58 +++++++++++++++++++---------------- src/Games/EvenGame.php | 24 +++++++-------- src/Games/GcdGame.php | 37 +++++++++++----------- src/Games/PrimeGame.php | 56 ++++++++++++++++----------------- src/Games/ProgressionGame.php | 38 +++++++++++------------ src/Utils/ConfigUtils.php | 19 ------------ src/Utils/ModuleUtils.php | 18 +++-------- 9 files changed, 142 insertions(+), 150 deletions(-) delete mode 100644 src/Utils/ConfigUtils.php diff --git a/composer.json b/composer.json index 8f0d80e..1d42167 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,6 @@ "files": [ "src/Engine.php", "src/Cli.php", - "src/Utils/ConfigUtils.php", "src/Utils/ModuleUtils.php" ] }, diff --git a/src/Engine.php b/src/Engine.php index 57906ce..cbe4222 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -2,46 +2,61 @@ namespace BrainGames\Engine; +use JsonException; use RuntimeException; -use function BrainGames\Utils\ConfigUtils\loadConfigurations; -use function BrainGames\Utils\ConfigUtils\loadModule; use function BrainGames\Utils\ModuleUtils\getQuestionAnswerPairHandler; use function BrainGames\Utils\ModuleUtils\getRulesDescription; use function BrainGames\Utils\ModuleUtils\getText; use function BrainGames\Utils\ModuleUtils\getUserName; -use function BrainGames\Utils\ModuleUtils\setTexts; use function BrainGames\Utils\ModuleUtils\setUserName; use function cli\line; use function cli\prompt; +use const BrainGames\Utils\ModuleUtils\LOCATION_TEXTS; + const ATTEMPT_NUMBER = 3; function runBrainGame(string $moduleName): void { - $module = loadGame($moduleName); + $module = buildGame($moduleName); + $module = loadTexts($module); $module = greetUser($module); runGame($module); } -function loadGame($moduleName): array +function loadTexts($module): array { - $moduleFile = __DIR__ . '/Games/' . $moduleName . '.php'; - if (!file_exists($moduleFile)) { - throw new RuntimeException("Module $moduleFile not found"); - } - $module = loadModule($moduleFile); - $textsFile = __DIR__ . '/texts.json'; if (!file_exists($textsFile)) { throw new RuntimeException("Config $textsFile not found"); } - $texts = loadConfigurations($textsFile); - $module = setTexts($module, $texts); + try { + $texts = json_decode(file_get_contents($textsFile), true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException $e) { + } + + if (empty($texts)) { + throw new RuntimeException("Config $textsFile wasn't loaded"); + } + + $module[LOCATION_TEXTS] = $texts; return $module; } +function buildGame($moduleName): array +{ + $module = []; + $moduleFile = __DIR__ . '/Games/' . $moduleName . '.php'; + if (!file_exists($moduleFile)) { + throw new RuntimeException("Module $moduleFile not found"); + } + + require_once $moduleFile; + return "BrainGames\Games\\$moduleName\loader"($module); +} + function greetUser(array $module): array { line(getText($module, 'dialogs.welcome')); diff --git a/src/Games/CalcGame.php b/src/Games/CalcGame.php index be17688..7be91ed 100644 --- a/src/Games/CalcGame.php +++ b/src/Games/CalcGame.php @@ -1,35 +1,39 @@ handler(); + return $module; +} - $question = "$firstNum $operand $secondNum"; - return [$question, $expectedAnswer]; - }; +function handler(): array +{ + $operators = ['*', '+', '-']; + $firstNum = random_int(1, 10); + $secondNum = random_int(1, 10); + $operator = $operators[random_int(0, count($operators) - 1)]; + switch ($operator) { + case '*': + $expectedAnswer = $firstNum * $secondNum; + break; + case '+': + $expectedAnswer = $firstNum + $secondNum; + break; + case '-': + $expectedAnswer = $firstNum - $secondNum; + break; + default: + throw new RuntimeException('Incorrect operator'); + } - return $module; -}; + $question = "$firstNum $operator $secondNum"; + return [$question, $expectedAnswer]; +} diff --git a/src/Games/EvenGame.php b/src/Games/EvenGame.php index 0a34254..b0b4c73 100644 --- a/src/Games/EvenGame.php +++ b/src/Games/EvenGame.php @@ -3,18 +3,18 @@ namespace BrainGames\Games\EvenGame; 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] = 'Answer "yes" if the number is even, otherwise answer "no".'; - - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () { - $question = random_int(1, 100); - $isEven = $question % 2 === 0; - return [$question, $isEven ? 'yes' : 'no']; - }; - +function loader($module): array +{ + $module[SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".'; + $module[HANDLER_QUESTION] = static fn() => handler(); return $module; -}; +} + +function handler(): array +{ + $question = random_int(1, 100); + $isEven = $question % 2 === 0; + return [$question, $isEven ? 'yes' : 'no']; +} diff --git a/src/Games/GcdGame.php b/src/Games/GcdGame.php index feb5f18..4e1fbcc 100644 --- a/src/Games/GcdGame.php +++ b/src/Games/GcdGame.php @@ -1,26 +1,27 @@ handler(); + return $module; +} - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () use ($gcd) { - $firstNum = random_int(1, 100); - $secondNum = random_int(1, 100); - $expectedAnswer = $gcd($firstNum, $firstNum); - $question = "$firstNum $secondNum"; - return [$question, $expectedAnswer]; - }; +function handler(): array +{ + $firstNum = random_int(1, 10); + $secondNum = random_int(1, 20); + $expectedAnswer = gcd($firstNum, $firstNum); + $question = "$firstNum $secondNum"; + return [$question, $expectedAnswer]; +} - return $module; -}; +function gcd($a, $b): int +{ + return $b === 0 ? $a : gcd($b, $a % $b); +} diff --git a/src/Games/PrimeGame.php b/src/Games/PrimeGame.php index 72540f1..f182832 100644 --- a/src/Games/PrimeGame.php +++ b/src/Games/PrimeGame.php @@ -1,35 +1,35 @@ handler(); return $module; -}; +} + +function handler(): array +{ + $question = random_int(1, 15); + $isPrime = isPrime($question); + return [$question, $isPrime ? 'yes' : 'no']; +} + +function isPrime($number): bool +{ + if ($number <= 1) { + return false; + } + + for ($i = 2; $i * $i <= $number; $i++) { + if ($number % $i === 0) { + return false; + } + } + + return true; +} diff --git a/src/Games/ProgressionGame.php b/src/Games/ProgressionGame.php index 4d24ce9..df2d93f 100644 --- a/src/Games/ProgressionGame.php +++ b/src/Games/ProgressionGame.php @@ -1,26 +1,26 @@ handler(); return $module; -}; +} + +function handler(): 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]; +} diff --git a/src/Utils/ConfigUtils.php b/src/Utils/ConfigUtils.php deleted file mode 100644 index da6bc0a..0000000 --- a/src/Utils/ConfigUtils.php +++ /dev/null @@ -1,19 +0,0 @@ -