|
1 | 1 | import readlineSync from 'readline-sync'; |
2 | 2 | import greeting from '../index.js'; |
3 | 3 |
|
4 | | -const playGame = (gameDescription, generateQuestion, generateIncorrectMessage) => { |
| 4 | +const playGame = (gameDescription, generateQuestion, generateIncorrectMessage, roundsCount = 3) => { |
5 | 5 | // Приветсвие и описание |
6 | 6 | const userName = greeting(); |
7 | 7 | console.log(gameDescription); |
8 | 8 | // Начало игры |
9 | | - for (let correctAnswersCount = 0; correctAnswersCount < 3;) { |
| 9 | + for (let round = 0; round < roundsCount; round += 1) { |
10 | 10 | // Формируем вопрос |
11 | 11 | const { question, correctAnswer } = generateQuestion(); |
12 | 12 | console.log(`Question: ${question}`); |
13 | 13 | // Получаем ответ |
14 | 14 | const userAnswer = readlineSync.question('Your answer: '); |
15 | | - // Сверяем результаты |
16 | | - const incorrectMessage = generateIncorrectMessage(userAnswer, String(correctAnswer), userName); |
17 | | - // Функция подсчета ответов |
18 | | - let updatedCorrectAnswersCount = correctAnswersCount; |
19 | 15 | // Сравниваем ответы |
20 | 16 | if (userAnswer === String(correctAnswer)) { |
21 | 17 | console.log('Correct!'); |
22 | | - updatedCorrectAnswersCount += 1; |
23 | | - if (updatedCorrectAnswersCount === 3) { |
24 | | - // Завершение игры при достижении 3 |
25 | | - console.log(`Congratulations, ${userName}!`); |
26 | | - break; |
27 | | - } |
28 | 18 | } else { |
29 | 19 | // Завершение игры при неправильном ответе |
| 20 | + // Ответа имеет 3 варианта, в зависимости от игры, не 1 :( |
| 21 | + const incorrectMessage = generateIncorrectMessage( |
| 22 | + userAnswer, |
| 23 | + String(correctAnswer), |
| 24 | + userName, |
| 25 | + ); |
30 | 26 | console.log(incorrectMessage); |
31 | 27 | break; |
32 | 28 | } |
33 | | - // Обновляем счетчик правильных ответов |
34 | | - correctAnswersCount = updatedCorrectAnswersCount; |
| 29 | + // Если это последний раунд, поздравляем пользователя |
| 30 | + if (round === roundsCount - 1) { |
| 31 | + console.log(`Congratulations, ${userName}!`); |
| 32 | + } |
35 | 33 | } |
36 | 34 | }; |
37 | 35 |
|
|
0 commit comments