Skip to content

Commit 3c000dd

Browse files
committed
8step + lint PSR
1 parent 1bb01fe commit 3c000dd

File tree

10 files changed

+100
-15
lines changed

10 files changed

+100
-15
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ brain-calc:
1313
brain-gcd:
1414
php ./bin/brain-gcd
1515

16+
brain-progression:
17+
php ./bin/brain-progression
18+
1619
validate:
1720
composer validate
1821

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Brain-Calc - https://asciinema.org/a/cKn78eDiU1h5MTCMr1183kawd
44

55
Brain-Gcd - https://asciinema.org/a/wWwYQWsxsikakJawzl5Wwxe4Y
66

7+
Result Url: https://asciinema.org/a/sNPCT391AYUgrvR8Oy4ASUkYk
8+
79
### Hexlet tests and linter status:
810

911
[![SonarQube Cloud](https://sonarcloud.io/images/project_badges/sonarcloud-light.svg)](https://sonarcloud.io/summary/new_code?id=webDevWay_php-project-lvl1)

bin/brain-calc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ if (file_exists($autoloadPath1)) {
1414
}
1515
//получим имя пользователя
1616
$userName = getUserName();
17-
//
17+
//запуск игры
1818
checkAnswer($count = 3, $userName, $path="calc");

bin/brain-even

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use function BrainGames\BrainEven\getUserName;
4+
//use function BrainGames\BrainEven\getUserName;
5+
use function BrainGames\Engine\getUserName;
6+
use function BrainGames\Engine\checkAnswer;
57

68
$autoloadPath1 = __DIR__ . '/../../../autoload.php';
79
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';
@@ -12,4 +14,6 @@ if (file_exists($autoloadPath1)) {
1214
require_once $autoloadPath2;
1315
}
1416
//получим имя пользователя
15-
getUserName();
17+
$userName = getUserName();
18+
//запуск игры
19+
checkAnswer($count = 3, $userName, $path="even");

bin/brain-gcd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use function BrainGames\Engine\getUserName;
55
use function BrainGames\Engine\checkAnswer;
6-
use function BrainGames\BrainGcd\getGcd;
6+
77

88
$autoloadPath1 = __DIR__ . '/../../../autoload.php';
99
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';
@@ -15,6 +15,6 @@ if (file_exists($autoloadPath1)) {
1515
}
1616
//получим имя пользователя
1717
$userName = getUserName();
18-
//
18+
//запуск игры
1919
checkAnswer($count = 3, $userName, $path="gcd");
2020

bin/brain-progression

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use function BrainGames\Engine\getUserName;
5+
use function BrainGames\Engine\checkAnswer;
6+
7+
8+
$autoloadPath1 = __DIR__ . '/../../../autoload.php';
9+
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';
10+
11+
if (file_exists($autoloadPath1)) {
12+
require_once $autoloadPath1;
13+
} else {
14+
require_once $autoloadPath2;
15+
}
16+
//получим имя пользователя
17+
$userName = getUserName();
18+
//Запустим игру
19+
checkAnswer($count = 3, $userName, $path="progression");
20+

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
"src/Games/BrainEven.php",
1212
"src/Games/BrainCalc.php",
1313
"src/Engine.php",
14-
"src/Games/BrainGcd.php"
14+
"src/Games/BrainGcd.php",
15+
"src/Games/BrainProgression.php"
1516
]
1617
},
1718
"bin": [
1819
"bin/brain-games",
1920
"bin/brain-even",
2021
"bin/brain-calc",
21-
"bin/brain-gcd"
22-
22+
"bin/brain-gcd",
23+
"bin/brain-progression"
2324
],
2425
"authors": [
2526
{

src/Engine.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use function cli\line;
66
use function cli\prompt;
77
use function BrainGames\BrainGcd\getGcd;
8+
use function BrainGames\BrainProgression\generateProgression;
9+
use function BrainGames\BrainProgression\hideElement;
810

911
//---Приветствуем пользователя, получаем имя
1012
function getUserName()
@@ -24,22 +26,22 @@ function checkAnswer($count, $name, $path)
2426
$num = random_int(1, 100);
2527
$num2 = random_int(1, 10);
2628

27-
//---Логика игр
28-
//Подлючим логику в зависимости от игры
29+
//--- Логика игр
30+
//-- Подлючим логику в зависимости от выбора игры
2931
switch ($filepath) {
30-
//Игра - Чётные числа
32+
//-- Игра - "Проверка на чётность"
3133
case "even":
3234
line('Answer "yes" if the number is even, otherwise answer "no".');
3335
$answer = prompt("Question:", $num);
3436
$isEven = $num % 2 === 0 ? true : false;
3537
$expectedAnswer = $isEven ? 'yes' : 'no';
3638
break;
37-
//Игра - Калькулятор
39+
//-- Игра - "Калькулятор"
3840
case "calc":
3941
line('What is the result of the expression?');
4042
$operation = ["+", "-", "*"];
4143
$thisOperation = $operation[random_int(0, 2)];
42-
//Перебрать рандомные операторы
44+
//-- Перебрать рандомные операторы --
4345
switch ($thisOperation) {
4446
case "*":
4547
$expectedAnswer = $num * $num2;
@@ -56,13 +58,20 @@ function checkAnswer($count, $name, $path)
5658
$answer = prompt("Question:", "{$num} {$thisOperation} {$num2}");
5759
$answer = is_string($answer) ? $answer : (int)$answer;
5860
break;
59-
//Игра - Общий делитель
61+
//-- Игра - Общий делитель -- "НОД"
6062
case "gcd":
6163
$expectedAnswer = getGcd($num, $num2);
62-
print_r($expectedAnswer);
6364
line('Find the greatest common divisor of given numbers.');
6465
$answer = prompt("Question:", "{$num} {$num2}");
6566
break;
67+
//-- Игра - "Арифметическая прогрессия"
68+
case "progression":
69+
$progression = generateProgression();
70+
$hiddenElement = hideElement($progression);
71+
$expectedAnswer = $hiddenElement["hidden_value"];
72+
line('What number is missing in the progression?');
73+
$answer = prompt("Question:", implode(" ", $hiddenElement["progression"]));
74+
break;
6675
default:
6776
return line("This Game is still in production :)");
6877
}

src/Games/BrainGcd.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BrainGames\BrainGcd;
4+
5+
//вычислим общий делитель
6+
function getGcd($num1, $num2)
7+
{
8+
$a = $num1;
9+
$b = $num2;
10+
while ($b != 0) {
11+
$temp = $b;
12+
$b = $a % $b;
13+
$a = $temp;
14+
}
15+
return abs($a);
16+
}

src/Games/BrainProgression.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BrainGames\BrainProgression;
4+
5+
//-- Генерируем прогрессию
6+
function generateProgression($length = 10, $minStep = 2, $maxStep = 5, $minStart = 1, $maxStart = 20)
7+
{
8+
$start = rand($minStart, $maxStart);
9+
$step = rand($minStep, $maxStep);
10+
11+
$progression = [];
12+
for ($i = 0; $i < $length; $i++) {
13+
$progression[] = $start + $i * $step;
14+
}
15+
16+
return $progression;
17+
}
18+
//-- Скрыть элемент
19+
function hideElement($progression)
20+
{
21+
$hiddenIndex = rand(0, count($progression) - 1);
22+
$hiddenValue = $progression[$hiddenIndex];
23+
$progression[$hiddenIndex] = '..';
24+
25+
return [
26+
'progression' => $progression,
27+
'hidden_index' => $hiddenIndex,
28+
'hidden_value' => $hiddenValue
29+
];
30+
}

0 commit comments

Comments
 (0)