diff --git a/src/app.js b/src/app.js index e89a2d9..1625319 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,49 @@ 'use strict'; -// Write your code here +const readline = require('readline'); +const { generateRandomNumber } = require('./modules/generateRandomNumber'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); +const { getBullsAndCows } = require('./modules/getBullsAndCows'); + +const secretNumber = generateRandomNumber(); + +const terminal = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +function playGame() { + terminal.question('Guess the number: ', (userInput) => { + const userNumber = +userInput; + + if (checkIsValidUserInput(userNumber)) { + const result = getBullsAndCows( + userInput.toString(), secretNumber.toString() + ); + + // eslint-disable-next-line no-console + console.log(`Bulls: ${result.bulls}, Cows: ${result.cows}`); + + if (result.bulls === 4) { + // eslint-disable-next-line no-console + console.log('Congratulations! You guessed the correct number.'); + terminal.close(); + } else { + playGame(); + } + } else { + // eslint-disable-next-line no-console + console.log( + 'Invalid input. Please enter a 4-digit number with no duplicate digits.' + ); + playGame(); + } + }); +} + +// eslint-disable-next-line no-console +console.log( + 'Welcome to the Bulls and Cows game! Try to guess the 4-digit number.' +); + +playGame(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 4097966..24727e0 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,7 +9,18 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + const inputLength = userInput.length; + const numericInput = userInput; + const isNumeric = !isNaN(numericInput); + const isInRange = numericInput >= 1000; + const uniqueDigits = new Set(userInput); + + const isValid = uniqueDigits.size === inputLength + && isNumeric + && isInRange + && inputLength === 4; + + return isValid; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2..08843c8 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,7 +7,23 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + const number = []; + + while (number.length < 4) { + let randomNumber = 0; + + if (number.length === 0) { + randomNumber = Math.floor(Math.random() * 8) + 1; + } else { + do { + randomNumber = Math.floor(Math.random() * 9); + } while (number.includes(randomNumber)); + } + + number.push(randomNumber); + } + + return Number([...number].join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a..d1a127d 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,21 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const input = userInput.toString(); + const number = numberToGuess.toString(); + const bullsAndCows = { + bulls: 0, cows: 0, + }; + + for (let i = 0; i < 4; i++) { + if (input[i] === number[i]) { + bullsAndCows.bulls += 1; + } else if (number.includes(input[i])) { + bullsAndCows.cows += 1; + } + } + + return bullsAndCows; } module.exports = {