From 820dc2780b96925203f4913d73f94087c51a7925 Mon Sep 17 00:00:00 2001 From: Nazar Pastuschak Date: Fri, 1 Nov 2024 16:36:43 +0200 Subject: [PATCH 1/2] add task solution --- .github/workflows/test.yml-template | 23 ++++++++++++++++ package-lock.json | 9 ++++--- package.json | 2 +- src/app.js | 40 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 17 +++++++++++- src/modules/generateRandomNumber.js | 14 +++++++++- src/modules/getBullsAndCows.js | 18 ++++++++++++- 7 files changed, 114 insertions(+), 9 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..631641b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,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", @@ -1467,10 +1467,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", diff --git a/package.json b/package.json index 216da526..5847534c 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", diff --git a/src/app.js b/src/app.js index e89a2d97..7f5b0a20 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,41 @@ 'use strict'; -// Write your code here +const readline = require('node:readline'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput.js'); +const { generateRandomNumber } = require('./modules/generateRandomNumber.js'); +const { getBullsAndCows } = require('./modules/getBullsAndCows.js'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const print = (message) => rl.write(message + '\n'); + +const secretNumber = generateRandomNumber(); + +// Функція, яка запускає одну спробу гри +function playRound() { + rl.question('Введіть ваше число: ', (guess) => { + const validInput = checkIsValidUserInput(guess); + + if (!validInput) { + print('невалідне значення'); + playRound(); + } + + if (validInput && guess !== secretNumber.toString()) { + const { bulls, cows } = getBullsAndCows(guess, secretNumber); + + print(`Бики: ${bulls}, Корови: ${cows}`); + playRound(); // Запускаємо ще один раунд + } + + if (guess === secretNumber.toString() && validInput) { + print(`Вітаємо! Ви вгадали число ${secretNumber}!`); + rl.close(); + } + }); +} + +playRound(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..221ba518 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,7 +9,22 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + const validInput = new Set(userInput); + const firstValue = Array.from(validInput)[0]; + + if (validInput.size !== 4) { + return false; + } + + if (firstValue === '0') { + return false; + } + + if (isNaN(+userInput)) { + return false; + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..8acac010 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,7 +7,19 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + const digits = new Set(); + + while (digits.size < 4) { + const digit = Math.floor(Math.random() * 10).toString(); + + if (digits.size === 0 && digit === '0') { + continue; + } + + digits.add(digit); + } + + return parseInt(Array.from(digits).join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..c17b0906 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,23 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const userInputStr = userInput.toString(); + const numberToGuessStr = numberToGuess.toString(); + + const result = { + bulls: 0, + cows: 0, + }; + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] === numberToGuessStr[i]) { + result.bulls++; + } else if (numberToGuessStr.includes(userInputStr[i])) { + result.cows++; + } + } + + return result; } module.exports = { From 906f6f40a715e241396e4f167d6cfc80b7d6d543 Mon Sep 17 00:00:00 2001 From: Nazar Pastuschak Date: Fri, 1 Nov 2024 16:58:48 +0200 Subject: [PATCH 2/2] fix --- src/app.js | 2 +- src/modules/checkIsValidUserInput.js | 16 +++++++--------- src/modules/generateRandomNumber.js | 11 ++++++----- src/modules/getBullsAndCows.js | 16 ++++++++++++++-- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/app.js b/src/app.js index 7f5b0a20..9b7c71dc 100644 --- a/src/app.js +++ b/src/app.js @@ -19,7 +19,7 @@ function playRound() { rl.question('Введіть ваше число: ', (guess) => { const validInput = checkIsValidUserInput(guess); - if (!validInput) { + if (!checkIsValidUserInput(guess)) { print('невалідне значення'); playRound(); } diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 221ba518..f60f837a 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,22 +9,20 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - const validInput = new Set(userInput); - const firstValue = Array.from(validInput)[0]; - - if (validInput.size !== 4) { + // Перевірка, що це 4-значне число + if (userInput.length !== 4 || isNaN(+userInput)) { return false; } - if (firstValue === '0') { + // Перевірка, що число не починається з 0 + if (userInput[0] === '0') { return false; } - if (isNaN(+userInput)) { - return false; - } + // Перевірка на дублікати за допомогою Set + const uniqueDigits = new Set(userInput.split('')); - return true; + return uniqueDigits.size === 4; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 8acac010..67b71aaa 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,19 +7,20 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - const digits = new Set(); + const digits = []; - while (digits.size < 4) { + while (digits.length < 4) { const digit = Math.floor(Math.random() * 10).toString(); - if (digits.size === 0 && digit === '0') { + // Перевіряємо, що перша цифра не є 0 та цифра ще не додана + if ((digits.length === 0 && digit === '0') || digits.includes(digit)) { continue; } - digits.add(digit); + digits.push(digit); } - return parseInt(Array.from(digits).join('')); + return parseInt(digits.join(''), 10); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index c17b0906..a911e977 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -21,11 +21,23 @@ function getBullsAndCows(userInput, numberToGuess) { cows: 0, }; + const checkedPositions = new Array(4).fill(false); + for (let i = 0; i < 4; i++) { if (userInputStr[i] === numberToGuessStr[i]) { result.bulls++; - } else if (numberToGuessStr.includes(userInputStr[i])) { - result.cows++; + checkedPositions[i] = true; + } + } + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] !== numberToGuessStr[i]) { + const cowIndex = numberToGuessStr.indexOf(userInputStr[i]); + + if (cowIndex !== -1 && !checkedPositions[cowIndex]) { + result.cows++; + checkedPositions[cowIndex] = true; + } } }