diff --git a/src/app.js b/src/app.js index ad9a93a..edf9845 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,61 @@ 'use strict'; + +const { getRandomDigits } = require('./generateRandom.js'); +const { terminal } = require('./terminal.js'); +const { check } = require('./cheacker.js'); +const { isValidGuess } = require('./guessValidation.js'); + +const startGame = () => { + const number = getRandomDigits(); + + const tryToGuess = () => { + terminal.question('Enter a number with 4 different digits: ', (guess) => { + const matches = check(number, guess); + + if (isNaN(guess)) { + // eslint-disable-next-line no-console + console.log('Invalid input. Please enter 4 different digits.'); + tryToGuess(); + + return; + } + + if (guess.length !== 4 || !isValidGuess(guess)) { + // eslint-disable-next-line no-console + console.log('Not valid number!'); + tryToGuess(); + + return; + } + + if (!matches.bull && !matches.cow) { + // eslint-disable-next-line no-console + console.log('Try again!'); + tryToGuess(); + + return; + } + + if (matches.bull === 4) { + // eslint-disable-next-line no-console + console.log('Congratulations!'); + terminal.close(); + + return; + } + + if (matches.bull !== 4) { + // eslint-disable-next-line no-console + console.log(` + Nice try! + You have ${matches.bull} bulls and ${matches.cow} cows + `); + tryToGuess(); + } + }); + }; + + tryToGuess(); +}; + +startGame(); diff --git a/src/cheacker.js b/src/cheacker.js new file mode 100644 index 0000000..1483864 --- /dev/null +++ b/src/cheacker.js @@ -0,0 +1,25 @@ +'use strict'; + +const check = (number, guess) => { + const obj = { + bull: 0, + cow: 0, + }; + const numberStr = number.toString(); + + guess.split('').forEach((digit, i) => { + if (numberStr[i] === digit) { + obj.bull += 1; + + return; + } + + if (numberStr.includes(digit)) { + obj.cow += 1; + } + }); + + return obj; +}; + +module.exports = { check }; diff --git a/src/generateRandom.js b/src/generateRandom.js new file mode 100644 index 0000000..1b79eb4 --- /dev/null +++ b/src/generateRandom.js @@ -0,0 +1,17 @@ +'use strict'; + +function getRandomDigits() { + const digits = []; + + while (digits.length < 4) { + const randomNumber = Math.floor(Math.random() * 10); + + if (!digits.includes(randomNumber)) { + digits.push(randomNumber); + } + } + + return digits.join(''); +} + +module.exports = { getRandomDigits }; diff --git a/src/guessValidation.js b/src/guessValidation.js new file mode 100644 index 0000000..f8e6766 --- /dev/null +++ b/src/guessValidation.js @@ -0,0 +1,11 @@ +'use strict'; + +const isValidGuess = (guess) => { + const arr = guess.split(''); + + const filtered = arr.filter((value, i) => arr.indexOf(value) === i); + + return arr.length === filtered.length; +}; + +module.exports = { isValidGuess }; 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 };