-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
168 lines (148 loc) · 4.9 KB
/
code.js
File metadata and controls
168 lines (148 loc) · 4.9 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
let gameOver = true;
let squares = document.querySelectorAll(".square");
let visibleSquares;
const header = document.querySelector("header");
const rgbStringInHeader = document.querySelector(".rgbString");
const info = document.querySelector(".info");
const hardBtn = document.querySelector(".hard");
const easyBtn = document.querySelector(".easy");
const newColorsBtn = document.querySelector(".newColors");
hardBtn.addEventListener("click", function () {
//HARD MODE
this.classList.add("selected");
easyBtn.classList.remove("selected");
startOverHard();
});
easyBtn.addEventListener("click", function () {
//EASY MODE
this.classList.add("selected");
hardBtn.classList.remove("selected");
startOverEasy();
});
newColorsBtn.addEventListener("click", function () {
gameOver = false;
if (isHard) {
startOverHard();
hardBtn.classList.add("selected");
easyBtn.classList.remove("selected");
} else {
startOverEasy();
easyBtn.classList.add("selected");
hardBtn.classList.remove("selected");
}
for (let i = 0; i < visibleSquares.length; i++) {
visibleSquares[i].classList.remove("clicked");
visibleSquares[i].classList.remove("answer");
}
});
//GLOBAL FUNCTIONS
function addEventListenerToVisibleSquares() {
//THIS. IS. A. MESS.
for (let i = 0; i < visibleSquares.length; i++) {
visibleSquares[i].addEventListener("click", function () {
if (!gameOver && this.classList.contains("square-visible")) {
this.classList.add("clicked");
if (
this.style.backgroundColor.toUpperCase() ===
rgbStringInHeader.textContent
) {
this.classList.add("answer");
gameOver = true;
for (let j = 0; j < visibleSquares.length; j++) {
visibleSquares[
j
].style.backgroundColor = this.style.backgroundColor;
visibleSquares[j].classList.add("square-visible");
}
info.style.opacity = "1";
info.textContent = "Correct!";
header.style.backgroundColor = this.style.backgroundColor;
newColorsBtn.textContent = "PLAY AGAIN?";
} else if (
this.style.backgroundColor.toUpperCase() !==
rgbStringInHeader.textContent &&
this.classList.contains("square-visible")
) {
this.classList.remove("square-visible");
info.style.opacity = "1";
info.textContent = "Try Again";
}
}
});
}
}
function startOverHard() {
isHard = true;
for (let i = 0; i < squares.length; i++) {
squares[i].classList.add("square-visible");
}
startOver();
for (let i = 0; i < visibleSquares.length; i++) {
visibleSquares[i].classList.remove("clicked");
visibleSquares[i].classList.remove("answer");
}
addEventListenerToVisibleSquares();
}
function startOverEasy() {
isHard = false;
for (let i = 0; i < squares.length; i++) {
squares[i].classList.add("square-visible");
}
for (let j = 3; j < squares.length; j++) {
squares[j].classList.remove("square-visible");
squares[j].style = "";
}
startOver();
for (let i = 0; i < visibleSquares.length; i++) {
visibleSquares[i].style.backgroundColor = colors[i];
visibleSquares[i].classList.remove("clicked");
visibleSquares[i].classList.remove("answer");
}
addEventListenerToVisibleSquares();
}
function startOver() {
gameOver = false;
newColorsBtn.textContent = "NEW COLORS";
visibleSquares = document.querySelectorAll(".square.square-visible");
renewColors();
assignColors();
rgbStringInHeader.textContent = pickARandomSquare().toUpperCase();
info.style.opacity = "0";
header.style.backgroundColor = "var(--my-blue)";
}
function renewColors() {
if (isHard) {
colors = [
randomRgbString(),
randomRgbString(),
randomRgbString(),
randomRgbString(),
randomRgbString(),
randomRgbString(),
];
} else {
colors = [randomRgbString(), randomRgbString(), randomRgbString()];
}
}
function assignColors() {
for (let i = 0; i < visibleSquares.length; i++) {
visibleSquares[i].style = "";
visibleSquares[i].style.backgroundColor = colors[i];
}
}
function randomIntFromInterval(min, max) {
//USEFUL FUNCTION TO GENERATE RANDOM NUMBERS
return Math.floor(Math.random() * (max - min + 1) + min);
}
function pickARandomSquare() {
//PICKS A SQUARE BY RANDOMLY GETTING ITS INDEX
if (isHard) return colors[randomIntFromInterval(0, 5)];
return colors[randomIntFromInterval(0, 2)];
}
function randomRgbString() {
//CREATES A RANDOM 'RGB(X, Y, Z)' STRING
return `rgb(${randomIntFromInterval(0, 255)}, ${randomIntFromInterval(
0,
255
)}, ${randomIntFromInterval(0, 255)})`;
}