From d20e46092c07e7e72a00bfcaae9585fba279c812 Mon Sep 17 00:00:00 2001 From: Avramidi Yaroslav Date: Wed, 23 Oct 2024 12:45:45 +0300 Subject: [PATCH] add solution --- .github/workflows/test.yml-template | 23 +++++++++++++++++ package-lock.json | 18 ++++++++++--- package.json | 5 +++- src/app.js | 38 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 22 +++++++++++++--- src/modules/generateRandomNumber.js | 16 ++++++++++-- src/modules/getBullsAndCows.js | 36 ++++++++++++++++++++++---- 7 files changed, 142 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/test.yml-template diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/package-lock.json b/package-lock.json index 185b2268..de1b0c7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,12 @@ "version": "1.0.0", "hasInstallScript": true, "license": "GPL-3.0", + "dependencies": { + "readline": "^1.3.0" + }, "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,10 +1470,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "1.9.12", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", + "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -7498,6 +7502,12 @@ "integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==", "dev": true }, + "node_modules/readline": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz", + "integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg==", + "license": "BSD" + }, "node_modules/regexp.prototype.flags": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz", diff --git a/package.json b/package.json index 216da526..919085a9 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -27,5 +27,8 @@ }, "mateAcademy": { "projectType": "javascript" + }, + "dependencies": { + "readline": "^1.3.0" } } diff --git a/src/app.js b/src/app.js index e89a2d97..e513ed96 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,39 @@ 'use strict'; -// Write your code here +'use strict'; + +const readline = require('readline'); +const { generateRandomNumber } = require('./generateRandomNumber'); +const { checkIsValidUserInput } = require('./checkIsValidUserInput'); +const { getBullsAndCows } = require('./getBullsAndCows'); + +const terminal = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +function playGame() { + const secretNumber = generateRandomNumber().toString(); + + function askGuess() { + terminal.question('Enter your number: ', (userInput) => { + if (!checkIsValidUserInput(userInput)) { + return askGuess(); + } + + const { bulls } = getBullsAndCows(secretNumber, userInput); + + if (bulls === 4) { + terminal.close(); + } else { + askGuess(); + } + }); + } + + askGuess(); +} + +module.exports = { + playGame, +}; diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..511d279b 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -5,11 +5,27 @@ * Valid user input is a 4-digit number that does not start with 0 * and does not contain any duplicate digits. * - * @param {string} userInput - The user input - * @return {boolean} - True if the user input is valid, false otherwise + * @param {string} userInput + * @return {boolean} */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + if (userInput.length !== 4) { + return false; + } + + const numbers = userInput.split(''); + + if (!numbers.every((num) => /^\d$/.test(num))) { + return false; + } + + if (userInput[0] === '0') { + return false; + } + + const uniqueNumber = new Set(numbers); + + return uniqueNumber.size === 4; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..e64b7ebe 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -4,10 +4,22 @@ * Generate a random 4-digit number that does not start with 0 * and does not contain any duplicate digits. * - * @return {number} A random 4-digit number + * @return {number} */ function generateRandomNumber() { - /* Write your code here */ + const numbers = []; + + numbers.push(Math.floor(Math.random() * 9) + 1); + + while (numbers.length < 4) { + const randomNumber = Math.floor(Math.random() * 10); + + if (!numbers.includes(randomNumber)) { + numbers.push(randomNumber); + } + } + + return Number(numbers.join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..c96867de 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -7,13 +7,39 @@ * Assume that the user input and the number to guess * are always 4-digit numbers. * - * @param {number} userInput - The user input - * @param {number} numberToGuess - The number to guess - * @return {object} An object containing the number of bulls and cows. - * Example: { bulls: 1, cows: 2 } + * @param {string} userInput + * @param {string} numberToGuess + * @return {object} */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const fixedUserInput = String(userInput); + const fixedNumberToGuess = String(numberToGuess); + + let bulls = 0; + let cows = 0; + + const userNumbers = []; + const guessNumbers = []; + + for (let i = 0; i < 4; i++) { + if (fixedUserInput[i] === fixedNumberToGuess[i]) { + bulls++; + } else { + userNumbers.push(fixedUserInput[i]); + guessNumbers.push(fixedNumberToGuess[i]); + } + } + + guessNumbers.forEach((number) => { + const cowIndex = userNumbers.indexOf(number); + + if (cowIndex !== -1) { + cows++; + userNumbers.splice(cowIndex, 1); + } + }); + + return { bulls, cows }; } module.exports = {