-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (191 loc) · 6.45 KB
/
index.js
File metadata and controls
196 lines (191 loc) · 6.45 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
var fJunction = () => {
var colors = ["red","red","red","red","green","green","green","green",
"orange","orange","orange","orange","white","white","white","white",
"yellow","yellow","yellow","yellow","blue","blue","blue","blue"];
var x = document.getElementsByClassName("cell");
var cells = [];
shuffleArray(colors);
let i,j,k;
for(j=0;j<5;j++){
cells[j] = Array();
for(k=0;k<5;k++)
cells[j][k] = x[k+j*5];
}
for(i=0;i<x.length;i++){
if (i <= 3)
x[i].style.backgroundColor = colors[i];
else if (i>=4 && i <= 7)
x[i].style.backgroundColor = colors[i];
else if (i>=8 && i <= 11)
x[i].style.backgroundColor = colors[i];
else if (i>=13 && i <= 15)
x[i].style.backgroundColor = colors[i-1];
else if (i>=16 && i <= 19)
x[i].style.backgroundColor = colors[i-1];
else if (i >=20 && i <= 24)
x[i].style.backgroundColor = colors[i-1];
}
Game(cells);
counter();
}
var counter = () => {
var minutes = document.getElementById("minutes");
var second = document.getElementById("seconds");
var tSecond = 0;
var timer = setInterval(Time, 1000);
function Time() {
++tSecond;
second.innerHTML = pad(tSecond % 60);
minutes.innerHTML = pad(parseInt(tSecond / 60));
}
function pad(val) {
var valstr = val + "";
if (valstr.length < 2) {
return "0" + valstr;
} else {
return valstr;
}
}
}
left = 37;
up = 38;
right = 39;
down = 40;
var cbox = () => {
var chbox = document.getElementById("checkbox");
if(chbox.checked == true){
left = 39;
right = 37
up=40;
down=38;
}else{
left = 37;
up = 38;
right = 39;
down = 40;
}
}
var Game = (array) => {
document.onkeydown = function(e) {
let i,j,n,m;
for(i =0;i<5;i++){
for(j=0;j<5;j++){
if(array[i][j].style.backgroundColor==""){
n=i;
m=j;
}
}
}
var verification = () => {
var x = document.getElementsByClassName("scells");
scells = [];
for(j=0;j<3;j++){
scells[j] = Array();
for(k=0;k<3;k++)
scells[j][k] = x[k+j*3];
}
counter = 0;
for(i =0;i<3;i++){
for(j=0;j<3;j++){
if(scells[i][j].style.backgroundColor == array[i+1][j+1].style.backgroundColor){
counter++;
}
}
}
console.log(counter);
if(counter==9){
var totalsec = parseInt(document.getElementById("minutes").innerHTML)*60+parseInt(document.getElementById("seconds").innerHTML);
if(totalsec<120){
alert("Well done your score is nice!"+"\n"+"Your time is: "+document.getElementById("minutes").innerHTML+" : "+document.getElementById("seconds").innerHTML);
}else if(totalsec>120 && totalsec<200){
alert("That's nice you can do better!"+"\n"+"Your time is: "+document.getElementById("minutes").innerHTML+" : "+document.getElementById("seconds").innerHTML);
}else{
alert("You need to imporove yourself!"+"\n"+"Your time is: "+document.getElementById("minutes").innerHTML+" : "+document.getElementById("seconds").innerHTML);
}
document.getElementById("counter").hidden = true;
}
}
switch (e.keyCode) {
case left://left
if(m>0){
var tmp= array[n][m-1].style.backgroundColor;
array[n][m-1].style.backgroundColor = array[n][m].style.backgroundColor;
array[n][m].style.backgroundColor = tmp;
verification();
console.log("left");
}
break;
case up://up
if(n>0){
var tmp= array[n-1][m].style.backgroundColor;
array[n-1][m].style.backgroundColor = array[n][m].style.backgroundColor;
array[n][m].style.backgroundColor = tmp;
verification();
console.log("up");
}
break;
case right://right
if(m<4){
var tmp= array[n][m+1].style.backgroundColor;
array[n][m+1].style.backgroundColor = array[n][m].style.backgroundColor;
array[n][m].style.backgroundColor = tmp;
verification();
console.log("right");
}
break;
case down://down
if(n<4){
var tmp= array[n+1][m].style.backgroundColor;
array[n+1][m].style.backgroundColor = array[n][m].style.backgroundColor;
array[n][m].style.backgroundColor = tmp;
verification();
console.log("down");
}
break;
}
};
}
var shuffleArray = (array) => {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
let shuffleTable = () => {
var colors = ["red","red","red","red","green","green","green","green",
"orange","orange","orange","orange","white","white","white","white",
"yellow","yellow","yellow","yellow","blue","blue","blue","blue"];
var x = document.getElementsByClassName("scells");
let i;
shuffleArray(colors);
for(i=0;i<x.length;i++){
if (i <= 2)
x[i].style.backgroundColor = colors[i];
else if (i>=3 && i <= 5)
x[i].style.backgroundColor = colors[i];
else if (i>=6 && i <= 8)
x[i].style.backgroundColor = colors[i];
}
}
// let verification = () => {
// }
var id;
let timeout = () =>{
id = setInterval(()=>{
shuffleTable();
},10);
}
let start = () =>{
timeout();
setTimeout(()=>{
clearInterval(id);
},1000);
setTimeout(()=>{
fJunction();
},2000);
document.getElementById("checkbox").hidden = true;
document.getElementById("label").hidden = true;
document.getElementById("start").hidden = true;
}