From fba6ccb6692bf1d7753addc762e403a79fecb30e Mon Sep 17 00:00:00 2001 From: Mikita Date: Thu, 7 Nov 2024 23:46:09 +0300 Subject: [PATCH] feat: progression game --- bin/brain-progression.js | 6 ++++++ package.json | 3 ++- src/games/index.js | 1 + src/games/progressionGame.js | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100755 bin/brain-progression.js create mode 100644 src/games/progressionGame.js diff --git a/bin/brain-progression.js b/bin/brain-progression.js new file mode 100755 index 0000000..446e036 --- /dev/null +++ b/bin/brain-progression.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node + +import brainGames from './brain-games.js'; +import { progressionGame } from '../src/games/index.js'; + +brainGames.startGame(progressionGame, 'What number is missing in the progression?'); diff --git a/package.json b/package.json index a51d8fd..deaaf97 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "brain-games": "bin/brain-games.js", "brain-even": "bin/brain-even.js", "brain-calc": "bin/brain-calc.js", - "brain-gcd": "bin/brain-gcd.js" + "brain-gcd": "bin/brain-gcd.js", + "brain-progression": "bin/brain-progression.js" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/src/games/index.js b/src/games/index.js index 6146732..2bb54dc 100644 --- a/src/games/index.js +++ b/src/games/index.js @@ -1,3 +1,4 @@ export { default as evenGame } from './evenGame.js'; export { default as calcGame } from './calcGame.js'; export { default as gcdGame } from './gcdGame.js'; +export { default as progressionGame } from './progressionGame.js'; diff --git a/src/games/progressionGame.js b/src/games/progressionGame.js new file mode 100644 index 0000000..56ba3d4 --- /dev/null +++ b/src/games/progressionGame.js @@ -0,0 +1,27 @@ +import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js'; + +export default () => { + const start = Math.round(Math.random() * 30); + const step = Math.round(Math.random() * 20); + const length = 10; + + const progression = Array(length).fill(start).map((value, index) => start + index * step); + const targetIndex = Math.floor(Math.random() * length); + const copyProgression = [...progression]; + + copyProgression[targetIndex] = '..'; + + const questionText = copyProgression.join(' '); + + askQuestion(questionText); + + const userAnswer = getAnswer('Your answer: '); + const rightAnswer = progression[targetIndex]; + + const isUserRight = Number(userAnswer) === rightAnswer; + const resultString = getResponseAfterRound(isUserRight, userAnswer, rightAnswer); + + console.log(resultString); + + return isUserRight; +};