From 0a342423b2fbbced574f2809c82ad9cc3cc31b28 Mon Sep 17 00:00:00 2001 From: Oleksandr Robenko Date: Fri, 27 Sep 2024 16:34:03 +0300 Subject: [PATCH] add solution --- .github/workflows/test.yml-template | 23 ++++++++++++++++++ package-lock.json | 18 ++++++++++---- package.json | 5 +++- readme.md | 2 +- src/app.js | 36 ++++++++++++++++++++++++++++ src/modules/checkIsValidUserInput.js | 23 ++++++++++++++++++ src/modules/generateRandomNumber.js | 18 +++++++++++++- src/modules/getBullsAndCows.js | 20 +++++++++++++++- 8 files changed, 137 insertions(+), 8 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/readme.md b/readme.md index b0d82156..f29ef6df 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # The "Bulls & Cows" game -**Read [the guideline](https://github.com/mate-academy/js_task-guideline/blob/master/README.md) before start** +**Read [the guideline](https://github.com/mate-academy/js_tasknpm-guideline/blob/master/README.md) before start** Implement `Bulls and cows` game so the user can run and play it using command line. diff --git a/src/app.js b/src/app.js index e89a2d97..624e83b0 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,39 @@ 'use strict'; // Write your code here +const readline = require('readline'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); +const { generateRandomNumber } = require('./modules/generateRandomNumber'); +const { getBullsAndCows } = require('./modules/getBullsAndCows'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const randomNum = generateRandomNumber(); + +function playGame() { + rl.question('Make your guess: ', (answer) => { + if (!checkIsValidUserInput(answer)) { + // eslint-disable-next-line + console.log('Please enter a valid 4 digit number that not starts with 0'); + + playGame(); + } else { + const bullsAndCows = getBullsAndCows(answer, randomNum); + // eslint-disable-next-line + console.log(`Bulls: ${bullsAndCows.bulls}, Cows: ${bullsAndCows.cows}`); + + if (bullsAndCows.bulls === 4) { + // eslint-disable-next-line + console.log('Congratulations! You guessed the correct number.'); + rl.close(); + } else { + playGame(); + } + } + }); +} + +playGame(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..db0cecac 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -10,6 +10,29 @@ */ function checkIsValidUserInput(userInput) { /* Write your code here */ + const numbers = '0123456789'; + + if (userInput.length !== 4) { + return false; + } + + if (userInput[0] === '0') { + return false; + } + + for (let i = 0; i < userInput.length - 1; i++) { + if (!numbers.includes(userInput[i])) { + return false; + } + + for (let j = i + 1; j < userInput.length; j++) { + if (userInput[i] === userInput[j]) { + return false; + } + } + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..99fb09e7 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,9 +7,25 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + const digits = []; + + digits.push(randomNumber(1, 9)); + + while (digits.length < 4) { + const nextDigit = randomNumber(0, 9); + + if (!digits.includes(nextDigit)) { + digits.push(nextDigit); + } + } + + return parseInt(digits.join(''), 10); } +const randomNumber = (min, max) => { + return Math.floor(Math.random() * (max - min + 1)) + min; +}; + module.exports = { generateRandomNumber, }; diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..7ad697f1 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,25 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const userDigits = userInput.toString().split(''); + const guessDigits = numberToGuess.toString().split(''); + const result = { + bulls: 0, + cows: 0, + }; + + for (let i = 0; i < userDigits.length; i++) { + if (guessDigits.includes(userDigits[i])) { + result.cows++; + } + + if (userDigits[i] === guessDigits[i]) { + result.bulls++; + result.cows--; + } + } + + return result; } module.exports = {