From d334c92ac15bf9cd3730a4c42589f221626994aa Mon Sep 17 00:00:00 2001 From: Olena Boldysheva Date: Tue, 8 Aug 2023 18:57:22 +0300 Subject: [PATCH] add solution --- src/app.js | 38 ++++++++++++++++++++++++++++++++++++ src/calculateBullsAndCows.js | 21 ++++++++++++++++++++ src/generateRandomNumber.js | 17 ++++++++++++++++ src/getUserInput.js | 26 ++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 src/calculateBullsAndCows.js create mode 100644 src/generateRandomNumber.js create mode 100644 src/getUserInput.js diff --git a/src/app.js b/src/app.js index ad9a93a..c695aa9 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,39 @@ +/* eslint-disable no-console */ 'use strict'; + +const { generateRandomNumber } = require('./generateRandomNumber'); +const { calculateBullsAndCows } = require('./calculateBullsAndCows'); +const { getUserInput, closeInterface } = require('./getUserInput'); + +(async () => { + const secretNumber = generateRandomNumber(); + + console.log('Welcome to Bulls and Cows game!'); + + let attempts = 0; + + while (true) { + const guess = await getUserInput('Enter your guess (4 different digits): '); + + console.log(`Your guess: ${guess}`); + + if (new Set(guess).size !== 4 || isNaN(guess)) { + console.log('Invalid input. Please enter 4 different digits.'); + continue; + } + + attempts++; + + const result = calculateBullsAndCows(secretNumber, guess); + + console.log(`Bulls: ${result.bulls}, Cows: ${result.cows}`); + + if (result.bulls === 4) { + console.log( + `Congratulations! You guessed the number in ${attempts} attempts.` + ); + closeInterface(); + break; + } + } +})(); diff --git a/src/calculateBullsAndCows.js b/src/calculateBullsAndCows.js new file mode 100644 index 0000000..8ead84a --- /dev/null +++ b/src/calculateBullsAndCows.js @@ -0,0 +1,21 @@ +'use strict'; + +function calculateBullsAndCows(secretNumber, guess) { + let bulls = 0; + let cows = 0; + + for (let i = 0; i < secretNumber.length; i++) { + if (secretNumber[i] === guess[i]) { + bulls++; + } else if (secretNumber.includes(guess[i])) { + cows++; + } + } + + return { + bulls, + cows, + }; +} + +module.exports = { calculateBullsAndCows }; diff --git a/src/generateRandomNumber.js b/src/generateRandomNumber.js new file mode 100644 index 0000000..8910a02 --- /dev/null +++ b/src/generateRandomNumber.js @@ -0,0 +1,17 @@ +'use strict'; + +function generateRandomNumber() { + 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 = { generateRandomNumber }; diff --git a/src/getUserInput.js b/src/getUserInput.js new file mode 100644 index 0000000..a25c542 --- /dev/null +++ b/src/getUserInput.js @@ -0,0 +1,26 @@ +/* eslint-disable no-console */ +'use strict'; + +const readline = require('readline'); + +const terminal = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +function getUserInput(prompt) { + return new Promise((resolve) => { + terminal.question(prompt, (answer) => { + resolve(answer); + }); + }); +} + +function closeInterface() { + terminal.close(); +} + +module.exports = { + getUserInput, + closeInterface, +};