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

Develop #250

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
40 changes: 40 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
'use strict';

const { generateNumber } = require('./generateNumber');
const { calculateBullsAndCows } = require('./calculateBullsAndCows');
const { terminal } = require('./userInterface');
const { validateInput } = require('./validateInput');
const {
QUESTION,
WINNER_MESSAGE,
INVALID_INPUT_MESSAGE,
} = require('./constants');

const generatedNumber = generateNumber();

function askQuestion() {
terminal.question(QUESTION, (userNumber) => {
const isInputValid = validateInput(userNumber);

if (!isInputValid) {
terminal.write(INVALID_INPUT_MESSAGE);
askQuestion();

return;
}

if (+userNumber === generatedNumber) {
terminal.write(WINNER_MESSAGE);
terminal.close();

return;
}

const { bulls, cows } = calculateBullsAndCows(userNumber, generatedNumber);
const WRONG_GUESS_MESSAGE = `Bulls: ${bulls}, Cows: ${cows}\n`;

terminal.write(WRONG_GUESS_MESSAGE);
askQuestion();
});
}

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

const { DEFAULT_ANSWER_VALUE } = require('./constants');

function calculateBullsAndCows(userNumber, generatedNumber) {
const userNumberSplited = userNumber.split('');
const generatedStringNumber = String(generatedNumber);

return userNumberSplited.reduce((acc, number, index) => {
if (number === generatedStringNumber[index]) {
acc.bulls++;
} else if (generatedStringNumber.includes(number)) {
acc.cows++;
}

return acc;
}, { ...DEFAULT_ANSWER_VALUE });
}

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

const DEFAULT_ANSWER_VALUE = {
bulls: 0,
cows: 0,
};

const QUESTION = 'Please enter your 4-digit guess: ';
const INVALID_INPUT_MESSAGE = 'Expecting a number of 4 different digits\n';
const WINNER_MESSAGE = "Congratulations! You've guessed the number!";

const EXPECTED_NUMBER_LENGTH = 4;

module.exports = {
DEFAULT_ANSWER_VALUE,
QUESTION,
INVALID_INPUT_MESSAGE,
WINNER_MESSAGE,
EXPECTED_NUMBER_LENGTH,
};
26 changes: 26 additions & 0 deletions src/generateNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { EXPECTED_NUMBER_LENGTH } = require('./constants');

function generateNumber() {
const digitsToInsert = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const distinctDigits = [];

while (distinctDigits.length < EXPECTED_NUMBER_LENGTH) {
const randomIndex = Math.floor(Math.random() * digitsToInsert.length);

if (!distinctDigits.length && digitsToInsert[randomIndex] === 0) {
continue;
}

const digitToInsert = digitsToInsert.splice(randomIndex, 1);

distinctDigits.push(digitToInsert);
}

return +distinctDigits.join('');
}

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

const readline = require('readline');

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

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

const { EXPECTED_NUMBER_LENGTH } = require('./constants');

function validateInput(input) {
const inputSplited = input.split('');
const inputSet = new Set(inputSplited);
const hasValidLength = inputSplited.length === EXPECTED_NUMBER_LENGTH;
const hasDistinctDigits = inputSplited.length === inputSet.size;

return hasValidLength && hasDistinctDigits;
};

Choose a reason for hiding this comment

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

Also you need to check if user entered only numbers
If user enter 123q your game will never finish


module.exports = {
validateInput,
};
Loading