-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
117 lines (104 loc) · 2.65 KB
/
scripts.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
"use strict";
let colors = [],
pickedColor,
numSquares = 6,
gameMode = "Normal",
h1 = document.querySelector("h1"),
colorDisplay = document.querySelector("#colorDisplay"),
messageDisplay = document.querySelector("#message"),
resetBtn = document.querySelector("#resetBtn"),
mode = document.querySelectorAll(".mode"),
squares = document.querySelectorAll(".square");
init();
function init(){
setupMode();
setupGameMode();
reset();
resetBtn.addEventListener("click", function(){
reset();
});
gameStart();
}
function setupMode() {
mode.forEach(function (btn) {
btn.addEventListener("click", function() {
mode[0].classList.remove("selected");
mode[1].classList.remove("selected");
mode[2].classList.remove("selected");
this.classList.add("selected");
gameMode = this.textContent;
setupGameMode();
reset();
});
});
}
function setupGameMode(){
if(gameMode === "Hard") {
return numSquares = 9;
} else if(gameMode === "Normal") {
return numSquares = 6;
} else {
return numSquares = 3;
}
}
function reset() {
squares.forEach(function(square) {
square.removeEventListener("click", reset);
});
selectColors();
pickColor();
h1.style.backgroundColor = "var(--bright)";
messageDisplay.textContent = "";
resetBtn.textContent = "New Colors";
}
function selectColors(color) {
colors = [];
squares.forEach(function(square, i) {
if(i < numSquares) {
let color = getRandomColor();
colors.push(color);
square.style.display = "block";
square.style.backgroundColor = color;
square.style.boxShadow = "var(--shadow)";
} else {
square.style.display = "none";
}
});
return colors;
}
function getRandomColor(color){
let r = Math.floor(Math.random() * 256),
g = Math.floor(Math.random() * 256),
b = Math.floor(Math.random() * 256);
color = "rgb(" + r + ", " + g + ", " + b + ")";
return color;
}
function pickColor(){
pickedColor = colors[Math.floor(Math.random() * numSquares)];
colorDisplay.textContent = pickedColor;
return pickedColor;
}
function gameStart(){
squares.forEach(function (square) {
square.addEventListener("click", function(){
let clickedColor = this.style.backgroundColor;
if(clickedColor === pickedColor) {
gameOver();
} else {
messageDisplay.textContent = "Try Again";
this.style.backgroundColor = "var(--dark)";
this.style.boxShadow = "none";
}
});
});
}
function gameOver(){
messageDisplay.textContent = "You won!";
h1.style.backgroundColor = pickedColor;
squares.forEach(function(square) {
square.style.backgroundColor = pickedColor;
square.style.boxShadow = "var(--shadow)";
square.addEventListener("click", reset);
});
resetBtn.textContent = "Play again?";
}