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..9b7c71dc 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 (!checkIsValidUserInput(guess)) { + 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..f60f837a 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,7 +9,20 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + // Перевірка, що це 4-значне число + if (userInput.length !== 4 || isNaN(+userInput)) { + return false; + } + + // Перевірка, що число не починається з 0 + if (userInput[0] === '0') { + return false; + } + + // Перевірка на дублікати за допомогою Set + const uniqueDigits = new Set(userInput.split('')); + + return uniqueDigits.size === 4; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..67b71aaa 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,7 +7,20 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + const digits = []; + + while (digits.length < 4) { + const digit = Math.floor(Math.random() * 10).toString(); + + // Перевіряємо, що перша цифра не є 0 та цифра ще не додана + if ((digits.length === 0 && digit === '0') || digits.includes(digit)) { + continue; + } + + digits.push(digit); + } + + return parseInt(digits.join(''), 10); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..a911e977 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,35 @@ * 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, + }; + + const checkedPositions = new Array(4).fill(false); + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] === numberToGuessStr[i]) { + result.bulls++; + 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; + } + } + } + + return result; } module.exports = {