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 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
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 uniq digits from 1 to 9');

Choose a reason for hiding this comment

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

The error message could be more specific. Consider changing it to: 'Number should include 4 unique digits from 1 to 9.' This makes it clear that the digits must be unique and within the specified range.

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 vailableDigits = [...digits];

Choose a reason for hiding this comment

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

There is a typo in the variable name vailableDigits. It should be corrected to availableDigits for clarity and to prevent potential errors.

let number = '';

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

Choose a reason for hiding this comment

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

Ensure that the typo in vailableDigits is corrected to availableDigits here as well.

const randomDigit = vailableDigits[randomIndex];

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

Choose a reason for hiding this comment

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

Ensure that the typo in vailableDigits is corrected to availableDigits here as well.

}

return Number(number);
}

module.exports = {
Expand Down
22 changes: 21 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const bullsAndCows = {
bulls: 0,
cows: 0,
};

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

for (let i = 0; i < 4; i++) {
const number = gameNumbers[i];

if (userNumbers.includes(number)) {
if (userNumbers.indexOf(number) === i) {
bullsAndCows.bulls += 1;
} else {
bullsAndCows.cows += 1;
}
}

Choose a reason for hiding this comment

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

The current logic may incorrectly count a digit as a cow even if it has already been counted as a bull. Consider using separate loops or a mechanism to track which digits have been counted as bulls to prevent them from being counted as cows.

}

return bullsAndCows;
}

module.exports = {
Expand Down
Loading