-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.js
188 lines (172 loc) · 5.46 KB
/
project1.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
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
var m = document.getElementById("moves");
var moves = 0;
var t = document.getElementById("Timer");
var time = 0;
var question = document.getElementsByClassName("question");
var colors = ["#FF355E", "#FFFF66", "#0000ff", "#66ff66", "#6CDAE7", "#FF6EFF"];
var work = document.getElementsByClassName("work");
var index = null;
console.log(question);
var score = window.localStorage.getItem("highscore");
if (score == null) {
document.getElementById("hiscore").innerHTML = "Hi-Score:";
} else {
document.getElementById("hiscore").innerHTML =
"Hi-Score: " + score + " moves";
}
var sec = window.localStorage.getItem("besttime");
if (sec == null) {
document.getElementById("besttime").innerHTML = "Best-Time:";
} else {
document.getElementById("besttime").innerHTML =
"Best-Time:" + sec + " seconds";
}
// ########## block display start ###########
const alphaCount = 9;
const deltaCount = 25;
for (let i = 0; i < alphaCount; i++) {
document.getElementById("alpha").innerHTML += `<div class="question"></div>`;
}
// ########## block display end ###########
for (var i = 0; i < 9; i++) {
var colr = colors[Math.floor(Math.random() * 6)];
question[i].style.backgroundColor = colr;
}
function bigb() {
document.getElementById("delta").style.display = "grid";
for (let i = 0; i < deltaCount; i++) {
document.getElementById("delta").innerHTML += `<div class="work"></div>`;
}
var ar = getarray();
function getarray() {
let temp, c;
var a = [];
for (var i = 0; i < 25; i++) a[i] = i;
for (i = 0; i < 25; i++) {
c = Math.floor(Math.random() * 25);
temp = a[c];
a[c] = a[i];
a[i] = temp;
}
return a;
}
for (i = 0; i < 24; i++) {
if (i < 9) {
var colr = question[i].style.backgroundColor;
work[ar[i]].style.backgroundColor = colr;
} else {
colr = colors[Math.floor(Math.random() * 6)];
work[ar[i]].style.backgroundColor = colr;
}
}
borderchange();
work[ar[24]].style.backgroundColor = "bisque";
index = ar[24];
}
var d = null;
document.getElementById("delta").addEventListener("click", (delta) => {
if (work[index - 5] == delta.target && index >= 5) {
//moving up
d = work[index - 5].style.backgroundColor;
work[index].style.backgroundColor = d;
work[index - 5].style.backgroundColor = "bisque";
index = index - 5;
++moves;
m.innerHTML = "Moves: " + moves;
condition = check();
}
if (work[index + 5] == delta.target && index <= 19) {
//moving down
d = work[index + 5].style.backgroundColor;
work[index].style.backgroundColor = d;
work[index + 5].style.backgroundColor = "bisque";
index = index + 5;
++moves;
m.innerHTML = "Moves: " + moves;
condition = check();
}
if (work[index - 1] == delta.target && index % 5 != 0) {
//moving left
d = work[index - 1].style.backgroundColor;
work[index].style.backgroundColor = d;
work[index - 1].style.backgroundColor = "bisque";
index = index - 1;
++moves;
m.innerHTML = "Moves: " + moves;
condition = check();
}
if (work[index + 1] == delta.target && index % 5 != 4) {
//moving right
d = work[index + 1].style.backgroundColor;
work[index].style.backgroundColor = d;
work[index + 1].style.backgroundColor = "bisque";
index = index + 1;
++moves;
m.innerHTML = "Moves: " + moves;
condition = check();
}
});
var condition = false;
// var s=document.getElementById("s")
// s.onclick=start();
function start() {
bigb();
seconds_stopwatch();
}
function seconds_stopwatch() {
t.innerHTML = "Seconds: " + time;
++time;
if (condition == false) setTimeout("seconds_stopwatch()", 1000);
else if (condition == true) {
alert(
"CONGRATULATIONS!!! You took " + moves + " moves and " + time + " seconds"
);
var tune = new Audio("gta_san_andreas.mp3");
tune.play();
if (score == null) {
score = moves;
windows.localStorage.setItem("highscore", score);
document.getElementById("hiscore").innerHTML =
"Hi-Score: " + score + " moves";
} else if (moves < score) {
window.localStorage.removeItem("highscore");
window.localStorage.setItem("highscore", moves);
document.getElementById("hiscore").innerHTML =
"Hi-Score: " + moves + " Moves";
}
if (sec == null) {
sec = time;
window.localStorage.setItem("besttime", time);
document.getElementById("besttime").innerHTML =
"Best-Time: " + time + " seconds";
} else if (timer < sec) {
window.localStorage.removeItem("besttime");
window.localStorage.setItem("besttime", time);
document.getElementById("besttime").innerHTML =
"Best-Time: " + time + " seconds";
}
}
}
function check() {
//to check if the task matches with the inner 3x3 grid
for (var i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (
question[i * 3 + j].style.backgroundColor !=
work[(i + 1) * 5 + j + 1].style.backgroundColor
)
break;
}
if (j != 3) break;
}
if (i == 3 && j == 3) return true;
else return false;
}
function borderchange() {
//to check if the task matches with the inner 3x3 grid so changing the border so it looks different
for (var i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
work[(i + 1) * 5 + j + 1].style.border = "solid black";
}
}
}