Skip to content

Commit 5cad8e3

Browse files
committed
Move games logic to /src/games according to new architecture & Add new calc-game
1 parent a52649c commit 5cad8e3

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

bin/brain-calc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import run from '../src/games/calc.js';
4+
5+
run();

src/games/calc.js

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

src/games/even.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import playGame from '../index.js';
2+
3+
const rules = 'Answer "yes" if the number is even, otherwise answer "no".';
4+
5+
const askIsEven = () => {
6+
const evenNum = Math.floor(Math.random() * 100) + 1;
7+
const correctAnswer = evenNum % 2 === 0 ? 'yes' : 'no';
8+
return [evenNum, correctAnswer];
9+
};
10+
11+
export default () => playGame(rules, askIsEven);

0 commit comments

Comments
 (0)