Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Egorskov committed Oct 27, 2024
1 parent 2386497 commit 0380b2c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++) {
Expand Down
21 changes: 9 additions & 12 deletions src/Games/Calc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
8 changes: 4 additions & 4 deletions src/Games/Even.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Games/GCD.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace BrainGames\Games\GCD;
namespace BrainGames\Games\Gcd;

use function BrainGames\Engine\communication;
use function BrainGames\Engine\communicate;

use const BrainGames\Engine\ROUND;
use const BrainGames\Engine\MIN_NUM;
Expand All @@ -15,12 +15,12 @@ function runGcd(): void
for ($i = 1; $i <= ROUND; $i++) {
$first = mt_rand(MIN_NUM, MAX_NUM);
$second = mt_rand(MIN_NUM, MAX_NUM);
$answers["$first $second"] = nod($first, $second);
$answers["$first $second"] = getNod($first, $second);
}
communication($answers, $game);
communicate($answers, $game);
}

function nod(int $x, int $y)
function getNod(int $x, int $y): string
{
while ($x !== 0 && $y !== 0) {
if ($x >= $y) {
Expand Down
8 changes: 4 additions & 4 deletions src/Games/Prime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Games/Progression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 0380b2c

Please sign in to comment.