Skip to content

Commit

Permalink
bulls-and-cows
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsvolodya committed Sep 24, 2024
1 parent b9eb4f1 commit b32106e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 10 deletions.
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
9 changes: 5 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 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 Down
41 changes: 40 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
'use strict';

// Write your code here
const readline = require('node:readline');
const terminal = readline.createInterface(process.stdin, process.stdout);
const { getBullsAndCows } = require('./modules/getBullsAndCows');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');

async function runTerminal() {
while (true) {
const numbers = await new Promise((resolve) => {
terminal.question(
"Let's start game! Write four numbers in field:\n",
resolve,
);
});

const check = await checkIsValidUserInput(numbers);

if (!check) {
process.stdout.write('False, check numbers, try again!\n');

continue;
}

const result = await getBullsAndCows(numbers);
const resStr = JSON.stringify(result);

process.stdout.write(`Result is ${resStr}\n`);

const continueGame = await new Promise((resolve) => {
terminal.question('Do you want to continue GAME, Yes/No ?', resolve);
});

if (continueGame.toLowerCase() === 'no') {
break;
}
}

terminal.close();
}

runTerminal();
14 changes: 13 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const usersNum = String(userInput).split('');
const unic = new Set(userInput);

if (
usersNum.length !== 4 ||
!/^\d+$/.test(userInput) ||
userInput[0].includes('0') ||
unic.size < 4
) {
return false;
}

return true;
}

module.exports = {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const n = new Set();

while (n.size < 4) {
n.add(Math.ceil(Math.random() * 9));
}

return +Array.from(n).join('');
}

module.exports = {
Expand Down
22 changes: 20 additions & 2 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { generateRandomNumber } = require('./generateRandomNumber');

/**
* Calculate the number of bulls and cows for a given user input.
* Bulls are digits that are in the correct position.
Expand All @@ -12,8 +14,24 @@
* @return {object} An object containing the number of bulls and cows.
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
function getBullsAndCows(userInput, numberToGuess = generateRandomNumber()) {
const convert = (v) => String(v).split('');
const result = {
bulls: 0,
cows: 0,
};
const userGuessed = convert(userInput);
const secretNum = convert(numberToGuess);

for (let i = 0; i < userGuessed.length; i++) {
if (userGuessed[i] === secretNum[i]) {
result.bulls += 1;
} else if (userGuessed.includes(secretNum[i])) {
result.cows += 1;
}
}

return result;
}

module.exports = {
Expand Down

0 comments on commit b32106e

Please sign in to comment.