diff --git a/src/Games/CalcGame.php b/src/Games/CalcGame.php index b867a71..be17688 100644 --- a/src/Games/CalcGame.php +++ b/src/Games/CalcGame.php @@ -2,8 +2,6 @@ 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; @@ -12,41 +10,26 @@ return function ($module) { $module[LOCATION_SETTINGS][SETTING_RULES] = 'What is the result of the expression?'; - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler(); + $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () { + $operands = ['*', '+', '-']; + $firstNum = random_int(1, 100); + $secondNum = random_int(1, 100); + $operand = $operands[random_int(0, count($operands) - 1)]; + switch ($operand) { + case '*': + $expectedAnswer = $firstNum * $secondNum; + break; + case '+': + $expectedAnswer = $firstNum + $secondNum; + break; + case '-': + $expectedAnswer = $firstNum - $secondNum; + break; + } + + $question = "$firstNum $operand $secondNum"; + return [$question, $expectedAnswer]; + }; 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 -{ - $operands = ['*', '+', '-']; - $firstNum = random_int(1, 100); - $secondNum = random_int(1, 100); - $operand = $operands[random_int(0, count($operands) - 1)]; - switch ($operand) { - case '*': - $expectedAnswer = $firstNum * $secondNum; - break; - case '+': - $expectedAnswer = $firstNum + $secondNum; - break; - case '-': - $expectedAnswer = $firstNum - $secondNum; - break; - } - - $question = "$firstNum $operand $secondNum"; - return [$question, $expectedAnswer]; -} diff --git a/src/Games/EvenGame.php b/src/Games/EvenGame.php index 9bb1a00..0a34254 100644 --- a/src/Games/EvenGame.php +++ b/src/Games/EvenGame.php @@ -2,8 +2,6 @@ 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; @@ -12,26 +10,11 @@ return function ($module) { $module[LOCATION_SETTINGS][SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".'; - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler(); + $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () { + $question = random_int(1, 100); + $isEven = $question % 2 === 0; + return [$question, $isEven ? 'yes' : 'no']; + }; 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 -{ - $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 76ee7db..c9f276e 100644 --- a/src/Games/GcdGame.php +++ b/src/Games/GcdGame.php @@ -2,8 +2,6 @@ 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; @@ -12,29 +10,14 @@ return function ($module) { $module[LOCATION_SETTINGS][SETTING_RULES] = 'Find the greatest common divisor of given numbers.'; - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler(); + $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () { + $firstNum = random_int(1, 100); + $secondNum = random_int(1, 100); + + $expectedAnswer = gmp_gcd($firstNum, $firstNum); + $question = "$firstNum $secondNum"; + return [$question, $expectedAnswer]; + }; 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 -{ - $firstNum = random_int(1, 100); - $secondNum = random_int(1, 100); - - $expectedAnswer = gmp_gcd($firstNum, $firstNum); - $question = "$firstNum $secondNum"; - return [$question, $expectedAnswer]; -} diff --git a/src/Games/ProgressionGame.php b/src/Games/ProgressionGame.php index 8fd8deb..4d24ce9 100644 --- a/src/Games/ProgressionGame.php +++ b/src/Games/ProgressionGame.php @@ -2,8 +2,6 @@ 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; @@ -12,32 +10,17 @@ return function ($module) { $module[LOCATION_SETTINGS][SETTING_RULES] = 'What number is missing in the progression?'; - $module[LOCATION_HANDLERS][HANDLER_QUESTION] = fn() => questionHandler(); + $module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () { + $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]; + }; 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]; -}