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

The task is completed #233

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

const { getBullsAndCows } = require('./bullsAndCows');
const { generateNumber } = require('./generateNumber');
const { terminal } = require('./terminal');
const { isNumberValid } = require('./validateNumber');

const generatedNumber = String(generateNumber());

const startGame = () => {
terminal.question('Try to quess the number:\n', (enteredNumber) => {
if (!isNumberValid(enteredNumber)) {
console.log('Invalid number\n');
startGame();
} else {
const bullsAndCows = getBullsAndCows(generatedNumber, enteredNumber);
const bulls = bullsAndCows[0];
const cows = bullsAndCows[1];

console.log(`Bulls: ${bulls}, Cows: ${cows}\n`);

if (bulls === 4) {
console.log('Congratulations, you win!');
terminal.close();
} else {
startGame();
}
}
});
};

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

const NUM_LENGTH = 4;

module.exports.getBullsAndCows = (numberToGuess, enteredNumber) => {
const numberToGuessCopy = numberToGuess.split('');
const enteredNumberCopy = enteredNumber.split('');
let bullsCount = 0;
let cowsCount = 0;

for (let i = 0; i < NUM_LENGTH; i++) {
const isBull = enteredNumberCopy[0] === numberToGuessCopy[0];
const isCow = numberToGuess.includes(enteredNumberCopy[0]);

if (isBull) {
bullsCount++;
} else if (isCow) {
cowsCount++;
}

numberToGuessCopy.shift();
enteredNumberCopy.shift();
}

Choose a reason for hiding this comment

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

Suggested change
for (let i = 0; i < NUM_LENGTH; i++) {
const isBull = enteredNumberCopy[0] === numberToGuessCopy[0];
const isCow = numberToGuess.includes(enteredNumberCopy[0]);
if (isBull) {
bullsCount++;
} else if (isCow) {
cowsCount++;
}
numberToGuessCopy.shift();
enteredNumberCopy.shift();
}
for (let i = 0; i < NUM_LENGTH; i++) {
const isBull = enteredNumberCopy[0] === numberToGuessCopy[i];
const isCow = numberToGuess.includes(enteredNumberCopy[i]);
if (isBull) {
bullsCount++;
} else if (isCow) {
cowsCount++;
}
}


return [bullsCount, cowsCount];
};
17 changes: 17 additions & 0 deletions src/generateNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const NUM_LENGTH = 4;

module.exports.generateNumber = () => {
const digitsArray = [];

while (digitsArray.length < NUM_LENGTH) {
const numToPush = Math.floor(Math.random() * 10);

if (!digitsArray.includes(numToPush)) {
digitsArray.push(numToPush);
}
}

return digitsArray.join('');
};

Choose a reason for hiding this comment

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

you func cant create number with duplicate number and can create number that start from 0, that not so logic

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

const readline = require('readline');

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

module.exports = { terminal };
13 changes: 13 additions & 0 deletions src/validateNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const NUM_LENGTH = 4;

module.exports.isNumberValid = (number) => {
for (let i = 1; i < NUM_LENGTH; i++) {
if (number[i] === number[i - 1]) {
return false;
}

Choose a reason for hiding this comment

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

this redundant but why do you think that the same number will be placed only together

}

return !isNaN(+number) && number.length === NUM_LENGTH;
};
Loading