-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (140 loc) · 5 KB
/
script.js
File metadata and controls
169 lines (140 loc) · 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
const clueHoldTime = 1000; // how long to hold each clue's light/sound
const cluePauseTime = 100; // interval to pause in-between clues
const nextClueWaitTime = 1550; // how long to wait before starting playback of the clue sequence
// Num waves or levels for the difficulties
const Easy = 5;
const Medium = 8;
const Hard = 12;
const Very_Hard = 16;
const Extreme = 20;
const European_Extreme = 25;
var progress = 0; // how far we have progressed into the clue sequence; the 'level'
var guessCounter = 0;
var tempClueholdTime = 1; // temp variable for ClueHoldTime modification, default value 1
var strikeCounter = 0;
var strikeLimit = 3; // max number of strikes allowed
var speedModifier = 1.05; // percent to modify speed after each-turn for PLAYBACK
var speedModifierClue = 0.95;
var currentSpeed = 1;
var gamePlaying = false;
var difficultySelected = false;
var currDifficulty = ""; // HTML sets this to a string when selected
var winText = "Nice job. You won!";
var loseText = "You are out of strikes! Game Over. You lose...";
var pattern = [];
function startGame(){
if (!difficultySelected){
alert("Please select a difficulty first!");
return;
}
progress = 0;
gamePlaying = true;
generatePattern(currDifficulty);
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("stopBtn").classList.remove("hidden");
playClueSequence();
}
function stopGame(){
gamePlaying = false;
pattern = [];
strikeCounter = 0;
tempClueholdTime = 1;
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("stopBtn").classList.add("hidden");
}
function loseGame(){
stopGame();
loseGameAudio();
setTimeout(alert, 500, loseText); // give sound time to play before alerting
}
function winGame(){
stopGame();
winGameAudio();
setTimeout(alert, 1000, winText);
}
function lightButton(btn){
document.getElementById("button"+btn).classList.add("lit");
}
function clearButton(btn){
document.getElementById("button"+btn).classList.remove("lit");
}
function playSingleClue(btn){
if(gamePlaying == true){
tempClueholdTime = clueHoldTime*(speedModifierClue)**progress + 225; // change speed in-between clues
lightButton(btn);
playAudio(document.getElementById("button"+btn).value);
setTimeout(clearButton, tempClueholdTime, btn);
}
}
function playClueSequence(){
guessCounter = 0;
let delay = nextClueWaitTime; // set delay to initial wait time
for( let i = 0; i <= progress; i++){ // for each clue that is revealed so far
console.log("play single clue: " + pattern[i] + " in " + delay + "ms");
setTimeout(playSingleClue, delay, pattern[i]); // timeout to play clue
delay += clueHoldTime * (speedModifierClue)**progress + 200; // 200 millisecond flat value ALWAYS
delay += cluePauseTime;
}
}
function playAudio(soundFile){
if(document.getElementById(soundFile).paused == false){
document.getElementById(soundFile).load();
document.getElementById(soundFile).playbackRate = currentSpeed;
document.getElementById(soundFile).play();
}
else{
document.getElementById(soundFile).playbackRate = 1 * (speedModifier)**progress; // modify audio speeds depending on speed modifier and progress
currentSpeed = 1 * speedModifier**progress;
document.getElementById(soundFile).play();
}
}
function guess(btn){
console.log("user guessed: " + btn);
if(!gamePlaying){
return;
}
if (pattern[guessCounter] == btn){ // Correct Guess: continue playing
if (guessCounter == progress){ // guess was correct, check for win condition
if (progress == pattern.length - 1){ // reaching the end == win
winGame();
}
else{
progress++;
playClueSequence();
}
}else{
guessCounter +=1; // Update guessCounter to move onto next clue
}
}else{
strikeCounter += 1; //user incorrectly guessed, check for loss condition
if (strikeCounter == strikeLimit){
loseGame();
stopGame();
return;
}
alert("Strike " + strikeCounter + "! " + "You have " + (strikeLimit-strikeCounter) + " strikes left!"); // user has strikeLimit guesses
playClueSequence();
}
}
function generatePattern(numPatterns){ // generates random pattern from 0-6 and shoves it inside pattern
for( let i = 0; i < numPatterns; i++){
pattern.push(Math.floor((Math.random() * 6) + 1));
}
console.log(pattern); // a little bonus for the user
}
function alertDifficulty(difficulty){
document.getElementById("current_difficulty").innerHTML = "Current Difficulty: " + difficulty; // handles the HTML current difficulty
alert("You have selected the following difficulty: " + difficulty);
}
function setDifficulty(difficulty){
currDifficulty = difficulty;
difficultySelected = true;
}
function winGameAudio(){
document.getElementById("winGameSound").load();
document.getElementById("winGameSound").play();
}
function loseGameAudio(){
document.getElementById("loseGameSound").load();
document.getElementById("loseGameSound").play();
}