Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add solution #414

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -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
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,5 +27,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"readline": "^1.3.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
36 changes: 36 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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();
23 changes: 23 additions & 0 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
20 changes: 19 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading