-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
157 lines (140 loc) · 4.27 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
//Current line
var CurrentId = undefined;
var inputValues = [];
const inputPrompts = [];
const logo = `
/ _ \\_ _ ___ ___ ___ /__ \\ |__ ___ /\\ \\ \\_ _ _ __ ___ | |__ ___ _ __
/ /_\\/ | | |/ _ \\/ __/ __| / /\\/ '_ \\ / _ \\ / \\/ / | | | '_ ' _ \\| '_ \\ / _ \\ '__|
/ /_\\\\| |_| | __/\\__ \\__ \\ / / | | | | __/ / /\\ /| |_| | | | | | | |_) | __/ |
\\____/ \\__,_|\\___||___/___/ \\/ |_| |_|\\___| \\_\\ \\/ \\__,_|_| |_| |_|_.__/ \\___|_|
`;
const EASY_LEVEL_TURNS = 10;
const HARD_LEVEL_TURNS = 5;
let answer;
let turns;
let gameOver = false;
//Click Run
$(document).ready(function () {
$("#run-button").click(function () {
inputValues = [];
$("#Content").empty();
restart();
});
});
function restart() {
answer = Math.floor(Math.random() * 100 + 1);
NewLine(logo, false);
NewLine("Welcome to the Number Guessing Game!", false);
NewLine("I'm thinking of a number between 1 and 100.", false);
// NewLine(`Pssst, the correct answer is ${answer}`);
NewLine("Choose a difficulty. Type 'easy' or 'hard': ", true);
}
//Enter button
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
if (x === 13 || x == 13) {
// Don't process input if game is over
if (gameOver) {
return;
}
var consoleLine = $("#" + CurrentId + " input").val();
inputValues.push({ id: CurrentId, val: consoleLine });
if (inputValues.length > 1) {
// Don't process input if game is over
if (gameOver) {
return;
}
const guess = Number(inputValues[inputValues.length - 1].val);
if (guess != answer) {
turns -= 1;
// Check turns BEFORE creating new prompts
if (turns <= 0) {
$(".console-carrot").remove();
NewLine(
"You've run out of guesses. Refresh the page to run again.",
false
);
return;
}
if (guess < answer) {
NewLine("Too low.", false);
} else if (guess > answer) {
NewLine("Too high.", false);
}
if (turns == 0) {
$(".console-carrot").remove();
NewLine("You've run out of guesses, you lose.", false);
gameOver = true;
return;
} else {
NewLine("Guess again.", false);
}
} else {
$(".console-carrot").remove();
NewLine(`You got it! The answer was ${answer}.`, false);
return;
}
} else {
if (inputValues[0].val.toLowerCase().trim() == "easy") {
turns = 10;
} else {
turns = 5;
}
}
NewLine(`You have ${turns} attempts remaining to guess the number.`);
NewLine("Make a guess: ", true);
// $(".console-carrot").remove();
// if (biddingShouldContinue) {
// NewLine(inputPrompts[inputValues.length - 1], true);
// }
}
});
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
var line = $("#" + CurrentId + " input");
var length = line.val().length;
if (x != 8) {
line.attr("size", 1 + length);
} else {
line.attr("size", length * 0.95);
}
if (length === 0) {
$("#" + CurrentId + " input").attr("size", "1");
}
});
$(document).on("click", function (e) {
$("#" + CurrentId + " input").focus();
});
//New line
function NewLine(text, isPrompt) {
$(".console-carrot").remove();
if (CurrentId !== undefined) {
$("#" + CurrentId + " input").prop("disabled", true);
}
CurrentId = "consoleInput-" + GenerateId();
if (isPrompt) {
$("#Content").append(
//One Line
'<div id="' +
CurrentId +
'">' +
text +
'<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" type="text" class="terminal-input" /><div class="console-carrot"></div></div>'
);
$("#" + CurrentId + " input").focus();
$("#" + CurrentId + " input").attr("size", "1");
} else {
$("#Content").append('<div id="' + CurrentId + '">' + text + "</div>");
}
document.getElementById(CurrentId).scrollIntoView();
}
function GenerateId() {
return Math.random().toString(16).slice(2);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}