Skip to content

Commit

Permalink
fdfd
Browse files Browse the repository at this point in the history
  • Loading branch information
Den committed Oct 13, 2024
1 parent b9eb4f1 commit bc54c03
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"readline": "^1.3.0"
}
}
37 changes: 36 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
/* eslint-disable max-len */
/* eslint-disable no-console */
'use strict';

// Write your code here
const readline = require('readline');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');
const { generateRandomNumber } = require('./modules/generateRandomNumber');

const terminal = readline.createInterface(process.stdin, process.stdout);

const randomNumber = generateRandomNumber();

const askForInput = () => {
terminal.question('Enter a number of 4 different digits: ', (number) => {
if (!checkIsValidUserInput(String(number))) {
console.log(
'Invalid input. Please enter a 4-digit number with unique digits that does not start with 0.',
);

return askForInput(); // Запрашиваем ввод снова
}

console.log(`Your input: ${number}`);

const { bulls, cows } = getBullsAndCows(+number, randomNumber);

if (bulls === 4) {
console.log('you win');
terminal.close();
} else {
console.log(`Bulls: ${bulls}, Cows: ${cows}`);
askForInput(); // Запрашиваем ввод снова
}
});
};

askForInput();
22 changes: 21 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const trimmedInput = userInput.trim();

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

if (trimmedInput[0] === '0') {
return false;
}

if (!/^\d+$/.test(trimmedInput)) {
return false;
}

const digitsSet = new Set(trimmedInput);

if (digitsSet.size !== 4) {
return false;
}

return true;
}

module.exports = {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
let result;

do {
result = Math.floor(1000 + Math.random() * 9000).toString();
} while (new Set(result).size !== 4);

return parseInt(result);
}

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

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

// Массивы для отслеживания использованных цифр
const userUsed = Array(4).fill(false);
const randomUsed = Array(4).fill(false);

// Считаем быков
for (let i = 0; i < 4; i++) {
if (strUser[i] === strRandom[i]) {
result.bulls += 1;
userUsed[i] = true; // Помечаем использованные цифры
randomUsed[i] = true; // Помечаем использованные цифры
}
}

// Считаем коров
for (let i = 0; i < 4; i++) {
if (!userUsed[i]) {
// Проверяем, не использована ли эта цифра
for (let j = 0; j < 4; j++) {
if (!randomUsed[j] && strUser[i] === strRandom[j]) {
result.cows += 1;
randomUsed[j] = true;

break;
}
}
}
}

return result;
}

module.exports = {
Expand Down

0 comments on commit bc54c03

Please sign in to comment.