-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
132 lines (118 loc) · 3.96 KB
/
Copy pathgame.js
File metadata and controls
132 lines (118 loc) · 3.96 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
/* TODO:1.Create a function for reset.
2.Call the reset function inside the init() method.
3.Refactor both the modes.
*/
var colors = generateRandomColors(6);
var numSquares = 6; //To avoid the bug for reset
var divs = document.getElementsByClassName("square");
var pickedColor = randomColor();
var display = document.getElementById("colorDisplay");
var h1 = document.getElementById("h1start")
var messageDisplay = document.querySelector("#message");
var newColors = document.getElementById("new-colors");
var easyMode = document.querySelector("#easyBtn");
var hardMode = document.querySelector("#hardBtn");
display.textContent = pickedColor;
easyMode.addEventListener("click", function() {
hardMode.classList.remove("blueButton");
easyMode.classList.add("blueButton");
numSquares = 3;
colors = generateRandomColors(numSquares);
pickedColor = randomColor();
display.textContent = pickedColor;
for (let i = 0; i < divs.length; i++) {
if(colors[i]) {
divs[i].style.background = colors[i];
}
else {
divs[i].style.display = "none";
}
}
})
hardMode.addEventListener("click", function() {
easyMode.classList.remove("blueButton");
hardMode.classList.add("blueButton");
numSquares = 6;
colors = generateRandomColors(numSquares);
pickedColor = randomColor();
display.textContent = pickedColor;
for (let i = 0; i < divs.length; i++) {
divs[i].style.display = "block";
divs[i].style.background = colors[i];
}
})
newColors.addEventListener("click", function() {
if(newColors.textContent == "Play Again ?") {
newColors.textContent = "New Colors";
messageDisplay.textContent = " ";
}
//Every new game should start how it looked in the beginning
h1.style.background = "#4682b4";
//generate all new colors
colors = generateRandomColors(numSquares);
//pick a new random color from the array
pickedColor = randomColor();
//change colordisplay to resemble the picked color
display.textContent = pickedColor;
//change the colors of the sqaure
for (let i = 0; i < divs.length; i++) {
//Assigning colors to the sqares
divs[i].style.backgroundColor = colors[i];
}
})
for (let i = 0; i < divs.length; i++) {
//Assigning colors to the sqares
divs[i].style.backgroundColor = colors[i];
//Assigning evenlisteners to the square divs
divs[i].addEventListener("click", function() {
//Matching The pattern
var clickedColor = this.style.backgroundColor;
if(clickedColor === pickedColor) {
//Change the Background of the h1 to th same background as the selected color
h1.style.background = pickedColor;
//Modifying a span saying it's correct instead of an alert
messageDisplay.textContent = "Correct";
changeColor();
newColors.textContent = "Play Again ?";
}
else {
//Fading The background if its incorrect
this.style.backgroundColor = "#232323";
//Adding a span and modifying it saying incorrect instead of alert
messageDisplay.textContent = "Try Again";
}
});
}
//Function for changing all the squares to the picked color is answer is correct
function changeColor() {
for (let i = 0; i < divs.length; i++) {
divs[i].style.backgroundColor = pickedColor;
}
}
//Function to create random selection from the colors array
function randomColor() {
var random = Math.floor(Math.random() * colors.length);
return colors[random];
}
//Function To push random colors in our array
function generateRandomColors(num) {
//make an array
var arr = [];
//add num colors to the array
for (let i = 0; i < num; i++) {
//Creating a function for generating random rgb values and pushing it into the array
arr.push(rgbColors());
}
//return that array
return arr;
}
//Function to generate random rgb colors
function rgbColors() {
//pick red from 0-255
var r = Math.floor(Math.random() * 256);
//pick green from 0-255
var g = Math.floor(Math.random() * 256);
//pick blue from 0-255
var b = Math.floor(Math.random() * 256);
return "rgb("+ r +", " + g +", " + b +")";
}