-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessingGame.js
executable file
·171 lines (149 loc) · 4.47 KB
/
guessingGame.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
$(document).ready(function() {
$('body').fadeIn(750);
// Start up with some important variables!
var playersGuess,
guesses = [],
winningNumber = generateRandomNumber(1, 100);
/* **** Guessing Game Functions **** */
// Generate random number in a range (will be used for winning number and providHint)
function generateRandomNumber(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Fetch the Player's Guess
function playersGuessSubmission(){
playersGuess = +document.getElementById('guess').value;
document.getElementById('guess').value = "";
}
// Determine if the next guess should be a lower or higher number
function lowerOrHigher(){
if (playersGuess > winningNumber) {
return "Guess lower!";
} else if (playersGuess < winningNumber) {
return "Guess higher!";
} else {
return "You Won!";
}
}
// Check if guess has been made before
function matchesPrevGuess() {
for (var i = 0; i < guesses.length; i++) {
if (playersGuess === guesses[i]) {return true;}
}
return false;
}
// Change message (placeholder) in input field as feedback to player
function guessMessage(message) {
$("#guess").attr("placeholder", message);
}
// Update appearance (state) of game when a guess is made
function stateUpdate(event) {
// For showing winning number when a game ends
function showWinningNumber() {
$("#status").fadeOut(500);
setTimeout(function() {
$("#status").text("The answer was " + winningNumber);
}, 500);
$("#status").fadeIn(500);
};
var curClass = $("#circle").attr("class");
if (event === "won") {
guessMessage("You won!");
showWinningNumber();
$("#circle").switchClass(curClass, "won", 500);
$("#submit-guess, #get-hint, #guess").prop("disabled", true);
} else if (event === "lost") {
guessMessage("You lost!");
showWinningNumber();
$("#circle").switchClass(curClass, "lost", 500);
$("#submit-guess, #get-hint, #guess").prop("disabled", true);
} else {
var newClass = temperature().slice(0, temperature().length-2);
if(newClass !== curClass) {$("#circle").switchClass(curClass, newClass);}
}
}
// Check if the Player's Guess is the winning number
function checkGuess(){
if (isNaN(+playersGuess)) {
guessMessage("numbers only!")
} else if (playersGuess < 1 || playersGuess > 100) {
guessMessage("range is 1-100!")
} else if (playersGuess === winningNumber) {
stateUpdate("won");
} else if (matchesPrevGuess()) {
guessMessage("already guessed that!");
setTimeout(function(){
guessMessage(temperature() + lowerOrHigher());
},750);
stateUpdate();
} else {
guesses.push(playersGuess);
$('#prev-guesses').text(guesses.join(", "))
$("#attempts").text(5 - guesses.length);
if (+$("#attempts").text() <= 0) {
stateUpdate("lost");
} else {
guessMessage(temperature() + lowerOrHigher());
stateUpdate();
}
}
}
// Determines what message to give the player based on how far away
function temperature() {
if (Math.abs(winningNumber-playersGuess) <= 5) {
return "HOT! ";
} else if (Math.abs(winningNumber-playersGuess) <= 10) {
return "WARM; ";
} else if (Math.abs(winningNumber-playersGuess) <= 20) {
return "COOL; ";
} else {
return "COLD; ";
}
}
// Opens a hidden element and provides hints, previous guesses, and key for temperatures
function provideHint() {
var hints=[];
var numberOfHints = 10 - (guesses.length * 2);
for (var i = 0; i < numberOfHints; i++) {
var hintNumber = generateRandomNumber(1, 100);
while (hints.indexOf(hintNumber) !== -1) {
hintNumber = generateRandomNumber(1, 100)
};
hints[i] = hintNumber;
};
if (hints.indexOf(winningNumber) === -1) {
var winnerPosition = generateRandomNumber(1,numberOfHints);
hints[winnerPosition] = winningNumber;
}
$("#hints").text(hints.join(", "));
$("#hint-popup").slideToggle();
}
// Restart the game
function restart() {
$('body').fadeOut(750);
setTimeout(function() {
location.reload();
}, 800);
}
/* **** Event Listeners/Handlers **** */
$('#submit-guess').click(function() {
playersGuessSubmission();
checkGuess();
});
$('#guess').keypress(function(key) {
if (key.which === 13) {
playersGuessSubmission();
checkGuess();
}
})
$('#get-hint').click(function() {
$("#screen").addClass('screen');
provideHint();
});
$('#close-hints').click(function() {
$("#screen").removeClass('screen');
$("#hint-popup").slideToggle();
});
$('#restart').click(function() {
restart();
});
});