From a6f2fd2be7d96ae532d53d679527214d88006c9c Mon Sep 17 00:00:00 2001 From: IvanLi Date: Wed, 24 Apr 2024 11:56:36 +0300 Subject: [PATCH] created one func for all games - playGame --- src/games/calcGame.js | 9 ++------- src/games/evGame.js | 11 ++--------- src/games/gcdGame.js | 11 ++--------- src/games/primeGame.js | 20 +++++++------------- src/games/progGame.js | 11 ++--------- src/index.js | 13 +++++++++++-- 6 files changed, 26 insertions(+), 49 deletions(-) diff --git a/src/games/calcGame.js b/src/games/calcGame.js index b25f31a..586db02 100644 --- a/src/games/calcGame.js +++ b/src/games/calcGame.js @@ -1,5 +1,5 @@ -import mainLogicGame from '../index.js'; import greeting from '../cli.js'; +import { playGame } from '../index.js'; const userName = greeting('What is the result of the expression?'); const calcExpression = (operator, number1, number2) => { @@ -27,12 +27,7 @@ const generateExpression = () => { }; const playCalcGame = () => { - for (let i = 0; i < 3; i += 1) { - const { question, rightAnswer } = generateExpression(); - const isCorrect = mainLogicGame(question, rightAnswer, userName); - if (!isCorrect) return; - } - console.log(`Congratulations, ${userName}!`); + playGame(generateExpression, userName); }; export default playCalcGame; diff --git a/src/games/evGame.js b/src/games/evGame.js index db3dbcf..3878dc0 100644 --- a/src/games/evGame.js +++ b/src/games/evGame.js @@ -1,5 +1,5 @@ import greeting from '../cli.js'; -import mainLogicGame from '../index.js'; +import { playGame } from '../index.js'; import getRandomNumber from '../randomNumber.js'; const userName = greeting('Answer "yes" if the number is even, otherwise answer "no".'); @@ -11,14 +11,7 @@ const generateExpression = () => { }; const playEvGame = () => { - for (let i = 0; i < 3; i += 1) { - const { question, rightAnswer } = generateExpression(); - const isCorrect = mainLogicGame(question, rightAnswer, userName); - if (!isCorrect) { - return; - } - } - console.log(`Congratulations, ${userName}!`); + playGame(generateExpression, userName); }; export default playEvGame; diff --git a/src/games/gcdGame.js b/src/games/gcdGame.js index 63e83ef..bf625b5 100644 --- a/src/games/gcdGame.js +++ b/src/games/gcdGame.js @@ -1,4 +1,4 @@ -import mainLogicGame from '../index.js'; +import { playGame } from '../index.js'; import greeting from '../cli.js'; import getRandomNumber from '../randomNumber.js'; @@ -48,14 +48,7 @@ const generateExpression = () => { }; const playGcd = () => { - for (let i = 0; i < 3; i += 1) { - const { question, rightAnswer } = generateExpression(); - const isCorrect = mainLogicGame(question, rightAnswer, userName); - if (!isCorrect) { - return; - } - } - console.log(`Congratulations, ${userName}!`); + playGame(generateExpression, userName); }; export default playGcd; diff --git a/src/games/primeGame.js b/src/games/primeGame.js index 03cc01e..f0d51f9 100644 --- a/src/games/primeGame.js +++ b/src/games/primeGame.js @@ -1,5 +1,5 @@ import greeting from '../cli.js'; -import mainLogicGame from '../index.js'; +import { playGame } from '../index.js'; import getRandomNumber from '../randomNumber.js'; const userName = greeting('Answer "yes" if given number is prime. Otherwise answer "no".'); @@ -12,20 +12,14 @@ const isPrime = (count) => { return true; }; -const generateExpression = () => { - const question = getRandomNumber(2, 100); - const rightAnswer = isPrime(question) ? 'yes' : 'no'; - return { question, rightAnswer }; +const generatePrimeExpression = () => { + const primeQuestion = getRandomNumber(2, 100); + const rightAnswer = isPrime(primeQuestion) ? 'yes' : 'no'; + return { question: primeQuestion, rightAnswer }; }; + const playPrime = () => { - for (let i = 0; i < 3; i += 1) { - const { question, rightAnswer } = generateExpression(); - const isCorrect = mainLogicGame(question, rightAnswer, userName); - if (!isCorrect) { - return; - } - } - console.log(`Congratulations, ${userName}!`); + playGame(generatePrimeExpression, userName); }; export default playPrime; diff --git a/src/games/progGame.js b/src/games/progGame.js index b4a503e..3daa427 100644 --- a/src/games/progGame.js +++ b/src/games/progGame.js @@ -1,4 +1,4 @@ -import mainLogicGame from '../index.js'; +import { playGame } from '../index.js'; import greeting from '../cli.js'; import getRandomNumber from '../randomNumber.js'; @@ -26,14 +26,7 @@ const generateExpression = () => { }; const playProgression = () => { - for (let i = 0; i < 3; i += 1) { - const { question, rightAnswer } = generateExpression(); - const isCorrect = mainLogicGame(question, rightAnswer, userName); - if (!isCorrect) { - return; - } - } - console.log(`Congratulations, ${userName}!`); + playGame(generateExpression, userName); }; export default playProgression; diff --git a/src/index.js b/src/index.js index 5028dfd..9a2a003 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import readlineSync from 'readline-sync'; -const mainLogicGame = (question, rightAnswer, userName) => { +export const mainLogicGame = (question, rightAnswer, userName) => { const userAnswer = readlineSync.question(`Question: ${question}`); console.log(`Your answer: ${userAnswer}`); if (String(userAnswer) === String(rightAnswer)) { @@ -12,4 +12,13 @@ const mainLogicGame = (question, rightAnswer, userName) => { return false; }; -export default mainLogicGame; +export const playGame = (generateExpression, userName) => { + for (let i = 0; i < 3; i += 1) { + const { question, rightAnswer } = generateExpression(); + const isCorrect = mainLogicGame(question, rightAnswer, userName); + if (!isCorrect) { + return; + } + } + console.log(`Congratulations, ${userName}!`); +};