44
55use function cli \line ;
66use function cli \prompt ;
7+ use function BrainGames \BrainEven \brainEvenGame ;
78use function BrainGames \BrainGcd \getGcd ;
89use function BrainGames \BrainProgression \generateProgression ;
910use 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}
0 commit comments