-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (90 loc) · 2.73 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
102
103
104
105
106
107
108
109
110
111
112
113
114
const readline = require('readline');
require('colors');
const {
makeLogFile,
clearLogFile,
removeLogFile
} = require('./game-functions/logFileFunctions');
const writeRoundStatistics = require('./game-functions/writeRoundStatistics');
const coinToss = require('./game-functions/coinToss');
const showResults = require('./game-functions/showResults');
const inputFormatter = require('./utils/inputFormatter');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let setCount = 0;
let roundCount = 0;
let casinoWinsCount = 0;
let playerWinsCount = 0;
rl.question('Let`s play? (enter yes to start) '.cyan, (answer) => {
if (inputFormatter(answer) !== 'yes') {
console.log('Game over'.blue);
rl.close();
} else {
roundCount += 1;
console.log(`Round ${roundCount}`.green);
game();
}
});
const startNewRound = () => {
rl.question('Another round? (enter yes to start) '.cyan, async (answer) => {
if (inputFormatter(answer) !== 'yes') {
console.log('Game over. Thanks for playing!'.blue);
await showResults();
removeLogFile();
rl.close();
} else {
setCount = 0;
casinoWinsCount = 0;
playerWinsCount = 0;
roundCount += 1;
console.log(`Round ${roundCount}`.green);
clearLogFile();
game();
}
});
};
const game = () => {
rl.question(
'Are you ready to rumble? (enter yes to start) '.cyan,
async (answer) => {
if (inputFormatter(answer) !== 'yes') {
console.log(`Round ${roundCount} over`.blue);
writeRoundStatistics(setCount, playerWinsCount, casinoWinsCount);
await showResults();
clearLogFile();
startNewRound();
return;
}
setCount += 1;
const coinTossValue = coinToss();
rl.question('Enter heads or tails: '.yellow, async (value) => {
if (inputFormatter(value) === coinTossValue) {
console.log(
`Congratulations, you won - it was ${coinTossValue}`.green
);
playerWinsCount += 1;
makeLogFile(
`Set ${setCount}. Coin toss: ${coinTossValue}, your answer: ${value}. You won!`
);
} else {
console.log(`You guessed wrong - it was ${coinTossValue}`.red);
casinoWinsCount += 1;
makeLogFile(
`Set ${setCount}. Coin toss: ${coinTossValue}, your answer: ${value}. Casino won!`
);
}
if (setCount === 5) {
writeRoundStatistics(setCount, playerWinsCount, casinoWinsCount);
console.log(`Round ${roundCount} over`.blue);
await showResults();
clearLogFile();
startNewRound();
} else {
game();
}
});
}
);
};