-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (74 loc) · 2.44 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const prompt = require('prompt-sync')();
const rng = require('random-number');
const { topics } = require("./data.json");
const ITEMS_PER_ROUND = 5
clear()
game()
function game(){
clear()
displayTopics()
// make sure a random topic is chosen if no topic was provided
let topic = topics[limit(Number(prompt(`Choose a topic or leave empty (0-${topics.length-1}): `)), 0, topics.length-1) || rng({min: 0, max: topics.length-1, integer:true})];
console.log(`Your Chosen Topic Is... ${topic.topic}!`);
sleep(1000)
choices = [undefined]
for (let i = 0; i < ITEMS_PER_ROUND; i++) {
clear()
console.table(choices)
let done = false
while(!done){
const chosen = topic.items[rng({min:0, max:topic.items.length-1, integer:true})]
if(!choices.includes(chosen)){
choices = getInput(chosen, choices)
done = true
}
}
}
console.table(choices)
}
function displayTopics(){
console.table([...topics].map(value => {return {topic: value.topic}}))
}
function limit(num, min, max){
const MIN = min;
const MAX = max;
const parsed = parseInt(num);
return Math.min(Math.max(parsed, MIN), MAX);
}
function clear(noHeader = false){
console.clear()
if(noHeader) return
console.log(`
#### # ### # # ### ### # # # # #
# # # # ## # # # # # # # ## # # #
#### # # # ## # # # # # # # ## ##
# # # # # # # # ### ##### # # # #
# # # # # # # # # # # # # # # #
#### #### ### # # ### # # # # # # # #
Write "exit" To Exit The Game
`)
}
function getInput(item, choices){
let done = false
while(!done){
const input = prompt(`Where Does ${item} belong to? (1-${ITEMS_PER_ROUND}) `)
if(input == "exit") {
clear(true)
process.exit()
}
if(Number(isNaN(input))) continue
if(limit(input, 1, ITEMS_PER_ROUND) != input) continue
if(choices[input]){
console.log("That Place Is Occupied By " + choices[input])
continue
}
done = true
choices[input] = item
}
return choices
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}