-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameLogic.js
More file actions
42 lines (34 loc) · 1.62 KB
/
gameLogic.js
File metadata and controls
42 lines (34 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// game.js
const readline = require('node:readline');
const { stdin: input, stdout: output } = require('node:process');
const rl = readline.createInterface({ input, output });
const { getRadicalNum, fetchCharacters, checksRadicals, getAllRadicals } = require('./radicals');
const { getRandom, addSpace } = require('./utils.js');
const { getDefinition } = require('./definition.js');
let accumlatedPoints = 0;
let hintGiven = false;
function startGame(currentRandomRadical) {
rl.question(`What character has the radical: ${currentRandomRadical}? \n> `, async (answer) => {
let numOfRadical = getRadicalNum(currentRandomRadical);
const allCharasWithRadical = await fetchCharacters(numOfRadical);
if (answer === "hint") {
let definition = getDefinition(currentRandomRadical);
console.log(`Here's the definition of ${currentRandomRadical}: ${definition}`);
hintGiven = true;
//prompt the user again so it doesnt exit the game
startGame(currentRandomRadical);
return;
}
if (checksRadicals(answer, allCharasWithRadical)) {
console.log(`${addSpace}-----------------------\n good job! here's the next question:`);
accumlatedPoints++;
hintGiven = false;
let randomRadicalEverytime = getRandom(getAllRadicals());
startGame(randomRadicalEverytime);
} else {
console.log(`${addSpace}-----------------------\n you lost! Here's how many you got right: ${accumlatedPoints}`);
rl.close();
};
});
}
module.exports = { startGame };