From 283913d87639bfc195160840d11fb2b5fdead337 Mon Sep 17 00:00:00 2001 From: AndruhaMan Date: Thu, 24 Aug 2023 18:48:16 +0300 Subject: [PATCH 1/2] The task is completed --- src/app.js | 32 ++++++++++++++++++++++++++++++++ src/bullsAndCows.js | 26 ++++++++++++++++++++++++++ src/generateNumber.js | 17 +++++++++++++++++ src/terminal.js | 10 ++++++++++ src/validateNumber.js | 13 +++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/bullsAndCows.js create mode 100644 src/generateNumber.js create mode 100644 src/terminal.js create mode 100644 src/validateNumber.js diff --git a/src/app.js b/src/app.js index ad9a93a..7548c32 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,33 @@ +/* eslint-disable no-console */ 'use strict'; + +const { getBullsAndCows } = require('./bullsAndCows'); +const { generateNumber } = require('./generateNumber'); +const { terminal } = require('./terminal'); +const { isNumberValid } = require('./validateNumber'); + +const generatedNumber = String(generateNumber()); + +const startGame = () => { + terminal.question('Try to quess the number:\n', (enteredNumber) => { + if (!isNumberValid(enteredNumber)) { + console.log('Invalid number\n'); + startGame(); + } else { + const bullsAndCows = getBullsAndCows(generatedNumber, enteredNumber); + const bulls = bullsAndCows[0]; + const cows = bullsAndCows[1]; + + console.log(`Bulls: ${bulls}, Cows: ${cows}\n`); + + if (bulls === 4) { + console.log('Congratulations, you win!'); + terminal.close(); + } else { + startGame(); + } + } + }); +}; + +startGame(); diff --git a/src/bullsAndCows.js b/src/bullsAndCows.js new file mode 100644 index 0000000..700407b --- /dev/null +++ b/src/bullsAndCows.js @@ -0,0 +1,26 @@ +'use strict'; + +const NUM_LENGTH = 4; + +module.exports.getBullsAndCows = (numberToGuess, enteredNumber) => { + const numberToGuessCopy = numberToGuess.split(''); + const enteredNumberCopy = enteredNumber.split(''); + let bullsCount = 0; + let cowsCount = 0; + + for (let i = 0; i < NUM_LENGTH; i++) { + const isBull = enteredNumberCopy[0] === numberToGuessCopy[0]; + const isCow = numberToGuess.includes(enteredNumberCopy[0]); + + if (isBull) { + bullsCount++; + } else if (isCow) { + cowsCount++; + } + + numberToGuessCopy.shift(); + enteredNumberCopy.shift(); + } + + return [bullsCount, cowsCount]; +}; diff --git a/src/generateNumber.js b/src/generateNumber.js new file mode 100644 index 0000000..38028e4 --- /dev/null +++ b/src/generateNumber.js @@ -0,0 +1,17 @@ +'use strict'; + +const NUM_LENGTH = 4; + +module.exports.generateNumber = () => { + const digitsArray = []; + + while (digitsArray.length < NUM_LENGTH) { + const numToPush = Math.floor(Math.random() * 10); + + if (!digitsArray.includes(numToPush)) { + digitsArray.push(numToPush); + } + } + + return digitsArray.join(''); +}; diff --git a/src/terminal.js b/src/terminal.js new file mode 100644 index 0000000..7c6c304 --- /dev/null +++ b/src/terminal.js @@ -0,0 +1,10 @@ +'use strict'; + +const readline = require('readline'); + +const terminal = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +module.exports = { terminal }; diff --git a/src/validateNumber.js b/src/validateNumber.js new file mode 100644 index 0000000..9f64337 --- /dev/null +++ b/src/validateNumber.js @@ -0,0 +1,13 @@ +'use strict'; + +const NUM_LENGTH = 4; + +module.exports.isNumberValid = (number) => { + for (let i = 1; i < NUM_LENGTH; i++) { + if (number[i] === number[i - 1]) { + return false; + } + } + + return !isNaN(+number) && number.length === NUM_LENGTH; +}; From 56cfdba67805bc87c01c36c345a18ad865f32afe Mon Sep 17 00:00:00 2001 From: AndruhaMan Date: Fri, 25 Aug 2023 12:53:20 +0300 Subject: [PATCH 2/2] Fix problems --- src/bullsAndCows.js | 9 ++------- src/generateNumber.js | 4 ++++ src/validateNumber.js | 9 +++++++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/bullsAndCows.js b/src/bullsAndCows.js index 700407b..28d86d3 100644 --- a/src/bullsAndCows.js +++ b/src/bullsAndCows.js @@ -3,23 +3,18 @@ const NUM_LENGTH = 4; module.exports.getBullsAndCows = (numberToGuess, enteredNumber) => { - const numberToGuessCopy = numberToGuess.split(''); - const enteredNumberCopy = enteredNumber.split(''); let bullsCount = 0; let cowsCount = 0; for (let i = 0; i < NUM_LENGTH; i++) { - const isBull = enteredNumberCopy[0] === numberToGuessCopy[0]; - const isCow = numberToGuess.includes(enteredNumberCopy[0]); + const isBull = enteredNumber[i] === numberToGuess[i]; + const isCow = numberToGuess.includes(enteredNumber[i]); if (isBull) { bullsCount++; } else if (isCow) { cowsCount++; } - - numberToGuessCopy.shift(); - enteredNumberCopy.shift(); } return [bullsCount, cowsCount]; diff --git a/src/generateNumber.js b/src/generateNumber.js index 38028e4..c632fb3 100644 --- a/src/generateNumber.js +++ b/src/generateNumber.js @@ -5,6 +5,10 @@ const NUM_LENGTH = 4; module.exports.generateNumber = () => { const digitsArray = []; + const randomNumExceptZero = Math.floor(Math.random() * 9 + 1); + + digitsArray.push(randomNumExceptZero); + while (digitsArray.length < NUM_LENGTH) { const numToPush = Math.floor(Math.random() * 10); diff --git a/src/validateNumber.js b/src/validateNumber.js index 9f64337..6e27d88 100644 --- a/src/validateNumber.js +++ b/src/validateNumber.js @@ -3,8 +3,13 @@ const NUM_LENGTH = 4; module.exports.isNumberValid = (number) => { - for (let i = 1; i < NUM_LENGTH; i++) { - if (number[i] === number[i - 1]) { + const digitsArray = number.split(''); + + for (let i = 0; i < NUM_LENGTH; i++) { + if ( + digitsArray.indexOf(digitsArray[i]) + !== digitsArray.lastIndexOf(digitsArray[i]) + ) { return false; } }