Skip to content

Commit cc80357

Browse files
committed
project check. Even edition
1 parent 0e1496a commit cc80357

File tree

6 files changed

+79
-69
lines changed

6 files changed

+79
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Welcome to the Brain Games!
1+
<h1>Welcome to the Brain Games!</h1>
22

33
Brain-Even: https://asciinema.org/a/UZBtDobwLPOrNa1r3vp6JYiWS
44

bin/brain-calc

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,6 @@ if (file_exists($autoloadPath1)) {
1212
} else {
1313
require_once $autoloadPath2;
1414
}
15-
//получим имя пользователя
16-
/*$userName = getUserName();
17-
//запуск игры
18-
checkAnswer($count = 3, $userName, $path="calc");
19-
//-- Игра - "Калькулятор"
20-
case "calc":
21-
line('What is the result of the expression?');
22-
$operation = ["+", "-", "*"];
23-
$thisOperation = $operation[random_int(0, 2)];
24-
//-- Перебрать рандомные операторы --
25-
switch ($thisOperation) {
26-
case "*":
27-
$expectedAnswer = $num * $num2;
28-
break;
2915

30-
case "+":
31-
$expectedAnswer = $num + $num2;
32-
break;
3316

34-
case "-":
35-
$expectedAnswer = $num - $num2;
36-
break;
37-
}
38-
$answer = prompt("Question: {$num} {$thisOperation} {$num2}");
39-
$answer = is_string($answer) ? $answer : (int)$answer;
40-
break;
41-
17+
//checkAnswer($count = 3, $userName, $path="calc");

src/Engine.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44

55
use function cli\line;
66
use function cli\prompt;
7-
use function BrainGames\BrainEven\brainEvenGame;
8-
use function BrainGames\BrainGcd\getGcd;
9-
use function BrainGames\BrainProgression\generateProgression;
10-
use function BrainGames\BrainProgression\hideElement;
11-
use function BrainGames\BrainPrime\isPrime;
127

13-
//---Приветствуем пользователя, получаем имя
8+
//---Приветствуем пользователя, получаем имя
149
function getUserName(): string
1510
{
1611
//Приветствуем пользователя
@@ -19,31 +14,37 @@ function getUserName(): string
1914
line("Hello, %s!", $name);
2015
//Вернуть полученное имя
2116
return $name;
17+
}
18+
//---Функция проверки ответов
19+
function checkAnswer(array $gameParams)
20+
{
21+
$name = getUserName();
22+
["rules" => $rules,
23+
"expectedAnswer" => $expectedAnswer,
24+
"questions" => $questions] = $gameParams;
25+
line($rules);
26+
for($i = 0; $i < count($questions); $i++) {
27+
$answer = prompt("Question: {$questions[$i]}");
28+
line("Your answer: {$answer}");
29+
if ($answer != $expectedAnswer[$i]) {
30+
wrongAnswer($name, $answer[$i], $expectedAnswer[$i]);
31+
return;
32+
} else {
33+
line("Correct!");
34+
}
35+
}
36+
trueAnswers($name);
2237
}
2338
//---Функция для неверного ответа
24-
function wrongAnswer(string $name, mixed $answer, mixed $expectedAnswer) {
39+
function wrongAnswer(string $name, mixed $answer, mixed $expectedAnswer)
40+
{
2541
line("{$answer} is wrong answer ;(. Correct answer was '{$expectedAnswer}'.");
2642
line("Let's try again, %s!", $name);
2743
return;
2844
}
2945
//---Функция для верного ответа
30-
function trueAnswer(string $name) {
46+
function trueAnswers(string $name)
47+
{
3148
line("Congratulations, %s!", $name);
3249
return;
33-
}
34-
//---Функция проверки ответов
35-
function checkAnswer(int $counterAnswers, string $name, mixed $answer, mixed $expectedAnswer)
36-
{
37-
line("Your answer: {$answer}");
38-
if ($answer != $expectedAnswer) {
39-
wrongAnswer($name, $answer, $expectedAnswer);
40-
return $counterAnswers = 3;
41-
} else {
42-
line("Correct!");
43-
$counterAnswers++;
44-
if ($counterAnswers == 3) {
45-
trueAnswer($name);
46-
}
47-
}
48-
return $counterAnswers;
4950
}

src/Games/BrainCalc.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
<?php
2+
3+
namespace BrainGames\BrainEven;
4+
5+
use function cli\line;
6+
use function cli\prompt;
7+
use function BrainGames\Engine\getUserName;
8+
use function BrainGames\Engine\checkAnswer;
9+
10+
//-- Игра - "Калькулятор"
11+
function brainCalcGame()
12+
{
13+
$userName = getUserName();
14+
line('What is the result of the expression?');
15+
16+
$num = random_int(1, 100);
17+
$num2 = random_int(1, 100);
18+
$operation = ["+", "-", "*"];
19+
$thisOperation = $operation[random_int(0, 2)];
20+
//-- Перебрать рандомные операторы --
21+
switch ($thisOperation) {
22+
case "*":
23+
$expectedAnswer = $num * $num2;
24+
break;
25+
26+
case "+":
27+
$expectedAnswer = $num + $num2;
28+
break;
29+
30+
case "-":
31+
$expectedAnswer = $num - $num2;
32+
break;
33+
}
34+
$answer = prompt("Question: {$num} {$thisOperation} {$num2}");
35+
$answer = is_string($answer) ? $answer : (int)$answer;
36+
}

src/Games/BrainEven.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22

33
namespace BrainGames\BrainEven;
44

5-
use function cli\line;
6-
use function cli\prompt;
7-
use function BrainGames\Engine\getUserName;
85
use function BrainGames\Engine\checkAnswer;
9-
//функция проверки чётности
10-
function isEven(int $num) : string
11-
{
12-
return $num % 2 === 0 ? "yes" : "no";
13-
}
6+
147
//-- Игра - "Проверка на чётность"
158
function brainEvenGame(int $count = 3): void
169
{
17-
//получим имя пользователя
18-
$userName = getUserName();
19-
line('Answer "yes" if the number is even, otherwise answer "no".');
20-
21-
$counterAnswers = 0;
22-
23-
while ($counterAnswers < $count) {
10+
$gameParams = [];
11+
$gameParams["rules"] = 'Answer "yes" if the number is even, otherwise answer "no".';
12+
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
2414
$num = random_int(1, 100);
25-
$answer = prompt("Question: {$num}");
26-
$expectedAnswer = isEven($num);
27-
//--Функция проверки ответов
28-
$counterAnswers = checkAnswer($counterAnswers, $userName, $answer, $expectedAnswer);
15+
$gameParams["expectedAnswer"][] = isEven($num) ? "yes" : "no";
16+
$gameParams["questions"][] = $num;
2917
}
18+
//--Функция проверки ответов
19+
$counterAnswers = checkAnswer($gameParams);
20+
}
21+
//--- Функция проверки на чётность
22+
function isEven(int $num) : string
23+
{
24+
return $num % 2 === 0;
3025
}

src/Games/BrainPrime.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace BrainGames\BrainPrime;
44

5+
use function BrainGames\Engine\getUserName;
6+
use function BrainGames\Engine\checkAnswer;
7+
58
//-- Проверка на простое число
69
function isPrime(int $number): bool
710
{

0 commit comments

Comments
 (0)