Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (117 loc) · 3.71 KB
/
Copy pathscript.js
File metadata and controls
143 lines (117 loc) · 3.71 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
const guess = document.getElementById('guess');
const correctness = document.getElementById('correctness');
const scoreCounter = document.getElementById('scoreCounter');
const timer = document.getElementById('timer');
const restartMenu = document.getElementById('restartMenu');
const endScore = document.getElementById('endScore');
var selectedMode = 'timed';
var score = 0;
var totalGames = 0;
var gameActive = false;
var chosenBranchList;
var branches = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'];
var oldRandomBranch = 0;
var randomBranch;
function branchClicked(branch) {
if (gameActive === false) {
return;
}
if (selectedMode === 'goal') {
if (branch === chosenBranchList[totalGames]) {
correctness.innerHTML = 'Correct!';
score++;
} else {
correctness.innerHTML = 'Incorrect!';
}
totalGames++
scoreCounter.innerHTML = score + ' / ' + totalGames + '<br>' + Math.floor(score / totalGames * 100) + '%';
guess.innerHTML = 'Find branch: ' + chosenBranchList[totalGames];
if (totalGames === 12) {
gameEnded();
randomBranch = Math.floor(Math.random() * 12);
guess.innerHTML = "Find branch: " + branches[randomBranch];
}
} else if (selectedMode === 'timed') {
totalGames++;
if (branch === branches[randomBranch]) {
//console.log('Correct!');
correctness.innerHTML = 'Correct!';
score++;
} else {
correctness.innerHTML = 'Incorrect!';
}
scoreCounter.innerHTML = score + ' / ' + totalGames + '<br>' + Math.floor(score / totalGames * 100) + '%';
randomBranch = Math.floor(Math.random() * 12);
oldRandomBranch = randomBranch;
while (randomBranch === oldRandomBranch) {
randomBranch = Math.floor(Math.random() * 12);
guess.innerHTML = 'Find branch: ' + branches[randomBranch];
}
}
}
function modeSelected() {
selectedMode = document.getElementById('modeSelect').value;
}
function restart() {
score = 0;
totalGames = 0;
gameActive = true;
randomBranch = Math.floor(Math.random() * 12);
guess.innerHTML = "Find branch: " + branches[randomBranch];
if (selectedMode === 'timed') {
startTime(90000);
} else if (selectedMode === 'goal') {
startCounter();
}
restartMenu.style.display = 'none';
scoreCounter.style.display = 'block';
}
function startTime(milliSeconds) {
var timeLeft = milliSeconds / 1000;
timer.innerHTML = Math.floor(timeLeft/60)+':'+timeLeft % 60;
var updateTimer = setInterval(function() {
timeLeft -= 1;
timer.innerHTML = Math.floor(timeLeft/60)+':'+timeLeft % 60;
if (timeLeft === 0) {
clearInterval(updateTimer);
gameEnded();
}
}, 1000);
}
function startCounter() {
chosenBranchList = [];
timer.innerHTML = formatTime(0);
for (var count = 0; count < 12; count++) {
randomBranch = Math.floor(Math.random() * 12);
while (chosenBranchList.includes(branches[randomBranch])) {
randomBranch = Math.floor(Math.random() * 12);
}
chosenBranchList.push(branches[randomBranch]);
}
console.log(chosenBranchList);
var elapsedTime = 0;
var updateStopwatch = setInterval(function() {
elapsedTime += 10;
timer.innerHTML = formatTime(elapsedTime);
if (gameActive === false) {
clearInterval(updateStopwatch);
}
}, 10);
guess.innerHTML = 'Find branch: ' + chosenBranchList[totalGames];
}
function gameEnded() {
gameActive = false;
restartMenu.style.display = 'block';
scoreCounter.style.display = 'none';
endScore.innerHTML = score + ' / ' + totalGames + '<br>' + Math.floor(score / totalGames * 100) + '%';
}
function formatTime(ms) {
let minutes = Math.floor((ms % 3600000) / 60000);
let seconds = Math.floor((ms % 60000) / 1000);
let milliSeconds = ms % 1000;
return (
String(minutes).padStart(1, '0') + ':' +
String(seconds).padStart(2, '0') + ':' +
String(milliSeconds).padStart(3, '0')
);
}