-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorGame.js
122 lines (79 loc) · 2.47 KB
/
ColorGame.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
117
118
119
120
var NumberOfSquares = 6;
var colors = ColorGeneratorMachine(NumberOfSquares);
var pickedColor = PickColor();
var RGB = document.getElementById("title");
RGB.textContent = pickedColor ;
var WelcomeTable = document.querySelector("#WelcomeTable");
var squares = document.querySelectorAll(".square");
var MessageDisplay = document.querySelector("#Message");
var Easybtn = document.querySelector("#EasyBtn");
var HardBtn = document.querySelector("#HardBtn");
var Reset = document.querySelector("#Reset");
Easybtn.addEventListener("click",function(){
HardBtn.classList.remove("selected");
Easybtn.classList.add("selected");
NumberOfSquares=3;
ResetProgram();
});
HardBtn.addEventListener("click",function(){
HardBtn.classList.add("selected");
Easybtn.classList.remove("selected");
NumberOfSquares=6;
ResetProgram();
});
Reset.addEventListener("click",function(){
HardBtn.classList.remove("selected");
EasyBtn.classList.remove("selected");
ResetProgram();
});
for(var i =0; i < squares.length;i++){
squares[i].style.backgroundColor= colors[i];
squares[i].addEventListener("click",function(){
// Take the color of this square and compare to picked Color
var clickedColor = this.style.backgroundColor ;
if(clickedColor === pickedColor){
MessageDisplay.textContent = "Congratulation !!!";
ColorChangeWinning(pickedColor);
}
else{
MessageDisplay.textContent = "Try Again !!!";
this.style.backgroundColor = "#232323"
}
});
}
function ColorChangeWinning(Color) {
for(var i =0; i < colors.length;i++){
squares[i].style.backgroundColor = Color
}
WelcomeTable.style.backgroundColor = pickedColor;
}
function PickColor(){
var random = Math.floor(Math.random()*colors.length);
return colors[random];
}
function ColorGeneratorMachine(size){
var colors = [];
for( var i =0 ;i < size ;i++){
colors.push(ColorGenerator());
}
return colors;
}
function ColorGenerator(){
var red = Math.floor(Math.random()*255+1)
var blue = Math.floor(Math.random()*255+1)
var green = Math.floor(Math.random()*255+1)
return "rgb(" + red + ", " + blue + ", " + green + ")";
}
function ResetProgram() {
colors = ColorGeneratorMachine(NumberOfSquares)
pickedColor = PickColor();
RGB.textContent = pickedColor ;
MessageDisplay.textContent = "";
WelcomeTable.style.backgroundColor = "steelblue";
for(var i =0; i < squares.length;i++) {
if(colors[i])
squares[i].style.backgroundColor= colors[i];
else
squares[i].style.backgroundColor = "#232323";
}
}