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 bulls and cows game #432

Open
wants to merge 2 commits 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
42 changes: 41 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
/* eslint-disable no-console */
'use strict';

// Write your code here
const readline = require('node:readline');

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

const randomNum = generateRandomNumber();

console.log('Random number:', randomNum);
askQuestion(randomNum);

function askQuestion(randomNumber) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const promptUser = () => {
rl.question('Guess the number:\n', (userNumber) => {
if (checkIsValidUserInput(userNumber)) {
const userTry = getBullsAndCows(userNumber, randomNumber);

console.log(userTry);

if (userTry.bulls === 4) {
console.log('You win!');
rl.close();
} else {
console.log('Try again');
promptUser();
}
} else {
console.log('Number should include 4 unique digits from 1 to 9');
promptUser();
}
});
};

promptUser();
}
20 changes: 19 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (userInput.startsWith('0')) {
return false;
}

if (userInput.length !== 4) {
return false;
}

if (Number.isNaN(Number(userInput))) {
return false;
}

for (let i = 0; i < 3; i++) {
if (userInput.includes(userInput[i], i + 1)) {
return false;
}
}

return true;
}

module.exports = {
Expand Down
14 changes: 13 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const availableDigits = [...digits];
let number = '';

for (let i = 0; i < 4; i++) {
const randomIndex = Math.floor(Math.random() * availableDigits.length);
const randomDigit = availableDigits[randomIndex];

number = number.concat(randomDigit);
availableDigits.splice(randomIndex, 1);
}

return Number(number);
}

module.exports = {
Expand Down
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 */
let bulls = 0;
let cows = 0;

const userNumbers = Array.from(String(userInput), (num) => +num);
const gameNumbers = Array.from(String(numberToGuess), (num) => +num);

gameNumbers.forEach((n, ind) => {
if (n === userNumbers[ind]) {
bulls += 1;
}
});

gameNumbers.forEach((n, ind) => {
if (userNumbers.includes(n) && ind !== userNumbers.indexOf(n)) {
cows += 1;
}
});

return { bulls, cows };
}

module.exports = {
Expand Down
Loading