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

added solution #234

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
/* eslint-disable no-console */
'use strict';

const generateRandomNumber = require('./generateRandomNumber');
const handlePlayRound = require('./handlePlayRound');

const handlePlayGame = () => {
const programNumber = generateRandomNumber();

handlePlayRound(programNumber);

console.log(
'Welcome to the Bulls and Cows game!',
'Try to guess the 4-digit number with unique digits:',
);
};

handlePlayGame();
22 changes: 22 additions & 0 deletions src/generateRandomNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const NUMBER_LENGTH = 4;
const MAX_RANGE_VALUE = 10;

function generateRandomNumber() {
const usedDigits = new Set();
let result = '';

while (usedDigits.size < NUMBER_LENGTH) {
const randomDigit = Math.floor(Math.random() * MAX_RANGE_VALUE);

if (!usedDigits.has(randomDigit)) {
usedDigits.add(randomDigit);
result += randomDigit;
}
}

return result;
}

module.exports = generateRandomNumber;
21 changes: 21 additions & 0 deletions src/getBullsAndCows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const getBullsAndCows = (programNumber, userNumber) => {
let bulls = 0;
let cows = 0;

for (let i = 0; i < programNumber.length; i++) {
if (userNumber[i] === programNumber[i]) {
bulls++;
} else if (programNumber.includes(userNumber[i])) {
cows++;
}
}

return {
bulls,
cows,
};
};

module.exports = getBullsAndCows;
38 changes: 38 additions & 0 deletions src/handlePlayRound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable no-console */
'use strict';

const getBullsAndCows = require('./getBullsAndCows');
const validateInput = require('./validateInput');
const inputAndOutputOperations = require('./inputAndOutputOperations');

const handlePlayRound = (programNumber) => {
inputAndOutputOperations.question('', (userNumber) => {
const isInputValid = validateInput(userNumber);
const result = getBullsAndCows(programNumber, userNumber);

if (!isInputValid) {
console.log('Please enter a valid 4-digit number with unique digits.');
handlePlayRound(programNumber);

return;
}

if (result.bulls === 4) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (result.bulls === 4) {
if (result.bulls === NUMBER_LENGTH) {

console.log(`Congratulations! You guessed the ${programNumber}!`);

inputAndOutputOperations.close();
} else {
console.log(
`The result is ${result.bulls} ${
result.bulls === 1 ? 'bull' : 'bulls'
} and ${result.cows} ${
result.cows === 1 ? 'cow' : 'cows'
}.\nEnter your guess: `,
);

handlePlayRound(programNumber);
}
});
};

module.exports = handlePlayRound;
10 changes: 10 additions & 0 deletions src/inputAndOutputOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const readline = require('readline');

const inputAndOutputOperations = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

module.exports = inputAndOutputOperations;
7 changes: 7 additions & 0 deletions src/validateInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const validateInput = (input) => {
return /^\d{4}$/.test(input) && new Set(input).size === 4;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return /^\d{4}$/.test(input) && new Set(input).size === 4;
return /^\d{4}$/.test(input) && new Set(input).size === NUMBER_LENGTH;

};

module.exports = validateInput;
Loading