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..28d86d3 --- /dev/null +++ b/src/bullsAndCows.js @@ -0,0 +1,21 @@ +'use strict'; + +const NUM_LENGTH = 4; + +module.exports.getBullsAndCows = (numberToGuess, enteredNumber) => { + let bullsCount = 0; + let cowsCount = 0; + + for (let i = 0; i < NUM_LENGTH; i++) { + const isBull = enteredNumber[i] === numberToGuess[i]; + const isCow = numberToGuess.includes(enteredNumber[i]); + + if (isBull) { + bullsCount++; + } else if (isCow) { + cowsCount++; + } + } + + return [bullsCount, cowsCount]; +}; diff --git a/src/generateNumber.js b/src/generateNumber.js new file mode 100644 index 0000000..c632fb3 --- /dev/null +++ b/src/generateNumber.js @@ -0,0 +1,21 @@ +'use strict'; + +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); + + 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..6e27d88 --- /dev/null +++ b/src/validateNumber.js @@ -0,0 +1,18 @@ +'use strict'; + +const NUM_LENGTH = 4; + +module.exports.isNumberValid = (number) => { + const digitsArray = number.split(''); + + for (let i = 0; i < NUM_LENGTH; i++) { + if ( + digitsArray.indexOf(digitsArray[i]) + !== digitsArray.lastIndexOf(digitsArray[i]) + ) { + return false; + } + } + + return !isNaN(+number) && number.length === NUM_LENGTH; +};