Skip to content

Commit

Permalink
created one func for all games - playGame
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanLiVa committed Apr 24, 2024
1 parent f24c8a4 commit a6f2fd2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 49 deletions.
9 changes: 2 additions & 7 deletions src/games/calcGame.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mainLogicGame from '../index.js';
import greeting from '../cli.js';
import { playGame } from '../index.js';

const userName = greeting('What is the result of the expression?');
const calcExpression = (operator, number1, number2) => {
Expand Down Expand Up @@ -27,12 +27,7 @@ const generateExpression = () => {
};

const playCalcGame = () => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) return;
}
console.log(`Congratulations, ${userName}!`);
playGame(generateExpression, userName);
};

export default playCalcGame;
11 changes: 2 additions & 9 deletions src/games/evGame.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import greeting from '../cli.js';
import mainLogicGame from '../index.js';
import { playGame } from '../index.js';
import getRandomNumber from '../randomNumber.js';

const userName = greeting('Answer "yes" if the number is even, otherwise answer "no".');
Expand All @@ -11,14 +11,7 @@ const generateExpression = () => {
};

const playEvGame = () => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) {
return;
}
}
console.log(`Congratulations, ${userName}!`);
playGame(generateExpression, userName);
};

export default playEvGame;
11 changes: 2 additions & 9 deletions src/games/gcdGame.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mainLogicGame from '../index.js';
import { playGame } from '../index.js';
import greeting from '../cli.js';
import getRandomNumber from '../randomNumber.js';

Expand Down Expand Up @@ -48,14 +48,7 @@ const generateExpression = () => {
};

const playGcd = () => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) {
return;
}
}
console.log(`Congratulations, ${userName}!`);
playGame(generateExpression, userName);
};

export default playGcd;
20 changes: 7 additions & 13 deletions src/games/primeGame.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import greeting from '../cli.js';
import mainLogicGame from '../index.js';
import { playGame } from '../index.js';
import getRandomNumber from '../randomNumber.js';

const userName = greeting('Answer "yes" if given number is prime. Otherwise answer "no".');
Expand All @@ -12,20 +12,14 @@ const isPrime = (count) => {
return true;
};

const generateExpression = () => {
const question = getRandomNumber(2, 100);
const rightAnswer = isPrime(question) ? 'yes' : 'no';
return { question, rightAnswer };
const generatePrimeExpression = () => {
const primeQuestion = getRandomNumber(2, 100);
const rightAnswer = isPrime(primeQuestion) ? 'yes' : 'no';
return { question: primeQuestion, rightAnswer };
};

const playPrime = () => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) {
return;
}
}
console.log(`Congratulations, ${userName}!`);
playGame(generatePrimeExpression, userName);
};

export default playPrime;
11 changes: 2 additions & 9 deletions src/games/progGame.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mainLogicGame from '../index.js';
import { playGame } from '../index.js';
import greeting from '../cli.js';
import getRandomNumber from '../randomNumber.js';

Expand Down Expand Up @@ -26,14 +26,7 @@ const generateExpression = () => {
};

const playProgression = () => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) {
return;
}
}
console.log(`Congratulations, ${userName}!`);
playGame(generateExpression, userName);
};

export default playProgression;
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import readlineSync from 'readline-sync';

const mainLogicGame = (question, rightAnswer, userName) => {
export const mainLogicGame = (question, rightAnswer, userName) => {
const userAnswer = readlineSync.question(`Question: ${question}`);
console.log(`Your answer: ${userAnswer}`);
if (String(userAnswer) === String(rightAnswer)) {
Expand All @@ -12,4 +12,13 @@ const mainLogicGame = (question, rightAnswer, userName) => {
return false;
};

export default mainLogicGame;
export const playGame = (generateExpression, userName) => {
for (let i = 0; i < 3; i += 1) {
const { question, rightAnswer } = generateExpression();
const isCorrect = mainLogicGame(question, rightAnswer, userName);
if (!isCorrect) {
return;
}
}
console.log(`Congratulations, ${userName}!`);
};

0 comments on commit a6f2fd2

Please sign in to comment.