55use function Cli \line ;
66use 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