Skip to content

Commit 0e1496a

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

File tree

8 files changed

+98
-112
lines changed

8 files changed

+98
-112
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Welcome to the Brain Games!
2+
13
Brain-Even: https://asciinema.org/a/UZBtDobwLPOrNa1r3vp6JYiWS
24

35
Brain-Calc: https://asciinema.org/a/cKn78eDiU1h5MTCMr1183kawd

bin/brain-calc

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ if (file_exists($autoloadPath1)) {
1313
require_once $autoloadPath2;
1414
}
1515
//получим имя пользователя
16-
$userName = getUserName();
16+
/*$userName = getUserName();
1717
//запуск игры
18-
checkAnswer($count = 3, $userName, $path="calc");
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;
29+
30+
case "+":
31+
$expectedAnswer = $num + $num2;
32+
break;
33+
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+

bin/brain-even

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

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

86
$autoloadPath1 = __DIR__ . '/../../../autoload.php';
97
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';
@@ -13,7 +11,6 @@ if (file_exists($autoloadPath1)) {
1311
} else {
1412
require_once $autoloadPath2;
1513
}
16-
//получим имя пользователя
17-
$userName = getUserName();
18-
//запуск игры
19-
checkAnswer($count = 3, $userName, $path="even");
14+
15+
//запуск игры со значением счётчиика по умолчанию
16+
brainEvenGame($count = 3);

bin/brain-gcd

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

21+
//-- Игра - Общий делитель -- "НОД"
22+
case "gcd":
23+
$expectedAnswer = getGcd($num, $num2);
24+
line('Find the greatest common divisor of given numbers.');
25+
$answer = prompt("Question: {$num} {$num2}");
26+
break;
27+

bin/brain-prime

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ if (file_exists($autoloadPath1)) {
1414
require_once $autoloadPath2;
1515
}
1616
//получим имя пользователя
17-
$userName = getUserName();
17+
/*$userName = getUserName();
1818
//запуск игры
1919
checkAnswer($count = 3, $userName, $path="prime");
20+
case "prime":
21+
line('Answer "yes" if given number is prime. Otherwise answer "no".');
22+
$answer = prompt("Question: {$num}");
23+
$expectedAnswer = isPrime($num) ? 'yes' : 'no';
24+
break;
25+
default:
26+
return line("This Game is still in production :)");
27+
}

bin/brain-progression

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ if (file_exists($autoloadPath1)) {
1414
require_once $autoloadPath2;
1515
}
1616
//получим имя пользователя
17-
$userName = getUserName();
17+
/*$userName = getUserName();
1818
//Запустим игру
1919
checkAnswer($count = 3, $userName, $path="progression");
2020

21+
case "progression":
22+
$progression = generateProgression();
23+
$hiddenElement = hideElement($progression);
24+
$expectedAnswer = $hiddenElement["hidden_value"];
25+
line('What number is missing in the progression?');
26+
$answer = prompt("Question: " . implode(" ", $hiddenElement["progression"]));
27+
break;
28+
//-- Игра - "Простое ли число?"
29+

src/Engine.php

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use function cli\line;
66
use function cli\prompt;
7+
use function BrainGames\BrainEven\brainEvenGame;
78
use function BrainGames\BrainGcd\getGcd;
89
use function BrainGames\BrainProgression\generateProgression;
910
use function BrainGames\BrainProgression\hideElement;
@@ -19,82 +20,30 @@ function getUserName(): string
1920
//Вернуть полученное имя
2021
return $name;
2122
}
22-
//---Функция проверки ответов
23-
function checkAnswer(int $count, string $name, string $path)
23+
//---Функция для неверного ответа
24+
function wrongAnswer(string $name, mixed $answer, mixed $expectedAnswer) {
25+
line("{$answer} is wrong answer ;(. Correct answer was '{$expectedAnswer}'.");
26+
line("Let's try again, %s!", $name);
27+
return;
28+
}
29+
//---Функция для верного ответа
30+
function trueAnswer(string $name) {
31+
line("Congratulations, %s!", $name);
32+
return;
33+
}
34+
//---Функция проверки ответов
35+
function checkAnswer(int $counterAnswers, string $name, mixed $answer, mixed $expectedAnswer)
2436
{
25-
//---Узнать какой файл обратился (для дальнейшей логики)
26-
$filepath = $path;
27-
//-- Генерация случайных чисел для игр
28-
$num = random_int(1, 100);
29-
$num2 = random_int(1, 10);
30-
31-
//--- Логика игр
32-
//-- Подлючим логику в зависимости от выбора игры
33-
switch ($filepath) {
34-
//-- Игра - "Проверка на чётность"
35-
case "even":
36-
line('Answer "yes" if the number is even, otherwise answer "no".');
37-
$answer = prompt("Question: {$num}");
38-
$isEven = $num % 2 === 0 ? true : false;
39-
$expectedAnswer = $isEven ? 'yes' : 'no';
40-
break;
41-
//-- Игра - "Калькулятор"
42-
case "calc":
43-
line('What is the result of the expression?');
44-
$operation = ["+", "-", "*"];
45-
$thisOperation = $operation[random_int(0, 2)];
46-
//-- Перебрать рандомные операторы --
47-
switch ($thisOperation) {
48-
case "*":
49-
$expectedAnswer = $num * $num2;
50-
break;
51-
52-
case "+":
53-
$expectedAnswer = $num + $num2;
54-
break;
55-
56-
case "-":
57-
$expectedAnswer = $num - $num2;
58-
break;
59-
}
60-
$answer = prompt("Question: {$num} {$thisOperation} {$num2}");
61-
$answer = is_string($answer) ? $answer : (int)$answer;
62-
break;
63-
//-- Игра - Общий делитель -- "НОД"
64-
case "gcd":
65-
$expectedAnswer = getGcd($num, $num2);
66-
line('Find the greatest common divisor of given numbers.');
67-
$answer = prompt("Question: {$num} {$num2}");
68-
break;
69-
//-- Игра - "Арифметическая прогрессия"
70-
case "progression":
71-
$progression = generateProgression();
72-
$hiddenElement = hideElement($progression);
73-
$expectedAnswer = $hiddenElement["hidden_value"];
74-
line('What number is missing in the progression?');
75-
$answer = prompt("Question: " . implode(" ", $hiddenElement["progression"]));
76-
break;
77-
//-- Игра - "Простое ли число?"
78-
case "prime":
79-
line('Answer "yes" if given number is prime. Otherwise answer "no".');
80-
$answer = prompt("Question: {$num}");
81-
$expectedAnswer = isPrime($num) ? 'yes' : 'no';
82-
break;
83-
default:
84-
return line("This Game is still in production :)");
85-
}
86-
//Показать пользователю его ответ
8737
line("Your answer: {$answer}");
88-
8938
if ($answer != $expectedAnswer) {
90-
line("{$answer} is wrong answer ;(. Correct answer was '{$expectedAnswer}'.");
91-
return line("Let's try again, %s!", $name);
39+
wrongAnswer($name, $answer, $expectedAnswer);
40+
return $counterAnswers = 3;
9241
} else {
9342
line("Correct!");
94-
$count--;
95-
if ($count <= 0) {
96-
return line("Congratulations, %s!", $name);
43+
$counterAnswers++;
44+
if ($counterAnswers == 3) {
45+
trueAnswer($name);
9746
}
98-
checkAnswer($count, $name, $path);
9947
}
48+
return $counterAnswers;
10049
}

src/Games/BrainEven.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,27 @@
44

55
use function cli\line;
66
use function cli\prompt;
7-
8-
/*
9-
function getUserName()
7+
use function BrainGames\Engine\getUserName;
8+
use function BrainGames\Engine\checkAnswer;
9+
//функция проверки чётности
10+
function isEven(int $num) : string
1011
{
11-
//Приветствуем пользователя
12-
line('Welcome to the Brain Game!');
13-
$name = prompt('May I have your name?');
14-
line("Hello, %s!", $name);
12+
return $num % 2 === 0 ? "yes" : "no";
13+
}
14+
//-- Игра - "Проверка на чётность"
15+
function brainEvenGame(int $count = 3): void
16+
{
17+
//получим имя пользователя
18+
$userName = getUserName();
1519
line('Answer "yes" if the number is even, otherwise answer "no".');
16-
//Инициализируем счётчик
17-
$count = 0;
18-
//--Функция проверки ответов
19-
function checkAnswer($count, $name)
20-
{
20+
21+
$counterAnswers = 0;
22+
23+
while ($counterAnswers < $count) {
2124
$num = random_int(1, 100);
22-
$isEven = $num % 2 === 0 ? true : false;
23-
$answer = prompt("Question:", $num);
24-
line("Your answer: {$answer}");
25-
$expectedAnswer = $isEven ? 'yes' : 'no';
26-
if ($answer !== $expectedAnswer) {
27-
line("{$answer} is wrong answer ;(. Correct answer was '{$expectedAnswer}'.");
28-
return line("Let's try again, %s!", $name);
29-
} else {
30-
line("Correct!");
31-
$count++;
32-
if ($count === 3) {
33-
return line("Congratulations, %s!", $name);
34-
}
35-
checkAnswer($count, $name);
36-
}
25+
$answer = prompt("Question: {$num}");
26+
$expectedAnswer = isEven($num);
27+
//--Функция проверки ответов
28+
$counterAnswers = checkAnswer($counterAnswers, $userName, $answer, $expectedAnswer);
3729
}
38-
checkAnswer($count, $name);
39-
}*/
30+
}

0 commit comments

Comments
 (0)