-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorGameProject.js
More file actions
210 lines (181 loc) · 5.78 KB
/
ColorGameProject.js
File metadata and controls
210 lines (181 loc) · 5.78 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var square=document.querySelectorAll(".square"); //rgb(0, 0, 0) not working!
var correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
var h1=document.querySelector("h1");
var index=randomNumber(0,5);
var header=document.querySelector(".header");
var button=document.querySelector("button");
var paragraph=document.getElementById("correct_incorrect");
var easy=document.querySelector("#easy");
var hard=document.querySelector("#hard");
var selected="none";
for(var i=0;i<square.length;i++)
{
var color='#'+Math.floor(Math.random()*16777215).toString(16);
square[i].style.backgroundColor=color;
}
h1.textContent=hexToRGB(correctColor);
square[index].style.backgroundColor=correctColor;
button.addEventListener("click",function(){
// for(var i=0;i<6;i++)
// { var x = Math.floor(Math.random() * 256);
// var y = Math.floor(Math.random() * 256);
// var z = Math.floor(Math.random() * 256);
// var bgColor = "rgb(" + x + "," + y + "," + z + ")";
// square[i].style.backgroundColor=bgColor;
// h1.textContent=bgColor;
// }
// var correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
// var index=randomNumber(0,5);
// square[index].style.backgroundColor=correctColor;
for(var i=0;i<6;i++)
{
square[i].style.visibility="visible";
var color='#'+Math.floor(Math.random()*16777215).toString(16);
square[i].style.backgroundColor=color;
}
correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
if(selected==="easy"){
index=randomNumber(0,2);
}
if(selected==="hard"||selected==="none"){
index=randomNumber(0,5);
}
h1.textContent=hexToRGB(correctColor);
square[index].style.backgroundColor=correctColor;
})
function hexToRGB(h) {
let r = 0, g = 0, b = 0;
// 3 digits
if (h.length == 4) {
r = "0x" + h[1] + h[1];
g = "0x" + h[2] + h[2];
b = "0x" + h[3] + h[3];
// 6 digits
} else if (h.length == 7) {
r = "0x" + h[1] + h[2];
g = "0x" + h[3] + h[4];
b = "0x" + h[5] + h[6];
}
return "rgb("+ +r + "," + +g + "," + +b + ")";
}
function randomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
for(var i=0;i<6;i++){
square[i].addEventListener("click",check);
}
function check(){
var selectedsquarecolor=this.style.backgroundColor;
var selectedsquarecolorhex=RGBToHex(selectedsquarecolor);
if(selectedsquarecolorhex===correctColor){
if(selected==="easy"){
for(var i=0;i<3;i++){
square[i].style.visibility="visible";
}
for(var i=0;i<3;i++){
square[i].style.backgroundColor=correctColor;
}
header.style.backgroundColor=correctColor;
if(paragraph.textContent!="Correct!"){
paragraph.textContent ="Correct!";
}
document.getElementById("reset").innerHTML="Play Again?";
button.addEventListener("click",function(){
header.style.backgroundColor="#20B2AA";
document.getElementById("reset").innerHTML="New Colors";
paragraph.textContent="";
})
}
if(selected==="hard"||selected=="none"){
for(var i=0;i<6;i++){
square[i].style.visibility="visible";
}
for(var i=0;i<6;i++){
square[i].style.backgroundColor=correctColor;
}
header.style.backgroundColor=correctColor;
if(paragraph.textContent!="Correct!"){
paragraph.textContent ="Correct!";
}
document.getElementById("reset").innerHTML="Play Again?";
button.addEventListener("click",function(){
header.style.backgroundColor="#20B2AA";
selected="none";
document.getElementById("reset").innerHTML="New Colors";
paragraph.textContent="";
for(var i=0;i<6;i++)
{
square[i].style.visibility="visible";
var color='#'+Math.floor(Math.random()*16777215).toString(16);
square[i].style.backgroundColor=color;
}
correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
if(selected==="easy"){
index=randomNumber(0,2);
}
if(selected==="hard"||selected==="none"){
index=randomNumber(0,5);
}
h1.textContent=hexToRGB(correctColor);
square[index].style.backgroundColor=correctColor;
})
}
}
else{
if(paragraph.textContent!="Try Again!")
paragraph.textContent += "Try Again!";
this.style.visibility="hidden";
}
}
function RGBToHex(rgb) {
// Choose correct separator
let sep = rgb.indexOf(",") > -1 ? "," : " ";
// Turn "rgb(r,g,b)" into [r,g,b]
rgb = rgb.substr(4).split(")")[0].split(sep);
let r = (+rgb[0]).toString(16),
g = (+rgb[1]).toString(16),
b = (+rgb[2]).toString(16);
if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
return "#" + r + g + b;
}
hard.addEventListener("click",function(){
selected="hard";
for(var i=0;i<square.length;i++){
square[i].style.visibility="visible";
}
for(var i=0;i<square.length;i++)
{
var color='#'+Math.floor(Math.random()*16777215).toString(16);
square[i].style.backgroundColor=color;
}
correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
index=randomNumber(0,5);
h1.textContent=hexToRGB(correctColor);
square[index].style.backgroundColor=correctColor;
header.style.backgroundColor="#20B2AA";
paragraph.textContent="";
})
easy.addEventListener("click",function(){
selected="easy";
for(var i=3;i<6;i++){
square[i].style.visibility="hidden";
}
for(var i=0;i<3;i++)
{
var color='#'+Math.floor(Math.random()*16777215).toString(16);
square[i].style.backgroundColor=color;
}
correctColor='#'+Math.floor(Math.random()*16777215).toString(16);
index=randomNumber(0,2);
h1.textContent=hexToRGB(correctColor);
square[index].style.backgroundColor=correctColor;
header.style.backgroundColor="#20B2AA";
paragraph.textContent="";
})