-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·182 lines (156 loc) · 7.87 KB
/
index.html
File metadata and controls
executable file
·182 lines (156 loc) · 7.87 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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Can you match all squares?</title>
<link href="./square_style.css" rel="stylesheet " type="text/css">
</head>
<body>
<div></div>
<div></div>
<div class="container">
<div class="row">
<div id="sq1" class="square" onclick="colorSelector(this)"></div>
<div id="sq2" class="square" onclick="colorSelector(this)"></div>
<div id="sq3" class="square" onclick="colorSelector(this)"></div>
<div id="sq4" class="square" onclick="colorSelector(this)"></div>
</div>
<div class="row">
<div id="sq5" class="square" onclick="colorSelector(this)"></div>
<div id="sq6" class="square" onclick="colorSelector(this)"></div>
<div id="sq7" class="square" onclick="colorSelector(this)"></div>
<div id="sq8" class="square" onclick="colorSelector(this)"></div>
</div>
<div class="row">
<div id="sq9" class="square" onclick="colorSelector(this)"></div>
<div id="sq10" class="square" onclick="colorSelector(this)"></div>
<div id="sq11" class="square" onclick="colorSelector(this)"></div>
<div id="sq12" class="square" onclick="colorSelector(this)"></div>
</div>
<div class="row">
<div id="sq13" class="square" onclick="colorSelector(this)"></div>
<div id="sq14" class="square" onclick="colorSelector(this)"></div>
<div id="sq15" class="square" onclick="colorSelector(this)"></div>
<div id="sq16" class="square" onclick="colorSelector(this)"></div>
</div>
</div>
<script src="jquery-3.2.1.js"></script>
<script type="text/javascript">
function shuffle(array) {
var m = array.length, temp, i;
//while there remain elements to shuffle...
while (m) {
//Pick a remaining element...
i = Math.floor(Math.random() * m--);
//and swap it with the current element
temp = array[m];
array[m] = array[i];
array[i] = temp;
}
return array;
}
//globals
//color_array will hold 8 different rgb colors
var color_array = [];
//grid array will hold the colors based on position
var grid = new Array();
for (i = 0; i < 8; i++) {
random_color = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)]
color_array.push(random_color);
//console.log(color_array[i]);
}
//console_array now holds 8 arrays of arrays [[r,b,g],...]
//shuffles the color_array
color_array = shuffle(color_array);
//grid will contain 16 random colors, represented as arrays [r,b,g], 8 of them unique
for (i = 0; i < color_array.length; i++) {
grid.push(color_array[i]);
//shuffles the grid array
//grid = shuffle(grid);
grid.push(color_array[i]);
}
//shuffle the grid array. It will contain 8 unique colors represented twice
grid = shuffle(grid);
//globals
var firstClicked, secondClicked, clickedDivArray = new Array(), matched_array = new Array(16);
var index1, index2;
//an array with 16 elements all of them false
matched_array.fill(false);
function setColor(elem, newColor) {
elem.style.backgroundColor = newColor;
}
function matchTwo(color_value, clickedDiv,grid_index) {
if (clickedDivArray.length == 0) {
clickedDivArray.push(clickedDiv);
console.log('first button clicked');
index1 = grid_index;
}
else {
if (clickedDivArray.length == 1) {
console.log('second button clicked')
clickedDivArray.push(clickedDiv);//clickedDivArray should now me of length 2
console.log(clickedDivArray)
//clickedDiv is the unique div that was clicked. Comparison is uniqueness of element, not color
if (clickedDivArray[0] == clickedDivArray[1]) {//if the same div clicked again, pop off the second instance
clickedDivArray.pop();//clickedDivArray is now of length 1
console.log('clicked same element again');
} else {//clickedDivs were unique
if (clickedDivArray[0].style.backgroundColor == clickedDivArray[1].style.backgroundColor) {
//both elements clicked share same background color
index2 = grid_index;
console.log("both elements have the same color!")
//index1 = grid.indexOf(clickedDivArray[0].style.backgroundColor);
//index2 = grid.lastIndexOf(clickedDivArray[1].style.backgroundColor);
console.log(index1,index2);
matched_array[index1] = true;
matched_array[index2] = true;
//clickedDivArray is reset
clickedDivArray = new Array();
//indexes are reset
index1, index2 = -1;
}
else { //elements did not share the same background color
console.log('elements did not match')
console.log(clickedDivArray[1].style.backgroundColor);
//divs did not match in color
//current_color = clickedDivArray[1].style.backgroundColor;
//setColor(clickedDiv,current_color);
//setTimeout(setColor(clickedDivArray[1], current_color), 1000);
setTimeout(setColor,1000, clickedDivArray[0], 'dimgrey');
setTimeout(setColor,1000, clickedDivArray[1], 'dimgrey');
//setTimeout(clickedDivArray[1].style.backgroundColor = 'dimgrey', 1000);
console.log(clickedDivArray[1].style.backgroundColor);
clickedDivArray = new Array();
}
}//end of unique clickedDivs Match
}//end logical check: clickedDivArray.length == 1
}
//console.log('fell out of ')
console.log('falling out of matchTwo function')
}//end of matchTwo function
//if a div element has already been matched ignore it! this is where the matched_array would come into play
function colorSelector(element) {
elem_id = element.id;
grid_index = Number(elem_id.split('sq')[1]) - 1;
console.log(matched_array);
if (matched_array[grid_index]!=true) {
rgb = grid[grid_index];
current_element_color = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'
setColor(element, current_element_color);
//console.log(grid[0]);
console.log('element background color changed to '+current_element_color);
matchTwo(rgb, element,grid_index);
} else {
console.log('do nothing!')
}
}
if (matched_array.includes(false)){
console.log('not finished yet')
} else {
}
/*$('.square').click(function (e) {
this.backgroundColor = 'rbg' + grid[0].join(',') + ')';
});*/
</script>
</body>
</html>