forked from BurlingtonCodeAcademy/guess-the-number-SenouLynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (230 loc) · 6.82 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//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);
});
}
gamePick();
//Variable Declarations
let maxRange;
let minRange;
let numGuess;
let compPick = "";
//Game Start
//Start will have you pick the number
//PlayerStart will have the computer pick the number
async function gamePick() {
let startTree = await ask ("Let's play a game where either you or I will pick a number and the opposite person will try and guess it. If you would like to guess the number I pick, type 'Me'. If you would like me to guess your number, type 'You'.\n")
if (startTree === "Me") {
start()
} else if (startTree === "You") {
playerStart()
} else {
console.log("Please type either 'Me' or 'You\n")
gamePick()
}
}
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, the range will be 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;
}
//switch pickNum to smartNum to toggle number picker function below //
//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"
) {
guessWrongLoop();
} else if (
compPick === "y" ||
compPick === "Yes" ||
compPick === "YES" ||
compPick === "yes"
) {
console.log("I got it you little shit.");
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;
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() {
let startAgain = await ask("Would you like to play again?\n");
if (
startAgain === "y" ||
startAgain === "Yes" ||
startAgain === "YES" ||
startAgain === "yes"
) {
gamePick();
} else if (
startAgain === "n" ||
startAgain === "No" ||
startAgain === "NO" ||
startAgain === "no"
) {
console.log("Okay byeeee!");
process.exit();
}
}
//Work in Progress - not quite working how I want it too//
//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();
}
}
//2nd part of the game - You try and guess the computer's number
let playerMinRange;
let playerMaxRange;
let compNum;
let compNumInt;
async function playerStart() {
console.log(
"I'm going to guess a number. Let's figure out how hard you want to make it"
);
playerMaxRange = await ask("What's the highest number I can set?\n");
playerMinRange = await ask("What's the lowest number I can set?\n");
compNum = pickNum(playerMinRange, playerMaxRange);
compNumInt = Number(compNum);
console.log("Computer picks " + compNumInt);
playerPick();
}
//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;
}
//Guessing Block
async function playerPick() {
while (true) {
let playerGuess = await ask("Guess a number.\n");
let playerNum = Number(playerGuess);
if (playerNum > compNumInt) {
console.log("Your guess is too high.");
} else if (playerGuess < compNumInt) {
console.log("Your guess is too low.");
} else if (playerGuess == compNumInt) {
console.log("You guessed right! Winner winner chicken dinner BOI!");
guessRightLoop();
}
}
}
//FIGURE OUT HOW TO INCORPORATE RANGE CHECK
//NOT FUNCTIONAL BELOW THIS LINE
async function playerRangeCheck() {
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, the range will be between ` +
minRange +
` and ` +
maxRange +
`. Let us begin.`
);
playerPick();
}
}