-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrototype.js
245 lines (220 loc) · 8.3 KB
/
Prototype.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
/*
Prototype Puzzle -
Different organization/program may be changed
to be more efficient or easier to use.
File dependent on Save.js, ensure Save.js is loaded in the page before this file.
*/
//Default global variables
let wordList;
let wordsFound;
let score;
let timerOn;
let gameOver;
let newHighScore;
let duplicateList;
startGame()//Main function
document.getElementById("Textbox").focus(); //Immediately hovers cursor over text
document.addEventListener("keydown",function(event){ //Allows keybinds commands to be utilized
if (event.code === 'Escape'){
homePage();
}
else if (event.code === 'Enter'){
document.getElementById("Textbox").focus();
submitWord();
}
},true)
//Initiates game, can be used to restart game as well.
function startGame(){
let difficultyNum = sessionStorage.getItem("difficulty");//Retrieves difficulty
const levelText = document.getElementById("difficulty");//Gets difficulty text
let difficulty = difficultyEasy; //Load easy puzzle by default
if (difficultyNum == 1){ //Load medium puzzle
difficulty = difficultyMedium;
levelText.innerHTML = "Medium" + "<div><span id=\"Clock\">0:30</span></div>";
}
else if (difficultyNum == 2){ //Load difficult puzzle
difficulty = difficultyHard;
levelText.innerHTML = "Hard" + "<div><span id=\"Clock\">0:30</span></div>";
}
//This prevents repetative puzzles in back-to-back play throughs
let puzzleIndex;
do{
puzzleIndex = Math.floor(Math.random()*difficulty.length);
}
while (puzzleIndex === duplicateList)
duplicateList = puzzleIndex;
const puzzle = difficulty[puzzleIndex]; //Retrieves object from Words.json
wordList = puzzle.WordList;
document.getElementById("letters").innerHTML = ("Letters: "+scrambleWord(puzzle.Name));
//Creating a list for words found
wordsFound = [];
wordList.forEach(function(){
wordsFound.push(new Array());})
//Setting Default variables
score = 0; //Player's puzzle score
timerOn = false; //Prevents timer from being triggered more than once
gameOver = false; //If the game is still going
newHighScore = false; //If the player got a new high score
//Set HTML text to defaults
document.getElementById("Results").innerHTML = "";
document.getElementById("ScoreBoard").innerHTML = "Points: 0";
document.getElementById("Clock").innerHTML = "0:30";
if (sessionStorage.getItem("multiplayer") != "0"){
document.getElementById("multiStatus").innerHTML = "Player " + sessionStorage.getItem("multiplayer");
}
}
//Returns back to home page
function homePage() {
window.location.href = "index.html";
}
//When player clicks submit button, function activates
function submitWord(){
//Gets objects from HTML page
const text = document.getElementById("Textbox").value;
const result = document.getElementById("Results");
//If submitting highscore name
if (newHighScore){
save(text,score);
result.innerHTML = "Congratulations " + text + " on your new high score of "+ score + "!";
newHighScore = false;
}
//If game is over, do nothing
if (gameOver) { return; }
//If no text entered, "Type a word!"
if (text === ""){
result.innerHTML = "Type a word!";
return;
}
//Timer function
clock(30); //MAKE SURE THIS STAYS CONSISTANT
//If word was invalid
if(!findWord(text,wordList)) { result.innerHTML = text.toUpperCase() +" is an invalid word.";}
//If word was already guessed
else if(findWord(text,wordsFound)) { result.innerHTML = text.toUpperCase() +" has already been used"; }
//If word meets all criteria to award points
else{
result.innerHTML = "The word " + text.toUpperCase() + " has been successfully found!";
score += text.length; //Points awarded by word length
document.getElementById("ScoreBoard").innerHTML = 'Points: '+score;
wordsFound[text.length-3].push(text.toLowerCase());
}
document.getElementById("Textbox").value = "";
}
//Checks if the word is valid to distribute points.
//Returns true if userWord is valid, otherwise false.
//Argument "userWord" is player's string input.
//Argument "array" must be a 2d array.
function findWord(userWord, array){
const wordLength = userWord.length;
//Only words with 3 or more characters are valid
if (wordLength<3 || wordLength>array.length+2){
return false;
}
//Search through array for specified word
for(let i=0; i<array[wordLength-3].length; i++){
if (userWord.toLowerCase() === (array[wordLength-3][i])){
return true;
}
}
return false;
}
//Creates a countdown timer
//Parameter "time" is counted in seconds for the countdown.
function clock(time) {
if (timerOn){ return; }
timerOn=true;
let start = new Date().getTime();
const end = start + (time * 1000);
let countDown = setInterval(function(){
const now = end - start; //Time in miliseconds
const clock = document.getElementById("Clock");
let minutes = Math.floor(now/60000);
let seconds = Math.floor(now/1000%60);
start += 1000;
let format = Math.floor(minutes%10) + ":" + Math.floor(seconds/10) + Math.floor(seconds%10);
clock.innerHTML = format;
if (now<0){
clearInterval(countDown);
clock.innerHTML = "Time's up!";
timerOn = false;
endGame();
}
},1000);
}
//Function ends the game.
function endGame(){
document.getElementById("button").setAttribute("disabled",true);
document.getElementById("ScoreBoard").innerHTML = "Final Score: "+score;
gameOver = true;
//Unlocks next puzzle, currently will be upon completion
//Unlocks puzzle indefinately
const difficulty = JSON.parse(sessionStorage.getItem("difficulty"));
if (difficulty <= 1 && score >= 15){
let access = JSON.parse(localStorage.getItem("levelAccess"));
access[difficulty] = true;
localStorage.setItem("levelAccess",JSON.stringify(access));
}
//If user's score is high enough to store AND if they are in singleplayer mode
let highScores = getLeaderBoard();
if (highScores[highScores.length-1]["Score"] < score && sessionStorage.getItem("multiplayer") == 0){
const result = document.getElementById("Results");
//Allows next text entry to be saved as leaderboard username
result.innerHTML = "New High Score!\nEnter a username you would like to save in the textbox above.";
newHighScore = true;
}
//Make the replay button visable/clickable
let restartButton = document.getElementById("restartButton");
restartButton.style.display = "block";
//Changes what the replay button says based on the situation the player(s) are in.
if (sessionStorage.getItem("multiplayer") == 1) { restartButton.innerHTML = "Continue"; }
else { restartButton.innerHTML = "Play Again"; }
multiplayerEnd();
}
//Takes in a String
//Returns a randomly scrambled word with the same letters
function scrambleWord(word){
for(let i=0; i<word.length; i++){
replace = Math.floor(Math.random()*(word.length-1))+1;
//if replace == i, function breaks
if (replace === i){
continue;
}
let max = Math.max(i,replace);
let min = Math.min(i,replace);
word = word.substring(0,min) +
word.charAt(max) +
word.substring(min+1,max) +
word.charAt(min) +
word.substring(max+1,word.length);
}
return word;
}
//MultiplayerEnd is a specific function in the event of ending a game in multiplayer mode.
function multiplayerEnd(){
//if player 1 turn ended, start player 2 turn
if (gameOver && sessionStorage.getItem("multiplayer") == 1){
sessionStorage.setItem("player1Score",score); //Stores player 1's score while player 2 plays.
sessionStorage.setItem("multiplayer",2); //This will allow the following if statement to run after another game.
document.getElementById("Results").innerHTML = "Player 1 scored "+ score +" points!"
+"\nClick continue for Player 2's turn!";
}
//if player 2 turn ended, compare player 1 and player 2 score.
//Announces winner
else if(gameOver && sessionStorage.getItem("multiplayer") == 2){
const p1Score = sessionStorage.getItem("player1Score");
let winStatus = "won"; //Default response
if (p1Score === score){ winStatus = "tied"; } //If player1 and player 2 earn the same score
else if (p1Score < score){ winStatus = "lost"; }//If player1 has a lower score than player 2
document.getElementById("Results").innerHTML = "Player 1 with a score of "+
p1Score + ", " + winStatus+
" against Player 2 with a score of " +
score +"!";
//If player decides to play again.
sessionStorage.setItem("multiplayer",1);
}
}
//Restarts the game
function restartGame(){
startGame();
document.getElementById("restartButton").style.display = "none";
}