From 0380b2c5afd26fd866fac48a024036c4604e0506 Mon Sep 17 00:00:00 2001 From: egorskov Date: Sun, 27 Oct 2024 18:25:11 +0500 Subject: [PATCH] some changes --- src/Engine.php | 6 +++--- src/Games/Calc.php | 21 +++++++++------------ src/Games/Even.php | 8 ++++---- src/Games/GCD.php | 10 +++++----- src/Games/Prime.php | 8 ++++---- src/Games/Progression.php | 8 ++++---- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/Engine.php b/src/Engine.php index 8b6371e..7f7855f 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -10,13 +10,13 @@ const MIN_NUM = 1; const MAX_NUM = 100; -function communication(array $answers, string $game): void +function communicate(array $answers, string $game): void { $name = greeting(); line("$game"); foreach ($answers as $key => $value) { $answer = prompt("Question: $key\nYou answer"); - if ($answer != $value) { + if ($answer !== $value) { line("'$answer' is wrong answer ;( Correct answer: '$value') \nLet's try again, $name!"); return; } else { @@ -26,7 +26,7 @@ function communication(array $answers, string $game): void line("Congratulations, $name!"); } -function arrayNumbers(): array +function getArrayNumbers(): array { $numbers = []; for ($i = 1; $i <= ROUND; $i++) { diff --git a/src/Games/Calc.php b/src/Games/Calc.php index 147894c..8976779 100644 --- a/src/Games/Calc.php +++ b/src/Games/Calc.php @@ -2,7 +2,7 @@ namespace BrainGames\Games\Calc; -use function BrainGames\Engine\communication; +use function BrainGames\Engine\communicate; use const BrainGames\Engine\ROUND; use const BrainGames\Engine\MIN_NUM; @@ -19,21 +19,18 @@ function runCalc(): void $operator = $signs[array_rand($signs)]; $answers["$first $operator $second"] = calculate($first, $second, $operator); } - communication($answers, $game); + communicate($answers, $game); } -function calculate(int $num1, int $num2, string $operation): int +function calculate($first, $second, $operator): string { - $result = 0; - switch ($operation) { + switch ($operator) { case '+': - $result = $num1 + $num2; - break; + return $first + $second; case '-': - $result = $num1 - $num2; - break; + return $first - $second; case '*': - $result = $num1 * $num2; - break; + return $first * $second; + default: + break; } - return $result; } diff --git a/src/Games/Even.php b/src/Games/Even.php index f502fc6..2daccd2 100644 --- a/src/Games/Even.php +++ b/src/Games/Even.php @@ -2,21 +2,21 @@ namespace BrainGames\Games\Even; -use function BrainGames\Engine\communication; -use function BrainGames\Engine\arrayNumbers; +use function BrainGames\Engine\communicate; +use function BrainGames\Engine\getArrayNumbers; function runEven() { $game = 'Answer "yes" if the number is even, otherwise answer "no".'; $answers = []; - foreach (arrayNumbers() as $number) { + foreach (getArrayNumbers() as $number) { if (isEven($number)) { $answers[$number] = 'yes'; } else { $answers[$number] = 'no'; } } - communication($answers, $game); + communicate($answers, $game); } function isEven(int $num): bool diff --git a/src/Games/GCD.php b/src/Games/GCD.php index f8f67c5..596c58b 100644 --- a/src/Games/GCD.php +++ b/src/Games/GCD.php @@ -1,8 +1,8 @@ = $y) { diff --git a/src/Games/Prime.php b/src/Games/Prime.php index 206df29..9fa2df7 100644 --- a/src/Games/Prime.php +++ b/src/Games/Prime.php @@ -2,21 +2,21 @@ namespace BrainGames\Games\Prime; -use function BrainGames\Engine\arrayNumbers; -use function BrainGames\Engine\communication; +use function BrainGames\Engine\getArrayNumbers; +use function BrainGames\Engine\communicate; function runPrime(): void { $game = 'Answer "yes" if given number is prime. Otherwise answer "no".'; $answers = []; - foreach (arrayNumbers() as $number) { + foreach (getArrayNumbers() as $number) { if (isPrime($number)) { $answers[$number] = 'yes'; } else { $answers[$number] = 'no'; } } - communication($answers, $game); + communicate($answers, $game); } function isPrime(int $num): bool diff --git a/src/Games/Progression.php b/src/Games/Progression.php index 5493b04..ae06fc0 100644 --- a/src/Games/Progression.php +++ b/src/Games/Progression.php @@ -2,7 +2,7 @@ namespace BrainGames\Games\Progression; -use function BrainGames\Engine\communication; +use function BrainGames\Engine\communicate; use const BrainGames\Engine\ROUND; use const BrainGames\Engine\MIN_NUM; @@ -13,16 +13,16 @@ function runProgression(): void $game = 'What number is missing in the progression?'; $answers = []; for ($i = 1; $i <= ROUND; $i++) { - $numbers = slicer(); + $numbers = getSlice(); $randomSym = mt_rand(0, (count($numbers) - 1)); $correctAnswer = $numbers[$randomSym]; $numbers[$randomSym] = '..'; $numbersString = implode(' ', $numbers); $answers[$numbersString] = "$correctAnswer"; } - communication($answers, $game); + communicate($answers, $game); } -function slicer(): array +function getSlice(): array { $num = []; $num[] = 0;