Skip to content

Commit 33152ac

Browse files
committed
Update src/Cli.php and Fix bin/brain-games
1 parent c903b8d commit 33152ac

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

bin/brain-games

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
require __DIR__."/../vendor/autoload.php";
66

7-
\BrainGames\Cli\sayHello();
7+
\BrainGames\Cli\sayGreeting();

src/Cli.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,45 @@
55
use function Cli\line;
66
use function Cli\prompt;
77

8-
function sayHello(): void
8+
function sayGreeting(string $gameDescription = ''): string
99
{
1010
line('Welcome to the Brain Game!');
1111
$name = prompt('May I have your name?');
1212
line("Hello, %s!", $name);
13+
if (!empty($gameDescription)) {
14+
line($gameDescription);
15+
}
16+
return $name;
1317
}
18+
19+
function sayCongratulions(string $userName): void
20+
{
21+
line("Congratulations, {$userName}!");
22+
}
23+
24+
function sayUserLose(string $userAnswer, string $correctAnswer, string $userName): void
25+
{
26+
line("'{$userAnswer}' is wrong answer ;(. Correct answer was '{$correctAnswer}'.");
27+
line("Let's try again, {$userName}");
28+
}
29+
30+
function askQuestion(mixed $question): string
31+
{
32+
line("Question: {$question}");
33+
return prompt("Your answer",);
34+
}
35+
36+
function playGame(array $questions, array $correctAnswers): void
37+
{
38+
$userName = sayGreeting('Answer "yes" if the number is even, otherwise answer "no".');
39+
for ($i = 0; $i < 3; $i++) {
40+
$question = $questions[$i];
41+
$correctAnswer = $correctAnswers[$i];
42+
$userAnswer = askQuestion($question);
43+
if ($userAnswer !== $correctAnswer) {
44+
sayUserLose($userAnswer, $correctAnswer, $userName);
45+
return;
46+
}
47+
}
48+
sayCongratulions($userName);
49+
}

0 commit comments

Comments
 (0)