Skip to content

Commit bdcc131

Browse files
committed
added changes to game files and with general lodic
1 parent 5a13cc9 commit bdcc131

File tree

7 files changed

+179
-39
lines changed

7 files changed

+179
-39
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### Hexlet tests and linter status:
22
[![Actions Status](https://github.com/KateChe31/frontend-project-44/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/KateChe31/frontend-project-44/actions)
33
[![Maintainability](https://api.codeclimate.com/v1/badges/4a5616b22f34629087fa/maintainability)](https://codeclimate.com/github/KateChe31/frontend-project-44/maintainability)
4-
## Демонстрация работы
5-
[Смотреть запись в терминале]( https://asciinema.org/connect/a69561bc-070b-40e4-9a31-cfc173f7f842)
4+
## Демонстрация работы brain-even.js
5+
[Смотреть запись в терминале]( https://asciinema.org/connect/a69561bc-070b-40e4-9a31-cfc173f7f842)
6+
## Демонстрация работы brain-calc.js
7+
[Смотреть запись в терминале]( https://asciinema.org/a/5OadaYOMLkEyfb4kMb1yoFihC)

bin/brain-calc.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import playGame from '../src/index.js';
2+
3+
const getRandomExpression = () => {
4+
const num1 = Math.floor(Math.random() * 100) + 1;
5+
const num2 = Math.floor(Math.random() * 100) + 1;
6+
const operations = ['+', '-', '*'];
7+
const operation = operations[Math.floor(Math.random() * operations.length)];
8+
const expression = `${num1} ${operation} ${num2}`;
9+
let correctAnswer;
10+
11+
switch (operation) {
12+
case '+':
13+
correctAnswer = num1 + num2;
14+
break;
15+
case '-':
16+
correctAnswer = num1 - num2;
17+
break;
18+
case '*':
19+
correctAnswer = num1 * num2;
20+
break;
21+
}
22+
return { question: expression, correctAnswer };
23+
};
24+
25+
const playGameCalc = () => {
26+
const gameQuestion = 'What is the result of the expression?';
27+
playGame(getRandomExpression, gameQuestion);
28+
};
29+
30+
playGameCalc();

bin/brain-even.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
1-
import readlineSync from "readline-sync";
1+
import playGame from '../src/index.js';
2+
23
const getRandomNumber = () => Math.floor(Math.random() * 100) + 1;
34
const isEven = (number) => number % 2 === 0;
45

5-
const playGame = () => {
6-
console.log("Welcome to the Brain Games!");
7-
const userName = readlineSync.question("May I have your name?");
8-
console.log("Hello, " + userName + "!");
9-
console.log('Answer "yes" if the number is even, otherwise answer "no".');
10-
11-
let correctAnswers = 0;
12-
13-
while (correctAnswers < 3) {
14-
const number = getRandomNumber();
15-
console.log('Question:' + number);
16-
const userAnswer = readlineSync.question('Your answer: ');
17-
18-
const correctAnswer = isEven(number) ? 'yes' : 'no';
19-
20-
if (userAnswer !== 'yes' && userAnswer !== 'no') {
21-
console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.`);
22-
console.log(`Let's try again, ${userName}!`);
23-
return;
24-
}
6+
const getQuestionAndAnswer = () => {
7+
const number = getRandomNumber();
8+
const question = number;
9+
const correctAnswer = isEven(number) ? 'yes' : 'no';
10+
return { question, correctAnswer };
11+
};
2512

26-
if (userAnswer === correctAnswer) {
27-
console.log('Correct!');
28-
correctAnswers += 1;
29-
} else {
30-
console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.`);
31-
console.log(`Let's try again, ${userName}!`);
32-
return;
33-
}
34-
}
35-
console.log(`Congratulations, ${userName}!`);
13+
const playGameEven = () => {
14+
const gameQuestion = 'Answer "yes" if the number is even, otherwise answer "no".';
15+
playGame(getQuestionAndAnswer, gameQuestion);
3616
};
37-
playGame();
17+
18+
playGameEven();

bin/demo1.cast

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{"version": 2, "width": 120, "height": 30, "timestamp": 1740220819, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.010112, "o", "\u001b[?2004h\u001b]0;katerina_che@LAPTOP-V6LNI5O7: ~/frontend-project-44/bin\u0007\u001b[01;32mkaterina_che@LAPTOP-V6LNI5O7\u001b[00m:\u001b[01;34m~/frontend-project-44/bin\u001b[00m$ \r\u001b[K\u001b]0;katerina_che@LAPTOP-V6LNI5O7: ~/frontend-project-44/bin\u0007\u001b[01;32mkaterina_che@LAPTOP-V6LNI5O7\u001b[00m:\u001b[01;34m~/frontend-project-44/bin\u001b[00m$ "]
3+
[2.309966, "o", "n"]
4+
[2.7254, "o", "o"]
5+
[3.178708, "o", "d"]
6+
[3.494521, "o", "e"]
7+
[4.431263, "o", " "]
8+
[5.231598, "o", "b"]
9+
[5.69753, "o", "r"]
10+
[6.046941, "o", "a"]
11+
[6.359245, "o", "i"]
12+
[6.740626, "o", "n"]
13+
[7.68883, "o", "-"]
14+
[8.207777, "o", "c"]
15+
[8.425244, "o", "a"]
16+
[8.820246, "o", "l"]
17+
[9.918523, "o", "c"]
18+
[10.774873, "o", "."]
19+
[11.285921, "o", "j"]
20+
[12.187154, "o", "s"]
21+
[13.444899, "o", "\r\n\u001b[?2004l\r"]
22+
[13.495011, "o", "Welcome to the Brain Games!\r\n"]
23+
[13.497796, "o", "May I have your name?"]
24+
[19.405783, "o", "K"]
25+
[19.876511, "o", "a"]
26+
[20.166502, "o", "t"]
27+
[20.475788, "o", "e"]
28+
[25.518499, "o", "\r\n"]
29+
[25.519428, "o", "Hello, Kate!\r\n"]
30+
[25.519778, "o", "What result of the expression?\r\n"]
31+
[25.520089, "o", "Question: 50 - 24\r\n"]
32+
[25.520262, "o", "Your answer: "]
33+
[33.390456, "o", "2"]
34+
[33.661932, "o", "6"]
35+
[34.712087, "o", "\r\n"]
36+
[34.712715, "o", "Correct!\r\n"]
37+
[34.712981, "o", "What result of the expression?\r\nQuestion: 19 * 18\r\n"]
38+
[34.713371, "o", "Your answer: "]
39+
[39.633719, "o", "3"]
40+
[39.911524, "o", "4"]
41+
[40.157864, "o", "2"]
42+
[41.21449, "o", "\r\n"]
43+
[41.214849, "o", "Correct!\r\n"]
44+
[41.215247, "o", "What result of the expression?\r\n"]
45+
[41.215907, "o", "Question: 19 + 67\r\n"]
46+
[41.216131, "o", "Your answer: "]
47+
[48.06504, "o", "8"]
48+
[48.319312, "o", "6"]
49+
[49.260127, "o", "\r\n"]
50+
[49.260548, "o", "Correct!\r\nCongratulations, Kate!\r\n"]
51+
[49.267439, "o", "\u001b[?2004h\u001b]0;katerina_che@LAPTOP-V6LNI5O7: ~/frontend-project-44/bin\u0007\u001b[01;32mkaterina_che@LAPTOP-V6LNI5O7\u001b[00m:\u001b[01;34m~/frontend-project-44/bin\u001b[00m$ "]
52+
[50.648858, "o", "n"]
53+
[51.035942, "o", "o"]
54+
[53.072195, "o", "d"]
55+
[53.326478, "o", "e"]
56+
[53.753206, "o", " "]
57+
[54.215863, "o", "b"]
58+
[54.663661, "o", "r"]
59+
[54.977528, "o", "a"]
60+
[55.619913, "o", "i"]
61+
[56.026596, "o", "n"]
62+
[56.72126, "o", "-"]
63+
[57.284531, "o", "c"]
64+
[57.663234, "o", "a"]
65+
[58.124159, "o", "l"]
66+
[58.626323, "o", "c"]
67+
[59.518566, "o", "."]
68+
[60.29206, "o", "j"]
69+
[60.508692, "o", "s"]
70+
[62.065053, "o", "\r\n\u001b[?2004l\r"]
71+
[62.131173, "o", "Welcome to the Brain Games!\r\n"]
72+
[62.136368, "o", "May I have your name?"]
73+
[63.521643, "o", "K"]
74+
[63.972533, "o", "a"]
75+
[64.123264, "o", "t"]
76+
[64.343837, "o", "e"]
77+
[64.948031, "o", "\r\n"]
78+
[64.948873, "o", "Hello, Kate!\r\n"]
79+
[64.949176, "o", "What result of the expression?\r\n"]
80+
[64.949386, "o", "Question: 33 + 10\r\nYour answer: "]
81+
[66.831348, "o", "4"]
82+
[67.32356, "o", "3"]
83+
[68.151041, "o", "\r\n"]
84+
[68.151449, "o", "Correct!\r\n"]
85+
[68.151647, "o", "What result of the expression?\r\n"]
86+
[68.151764, "o", "Question: 79 + 84\r\n"]
87+
[68.152059, "o", "Your answer: "]
88+
[71.580927, "o", "1"]
89+
[72.338416, "o", "6"]
90+
[72.70854, "o", "5"]
91+
[73.504621, "o", "\r\n"]
92+
[73.504909, "o", "165 is wrong answer ;(. Correct answer was 163.\r\n"]
93+
[73.505227, "o", "Let's try again, Kate!\r\n"]
94+
[73.512585, "o", "\u001b[?2004h\u001b]0;katerina_che@LAPTOP-V6LNI5O7: ~/frontend-project-44/bin\u0007\u001b[01;32mkaterina_che@LAPTOP-V6LNI5O7\u001b[00m:\u001b[01;34m~/frontend-project-44/bin\u001b[00m$ "]
95+
[76.741681, "o", "\u001b[?2004l\r\r\nexit\r\n"]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"bin": {
88
"code": "bin/brain-games.js",
99
"brain-games": "bin/brain-games.js",
10-
"brain-even": "bin/brain-even.js"
10+
"brain-even": "bin/brain-even.js",
11+
"brain-calc": "bin/brain-calc.js"
1112
},
1213
"scripts": {
1314
"test": "echo \"Error: no test specified\" && exit 1"

src/cli.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import readlineSync from "readline-sync";
2-
console.log("Welcome to the Brain Games!");
3-
const userName = readlineSync.question("May I have your name?");
4-
console.log("Hello, " + userName + "!");
5-
export default userName;
2+
3+
const greetings =() => {
4+
console.log("Welcome to the Brain Games!");
5+
const userName = readlineSync.question("May I have your name?");
6+
console.log("Hello, " + userName + "!");
7+
return userName;
8+
};
9+
export default greetings;

src/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import readlineSync from "readline-sync";
2+
import greetings from '../src/cli.js';
3+
4+
const playGame = (getQuestionAndAnswer, gameQuestion, roundsCount = 3) => {
5+
const userName = greetings();
6+
console.log(gameQuestion);
7+
let correctAnswers = 0;
8+
9+
while (correctAnswers < roundsCount) {
10+
const { question, correctAnswer } = getQuestionAndAnswer();
11+
console.log('Question: ' + question);
12+
const userAnswer = readlineSync.question('Your answer: ');
13+
14+
if (userAnswer !== String(correctAnswer)) {
15+
console.log(`${userAnswer} is wrong answer ;(. Correct answer was ${correctAnswer}.`);
16+
console.log(`Let's try again, ${userName}!`);
17+
return;
18+
}
19+
20+
console.log('Correct!');
21+
correctAnswers += 1;
22+
}
23+
24+
console.log(`Congratulations, ${userName}!`);
25+
};
26+
27+
export default playGame;

0 commit comments

Comments
 (0)