From b2bbd292612b2c799395a749cdef7e3d0eabc57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B5=D0=B2=D0=B0?= Date: Tue, 17 Oct 2023 16:13:44 +0300 Subject: [PATCH] a --- bin/brain-even.js | 4 ++-- bin/brain-gcd.js | 4 ++-- bin/brain-progression.js | 4 ++-- games/calc.js | 34 +++++++++++-------------------- games/even.js | 30 ++++++++++------------------ games/gcd.js | 34 +++++++++++-------------------- games/prime.js | 43 +++++++++++++++++++--------------------- games/progression.js | 27 +++++++++---------------- index.js | 23 +++++++++++++++++++++ 9 files changed, 92 insertions(+), 111 deletions(-) create mode 100644 index.js diff --git a/bin/brain-even.js b/bin/brain-even.js index 672c4f6..40ec400 100755 --- a/bin/brain-even.js +++ b/bin/brain-even.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -import startGame from '../games/even.js'; +import isItEven from '../games/even.js'; -startGame(); +isItEven(); diff --git a/bin/brain-gcd.js b/bin/brain-gcd.js index d67f11e..ed2abb9 100755 --- a/bin/brain-gcd.js +++ b/bin/brain-gcd.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -import maxGeneralDivider from '../games/gcd.js'; +import isMaxGeneralDivider from '../games/gcd.js'; -maxGeneralDivider(); +isMaxGeneralDivider(); diff --git a/bin/brain-progression.js b/bin/brain-progression.js index 81b3fdf..316c213 100755 --- a/bin/brain-progression.js +++ b/bin/brain-progression.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -import progression from '../games/progression.js'; +import getProgression from '../games/progression.js'; -progression(); +getProgression(); diff --git a/games/calc.js b/games/calc.js index 9c58190..3ff3e9e 100644 --- a/games/calc.js +++ b/games/calc.js @@ -1,6 +1,4 @@ -import readlineSync from 'readline-sync'; - -import greetGamer from '../src/cli.js'; +import game from '../index.js'; import getRandomInt from '../function/getRandomInt.js'; @@ -22,25 +20,17 @@ const isRightAnswer = (numberOne, numberTwo, operator) => { return result; }; +const playCalc = () => { + const numberOne = getRandomInt(100); + const numberTwo = getRandomInt(100); + const operators = ['+', '-', '*']; + const operator = operators[getRandomInt(3)]; + const question = `${numberOne} ${operator} ${numberTwo}`; + const answerCorrect = isRightAnswer(numberOne, numberTwo, operator); + return [question, answerCorrect]; +}; const calcGame = () => { - const userName = greetGamer(); - console.log('What is the result of the expression?'); - for (let i = 0; i < 3; i += 1) { - const numberOne = getRandomInt(100); - const numberTwo = getRandomInt(100); - const operators = ['+', '-', '*']; - const operator = operators[getRandomInt(3)]; - const expression = `${numberOne} ${operator} ${numberTwo}`; - console.log('Question: ', expression); - const answerCorrect = isRightAnswer(numberOne, numberTwo, operator); - const answerUser = readlineSync.question('Your answer: '); - if (answerUser !== answerCorrect.toString()) { - console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`); - console.log("Let's try again, ", userName); - return; - } - console.log('Correct!'); - } - console.log('Congratulations,', userName, '!'); + const condition = ('What is the result of the expression?'); + game(playCalc, condition); }; export default calcGame; diff --git a/games/even.js b/games/even.js index 9396cc5..fb3264d 100644 --- a/games/even.js +++ b/games/even.js @@ -1,26 +1,16 @@ -import readlineSync from 'readline-sync'; +import game from '../index.js'; import getRandomInt from '../function/getRandomInt.js'; const isEvenNum = (number) => number % 2 === 0; -const startGame = () => { - console.log('Welcome to the Brain Games!'); - const userName = readlineSync.question('May I have your name? '); - console.log(`Hello, ${userName}!`); - console.log('Answer "yes" if the number is even, otherwise answer "no".'); - for (let i = 0; i < 3; i += 1) { - const number = getRandomInt(100); - console.log('Question: ', number); - const answerCorrect = isEvenNum(number) ? 'yes' : 'no'; - const answerUser = readlineSync.question('Your answer: '); - if (answerUser !== answerCorrect) { - console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`); - console.log("Let's try again, ", userName); - return; - } - console.log('Correct!'); - } - console.log('Congratulations,', userName, '!'); +const playEven = () => { + const question = String(getRandomInt(100)); + const answerCorrect = isEvenNum(question) ? 'yes' : 'no'; + return [question, answerCorrect]; }; -export default startGame; +const isItEven = () => { + const condition = ('Answer "yes" if the number is even, otherwise answer "no".'); + game(playEven, condition); +}; +export default isItEven; diff --git a/games/gcd.js b/games/gcd.js index bef4a6d..3418380 100644 --- a/games/gcd.js +++ b/games/gcd.js @@ -1,6 +1,4 @@ -import readlineSync from 'readline-sync'; - -import greetGamer from '../src/cli.js'; +import game from '../index.js'; import getRandomInt from '../function/getRandomInt.js'; @@ -19,23 +17,15 @@ const getMaxDevidor = (numberOne, numberTwo) => { } } return maxDevidor; }; -const maxGeneralDivider = () => { - const userName = greetGamer(); - console.log('Find the greatest common divisor of given numbers.'); - for (let i = 0; i < 3; i += 1) { - const numberOne = getRandomInt(100); - const numberTwo = getRandomInt(100); - const expression = `${numberOne} ${numberTwo}`; - console.log('Question: ', expression); - const answerCorrect = getMaxDevidor(numberOne, numberTwo); - const answerUser = readlineSync.question('Your answer: '); - if (answerUser !== answerCorrect.toString()) { - console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`); - console.log("Let's try again, ", userName); - return; - } - console.log('Correct!'); - } - console.log('Congratulations,', userName, '!'); +const playMaxGeneralDivider = () => { + const numberOne = getRandomInt(100); + const numberTwo = getRandomInt(100); + const question = `${numberOne} ${numberTwo}`; + const answerCorrect = getMaxDevidor(numberOne, numberTwo); + return [question, answerCorrect]; +}; +const isMaxGeneralDivider = () => { + const condition = ('Find the greatest common divisor of given numbers.'); + game(playMaxGeneralDivider, condition); }; -export default maxGeneralDivider; +export default isMaxGeneralDivider; diff --git a/games/prime.js b/games/prime.js index 227b2ab..3a988aa 100644 --- a/games/prime.js +++ b/games/prime.js @@ -1,31 +1,28 @@ -import readlineSync from 'readline-sync'; - -import greetGamer from '../src/cli.js'; - import getRandomInt from '../function/getRandomInt.js'; -const isNumPrime = (num) => { - if (num === 1) { - return 'no'; +import game from '../index.js'; + +const isNumPrime = (value) => { + const num = Number(value); + if (num === 1 || num === 0) { + return false; + } + for (let i = 2; i < value; i += 1) { + if (num % i === 0) { + return false; + } } - return (num % 2 === 0 || num % 3 === 0) ? 'no' : 'yes'; + return true; +}; + +const playPrime = () => { + const question = String(getRandomInt(100)); + const answerCorrect = isNumPrime(question) ? 'yes' : 'no'; + return [question, answerCorrect]; }; const isItPrime = () => { - const userName = greetGamer(); - console.log('Answer "yes" if given number is prime. Otherwise answer "no".'); - for (let i = 0; i < 3; i += 1) { - const number = getRandomInt(100); - console.log('Question: ', number); - const answerCorrect = isNumPrime(number); - const answerUser = readlineSync.question('Your answer: '); - if (answerUser !== answerCorrect) { - console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`); - console.log("Let's try again, ", userName); - return; - } - console.log('Correct!'); - } - console.log('Congratulations,', userName, '!'); + const condition = ('Answer "yes" if given number is prime. Otherwise answer "no".'); + game(playPrime, condition); }; export default isItPrime; diff --git a/games/progression.js b/games/progression.js index d103c35..a56dd6b 100644 --- a/games/progression.js +++ b/games/progression.js @@ -1,6 +1,4 @@ -import readlineSync from 'readline-sync'; - -import greetGamer from '../src/cli.js'; +import game from '../index.js'; import getRandomInt, { getRandomArbitrary } from '../function/getRandomInt.js'; @@ -22,19 +20,12 @@ const getArray = () => { }; const progression = () => { - const userName = greetGamer(); - console.log('What number is missing in the progression?'); - for (let i = 0; i < 3; i += 1) { - const [expression, answer] = getArray(); - console.log('Question: ', expression.join(' ')); - const answerUser = readlineSync.question('Your answer: '); - if (answerUser !== answer.toString()) { - console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answer}'.`); - console.log("Let's try again, ", userName); - return; - } - console.log('Correct!'); - } - console.log('Congratulations,', userName, '!'); + const [expression, answerCorrect] = getArray(); + const question = expression.join(' '); + return [question, answerCorrect]; +}; +const getProgression = () => { + const condition = ('What number is missing in the progression?'); + game(progression, condition); }; -export default progression; +export default getProgression; diff --git a/index.js b/index.js new file mode 100644 index 0000000..b5c51ca --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +import readlineSync from 'readline-sync'; + +import greetGamer from './src/cli.js'; + +const game = (partGame, condition) => { + const userName = greetGamer(); + console.log(condition); + for (let i = 0; i < 3; i += 1) { + const [question, answerCorrect] = partGame(); + console.log('Question: ', question); + const answerUser = readlineSync.question('Your answer: '); + if (answerUser === String(answerCorrect)) { + console.log('Correct!'); + } else { + console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`); + console.log("Let's try again, ", userName); + return; + } + } + console.log('Congratulations,', userName, '!'); +}; + +export default game;