-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-script.js
More file actions
142 lines (116 loc) · 3.4 KB
/
game-script.js
File metadata and controls
142 lines (116 loc) · 3.4 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
let sequenceLength = 3;
let computerPicks = [];
let userPicks = [];
let userScore = 0;
let computerScore = 0;
let level = 1;
function showSelected(index) {
let target = index.toString();
let f = document.getElementById(target);
f.style.backgroundColor = "red";
setTimeout(() => setToBlue(f), 1000);
}
function removeClass(myElement, myClass) {
myElement.classList.remove(myClass);
}
function spinIt(target) {
target.classList.add("spinjitsu");
setTimeout(() => removeClass(target, "spinjitsu"), 2000);
}
function updateCorrect() {
let targetScore = document.getElementById("correct");
userScore = userScore + 1;
targetScore.innerHTML = `Correct: ${userScore}`;
spinIt(targetScore);
}
function updateIncorrect() {
alert("incorrect sequence input, click button to try a new sequence");
let targetScore = document.getElementById("incorrect");
computerScore = computerScore + 1;
targetScore.innerHTML = `Incorrect : ${computerScore}`;
spinIt(targetScore);
}
function setToBlue(element) {
element.style.backgroundColor = "blue";
}
// compare user picks to computer picks
function comparePicks() {
let myConditional = null;
for (let reps = 0; reps < computerPicks.length; reps++) {
myConditional = computerPicks[reps] == userPicks[reps];
}
if (myConditional) {
updateCorrect();
} else {
updateIncorrect();
}
computerPicks = [];
userPicks = [];
if (userScore >= 3 && userScore % 3 == 0) {
sequenceLength++;
level++;
updateGameStatus();
console.log(sequenceLength);
}
}
// get user picks for comparison
function getClickCallback(i) {
return function() {
showSelected(i);
userPicks.push(i);
if (userPicks.length == computerPicks.length) {
comparePicks();
}
}
}
//show computer selected sequence
function showSequence(sequenceArray) {
let limit = sequenceArray.length;
for (let rep = 0; rep < limit; rep++) {
setTimeout(() => {
showSelected(sequenceArray[rep]);
}, 1000 * rep);
}
}
// set up user click event
let screengrab = document.getElementsByClassName("gamebox");
let limit = screengrab.length;
for (let x = 0; x < limit; x++) {
screengrab[x].onclick = getClickCallback(x)
};
// initialize restart button
document.getElementById("restart-game-button").addEventListener("click", () => {
userScore = 0;
computerScore = 0;
sequenceLength = 3;
level = 1;
location.reload(true)
});
// update game status
function updateGameStatus(){
document.getElementById("gameStatus").innerHTML = `Game Level:${level} Sequence Length:${sequenceLength}`
}
// generate sequnce for computer picks
function randomUniqueNum(range, outputCount) {
let arr = []
for (let i = 1; i <= range; i++) {
arr.push(i)
}
let result = [];
for (let i = 1; i <= outputCount; i++) {
const random = Math.floor(Math.random() * (range - i));
result.push(arr[random]);
arr[random] = arr[range - i];
}
return result;
}
//main game loop
function gameEngine() {
// get computer selections
computerPicks = randomUniqueNum(8, sequenceLength);
// show computer selections
console.log(computerPicks);
showSequence(computerPicks);
}
//initialize start button
document.getElementById("StartButton").addEventListener("click", gameEngine);