From e6155a05c18d3dcb0b7e488a623a17987c6e8061 Mon Sep 17 00:00:00 2001 From: Vladyslav Shylin Date: Wed, 23 Oct 2024 14:52:45 +0300 Subject: [PATCH] add task solution --- .github/workflows/test.yml-template | 23 ++++++++++++++++++ package-lock.json | 9 +++---- package.json | 2 +- src/app.js | 35 ++++++++++++++++++++++++++++ src/modules/checkIsValidUserInput.js | 18 ++++++++++++++ src/modules/generateRandomNumber.js | 14 ++++++++++- src/modules/getBullsAndCows.js | 20 ++++++++++++++++ 7 files changed, 115 insertions(+), 6 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..127f2c10 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,38 @@ 'use strict'; // Write your code here +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..189df5bc 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -10,6 +10,24 @@ */ 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..1edd55c2 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 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..5852cf58 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -14,6 +14,26 @@ */ function getBullsAndCows(userInput, numberToGuess) { /* Write your code here */ + const userInputStr = userInput.toString(); + const numberToGuessStr = numberToGuess.toString(); + + let bulls = 0; + let cows = 0; + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] === numberToGuessStr[i]) { + bulls++; + } + + if ( + userInputStr[i] !== numberToGuessStr[i] && + numberToGuessStr.includes(userInputStr[i]) + ) { + cows++; + } + } + + return { bulls, cows }; } module.exports = {