-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTEST.js
More file actions
183 lines (166 loc) · 4.67 KB
/
TEST.js
File metadata and controls
183 lines (166 loc) · 4.67 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//readline library link
const readline = require("readline");
const rl = readline.createInterface(process.stdin, process.stdout);
function ask(questionText) {
return new Promise((resolve, reject) => {
rl.question(questionText, resolve);
});
}
start();
//Variable Declarations
let maxRange;
let minRange;
let numGuess;
let compPick = "";
//Game Start
async function start() {
console.log(
"Let's play a game where you (Big BAUSE) make up a number and I (BeepBoopMachine) try to guess it. \n"
);
setInitRange();
}
//Set initial range with stops to make sure input is a number
async function setInitRange() {
maxRange = await ask("So what's the highest number I can guess?\n");
while (isNaN(maxRange)) {
maxRange = await ask("Let's try this again. Please enter a number. \n");
}
maxRange = Number(maxRange);
minRange = await ask("And what is the lowest number I can guess?\n");
while (isNaN(minRange)) {
minRange = await ask("Let's try this again. Please enter a number.\n");
}
minRange = Number(minRange);
rangeCheck();
}
//Initial range check - make sure minRange < maxRange//
async function rangeCheck() {
if (minRange > maxRange) {
console.log("It looks like your range makes no sense. Lets try it again\n");
setInitRange();
} else if (minRange === maxRange) {
console.log("Well it seems as if " + minRange + " is your number\n");
guessRightLoop();
} else if (minRange < maxRange) {
console.log(
`Okay cool, I can only guess numbers between ` +
minRange +
` and ` +
maxRange +
`. Let us begin.`
);
compPickLoop();
}
}
//Random number picker within range (inclusive) //
function pickNum(min, max) {
let range = max - min + 1;
let randInt = min + Math.floor(Math.random() * range);
return randInt;
}
//Smart number picker - mid point within range.
function smartNum(min, max) {
let range = max - min + 1;
let midInt = Math.floor(range/2);
return midInt;
}
//Is numGuess your number?//
async function compPickLoop() {
numGuess = pickNum(minRange, maxRange);
//numGuess = smartNum(minRange, maxRange);
compPick = await ask("Is " + numGuess + " your number?\n");
if (
compPick === "n" ||
compPick === "No" ||
compPick === "NO" ||
compPick === "no"
) {
//console.log check block start//
console.log("minRange is " + minRange);
console.log("maxRange is " + maxRange);
//console.log check block end//
guessWrongLoop();
} else if (
compPick === "y" ||
compPick === "Yes" ||
compPick === "YES" ||
compPick === "yes"
) {
guessRightLoop();
} else {
console.log(
"Hey dingus, please write either 'Yes' or 'No'. I'm a computer, make it easy for me.\n"
);
compPickLoop();
}
}
//Wrong guess loop
async function guessWrongLoop() {
let secondPick = await ask("Should I guess higher or lower?\n");
compPick = "";
if (
secondPick === "higher" ||
secondPick === "Higher" ||
secondPick === "h" ||
secondPick === "H"
) {
minRange = numGuess;
//console.log check block start//
console.log("minRange is " + minRange);
console.log("maxRange is " + maxRange);
//console.log check block end//
compPickLoop();
//loopCheckRange();
} else if (
secondPick === "lower" ||
secondPick === "Lower" ||
secondPick === "l" ||
secondPick === "L"
) {
maxRange = numGuess;
compPickLoop();
//loopCheckRange;
} else {
console.log(
"This means nothing to me. Please use either 'higher' or 'lower', 'h', or 'l'.\n"
);
guessWrongLoop();
}
}
//Right Guess Loop - start again or exit//
async function guessRightLoop() {
console.log("I got it you little shit.");
let startAgain = await ask("Would you like to play again?\n");
if (
startAgain === "y" ||
startAgain === "Yes" ||
startAgain === "YES" ||
startAgain === "yes"
) {
start();
} else if (
startAgain === "n" ||
startAgain === "No" ||
startAgain === "NO" ||
startAgain === "no"
) {
console.log("Okay byeeee!");
process.exit();
}
}
//Loop check range//
function loopCheckRange () {
if (minRange < maxRange) {
compPickLoop();
//console.log check block start//
console.log("minRange is " + minRange);
console.log("maxRange is " + maxRange);
//console.log check block end//
} if (minRange === maxRange){
console.log("We've come to the end of the road here. Both your ranges seem to be " + maxRange +". I have no choice but to declare myself winner. Suck it.")
guessRightLoop();
} else if (minRange > maxRange) {
console.log("It looks like your range makes no sense. Lets try it again\n");
guessWrongLoop();
}
}