-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePage.html
More file actions
337 lines (291 loc) · 11.5 KB
/
GamePage.html
File metadata and controls
337 lines (291 loc) · 11.5 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="https://getbootstrap.com/favicon.ico">
<title>Simple Math Game</title>
<!-- Bootstrap core CSS -->
<link href="Starter%20Template%20for%20Bootstrap_files/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="Starter%20Template%20for%20Bootstrap_files/starter-template.css" rel="stylesheet">
<!-- <link href="css/simple-sidebar.css" rel="stylesheet"> -->
</head>
<script>
var max = 10; //Maximum number that can be generated in a problem (starting value)
var min = 0; //Minimum number that can be generated in a problem (starting value)
var score = 0; //User-score. Increments by one for each problem answered correctly.
var lives = 3; //User-lives. Decrements by one for each problem answered incorrectly.
var level = 1; //User-level. Increments by one everytime the score increments by five.
var numAddends = 20;
var JSONarray = {"numbers":[]};
var num1JSONlocation;
var num2JSONlocation;
generateNumbers();
chooseProblems();
function getRandomInt(max, min)
{
return Math.floor((Math.random() * max) + min);
}
function generateNumbers()
{
JSONarray.numbers = [];
for (index = 0; index < numAddends; index++)
{
JSONarray.numbers.push(getRandomInt(max, min).toString());
}
}
function chooseProblems()
{
num1JSONlocation = getRandomInt(JSONarray.numbers.length, 0);
num2JSONlocation = getRandomInt(JSONarray.numbers.length, 0);
//num1 & num2 JSON location must be odd.
if ( (num1JSONlocation % 2) == 0)
{
num1JSONlocation++;
}
if ( (num2JSONlocation % 2) == 0)
{
num2JSONlocation++;
}
}
</script>
<body>
<!-- Menu bar ontop of the page -->
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="HomePage.html">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="highscores-button">High Scores</a>
</li>
</ul>
</div>
</nav>
<!-- Highscores sidebar -->
<div id="wrapper">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<p id=highscoresToHTML></p>
</ul>
</div>
</div>
<main role="main" class="container">
<!-- User interface -->
<div class="starter-template">
<h1>Evilscaught Presents: The Math Game</h1>
<a id="lives">Lives: <br></a>
<a id="score">Score: <br></a>
<a id="level">Level: <br></a>
<a id="problem"></a>
<div class="answer-container" id="answer-container">
<label for="answerField">
<p>The answer is: <span class="tooltiptext">Press Enter to Submit Answer</span> </p> <input type="text" id="answerField"/>
</label>
</div>
<h1 id="message"><br></h1>
<a id="tryAgain" style="visibility: hidden; color: white; text-decoration: underline;" href="#" onclick="resetGame();">Try Again?<br></a>
</div>
<div class="username-prompt" id="usernamePrompt">
<h3>Enter your username to record your current score<br>(only a new highscore will result in update)</h3>
<div class="username-container" id="username-container">
<label for="usernameField">
<p>My username is: </p> <input type="text" id="usernameField"/>
</label>
</div>
</div>
</main><!-- /.container -->
<!-- Allows the menu-bar ontop of page to adjust to page size -->
<nav class="nav navbar-expand-md navbar-dark bg-dark fixed-left"> </nav>
<!-- Required libraries for highscores side-bar to render and animate -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script>
//e is an abbreviation for "element", i.e. eMessage is short for elementMessage.
//This syntax is used to indicate that HTML elements are being called.
var eProblem = document.getElementById("problem");
var eAnswerField = document.getElementById("answerField");
var eMessage = document.getElementById("message");
var eTryAgain = document.getElementById("tryAgain");
var eLives = document.getElementById("lives");
var eScore = document.getElementById("score");
var eLevel = document.getElementById("level");
var eUsernamePrompt = document.getElementById("usernamePrompt");
var eUsernameField = document.getElementById("usernameField");
var usernamesJSON = null;
var highscoresJSON = null;
eLives.innerHTML = "Lives: " + lives + "<br>";
eScore.innerHTML = "Score: " + score + "<br>";
eLevel.innerHTML = "Level: " + level + "<br>";
eProblem.innerHTML = "<br>What is " + JSONarray.numbers[num1JSONlocation] + " + " + JSONarray.numbers[num2JSONlocation] + "?<br>";
document.getElementById("highscoresToHTML").innerHTML = "<li><a>No Highscores Yet.</a></li>";
//This function allows user to press "enter" to submit the answer to thier problem.
eAnswerField.onkeydown = function(event)
{
if (event.keyCode == 13)
{
checkAnswer();
}
}
//This function allows user to press "enter" to submit their username and record their highscore.
eUsernameField.onkeydown = function(event)
{
if (event.keyCode == 13)
{
logHighScore();
}
}
function resetGame()
{
lives = 3;
score = 0;
level = 0;
max = 10;
min = 0;
eLives.innerHTML = "Lives: " + lives + "<br>";
eScore.innerHTML = "Score: " + score + "<br>";
eLevel.innerHTML = "Level: " + level + "<br>";
eMessage.innerHTML = "";
document.getElementById("answer-container").style.visibility = "visible";
eProblem.style.visibility = "visible";
eTryAgain.style.visibility = "hidden";
generateNumbers();
generateNewProblem();
}
//This function will toggle the highscores sidebar
$("#highscores-button").click(function(e)
{
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
//This function will make the answer-container shake if the user submits incorrect input.
jQuery.fn.shake = function()
{
this.each(function(i)
{
$("#answer-container").css({ "position" : "relative" });
for (var x = 1; x <= 3; x++)
{
$(this).animate({ left: -5 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
}
});
return this;
}
function checkAnswer()
{
var correctAnswer = parseInt(JSONarray.numbers[num1JSONlocation]) + parseInt(JSONarray.numbers[num2JSONlocation]);
//If the user-entered answer is equal to the correct answer:
if (answerField.value == correctAnswer)
{
score++;
//Reset answer field and any messages on display
eAnswerField.value = "";
eMessage.innerHTML = "";
eScore.innerHTML = "Score: " + score + "<br>";
//If the score has increment by five, level up!
if (score % 5 == 0)
{
level++;
max+=10;
min+=10;
generateNumbers();
eMessage.innerHTML = "Level Up!";
eLevel.innerHTML = "Level: " + level + "<br>";
}
generateNewProblem();
}
//If the user did not enter anything:
else if (answerField.value == "")
{
eMessage.innerHTML = "Cannot be Empty!";
$("#answer-container").shake();
}
//If the user-entered answer is not equal to the correct answer:
else
{
$("#answer-container").shake();
lives--;
if (lives < 0)
{
document.getElementById("answer-container").style.visibility = "hidden";
eMessage.innerHTML = "GAME OVER";
eTryAgain.style.visibility = "visible";
eProblem.style.visibility = "hidden";
eUsernameField.style.visibility = "visible";
}
else
{
eMessage.innerHTML = "Try Again";
eLives.innerHTML = "Lives: " + lives + "<br>";
}
}
}
function generateNewProblem()
{
chooseProblems();
document.getElementById("problem").innerHTML = "<br>What is " + JSONarray.numbers[num1JSONlocation] + " + " + JSONarray.numbers[num2JSONlocation] + "?<br>";
}
//'highscores' and 'usernames' are parallel arrays, they both have the same length
function logHighScore()
{
var usernames = JSON.parse(usernamesJSON);
var highscores = JSON.parse(highscoresJSON);
//If there are no existing usernames and highscores make a new entry
if ( (usernames == null) && (highscores == null) )
{
usernames = [];
highscores = [];
usernames.push(eUsernameField.value);
highscores.push(score);
}
//If the entered username already exists, replace their highscore only if ...
//... their new highscore is greater than the old one.
else if ( usernames.includes(eUsernameField.value) )
{
for (index = 0; index < highscores.length; index++)
{
if ( (eUsernameField.value == usernames[index]) && (score > highscores[index]) )
{
highscores[index] = score;
}
}
}
//Otherwise, make a new entry for that username.
else
{
//Add new username & highscore respective to its rank in the list of existing highscores
for (index = 0; index < highscores.length; index++)
{
if (score <= highscores[index])
{
highscores.splice(index, 0, score);
usernames.splice(index, 0, eUsernameField.value);
break;
}
}
}
eUsernameField.value = "";
//Update highscores and usernames sidebar
var html = "";
for (index = highscores.length - 1; index >= 0; index--)
{
html += "<li><a>" + usernames[index] + ": " + highscores[index] + "</a></li>";
}
document.getElementById("highscoresToHTML").innerHTML = html;
//Toggle highscores sidebar to visible
$("#wrapper").toggleClass("toggled");
//Convert highscores and usernames back to JSON
usernamesJSON = JSON.stringify(usernames);
highscoresJSON = JSON.stringify(highscores);
}
</script>
</body>
</html>