-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
210 lines (174 loc) · 5.42 KB
/
script.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
let row = 4; // Number of rows
let col = 4; // Number of columns
let currTile;
let otherTile;
let turns = 0;
let timer;
let startTime;
let elapsedTime = 0;
let cont = document.getElementById("container");
let header = document.getElementById("header");
let ins=document.getElementById("ins")
cont.style.display = "none";
window.onload = function () {
document.getElementById("startButton").addEventListener("click", startGame);
}
function startGame() {
cont.style.display = "block";
ins.style.display="none"
initializeBoard();
startTimer();
document.getElementById("startButton").disabled = true; // Disable start button after starting the game
header.style.display = "none";
}
function initializeBoard() {
// Clear the board
document.getElementById("board").innerHTML = '';
document.getElementById("pieces").innerHTML = '';
document.getElementById("message").innerText = '';
turns = 0;
document.getElementById("turns").innerText = turns;
for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
let tile = document.createElement("img");
tile.src = "White.png";
// DRAG FUNCTIONALITY
addDragAndTouchListeners(tile);
document.getElementById("board").append(tile);
}
}
// Choose a random image folder
let folderNum = Math.floor(Math.random() * 7) + 1;
let folderName = `image${folderNum}`;
//pieces
let pieces = [];
for (let i = 1; i <= row * col; i++) {
pieces.push(`${folderName}/${i}.jpg`); // put image set pieces into the array
}
pieces.reverse();
for (let i = 0; i < pieces.length; i++) {
let j = Math.floor(Math.random() * pieces.length);
//swap
let temp = pieces[i];
pieces[i] = pieces[j];
pieces[j] = temp;
}
for (let i = 0; i < pieces.length; i++) {
let tile = document.createElement("img");
tile.src = pieces[i];
// DRAG FUNCTIONALITY
addDragAndTouchListeners(tile);
document.getElementById("pieces").append(tile);
}
}
function addDragAndTouchListeners(tile) {
// Mouse events
tile.addEventListener("dragstart", dragStart);
tile.addEventListener("dragover", dragOver);
tile.addEventListener("dragenter", dragEnter);
tile.addEventListener("dragleave", dragLeave);
tile.addEventListener("drop", dragDrop);
tile.addEventListener("dragend", dragEnd);
// Touch events
tile.addEventListener("touchstart", touchStart);
tile.addEventListener("touchmove", touchMove);
tile.addEventListener("touchend", touchEnd);
}
// DRAG TILES
function dragStart(e) {
currTile = this;
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
}
function dragLeave(e) { }
function dragDrop() {
otherTile = this;
}
function dragEnd() {
if (currTile.src.includes("White")) {
return;
}
swapTiles();
turns += 1;
document.getElementById("turns").innerText = turns;
if (checkIfSolved()) {
document.getElementById("message").innerText = "Congratulations! You've solved the puzzle!";
stopTimer();
resetTimer();
header.style.display = "block"; // Show the header again
document.getElementById("startButton").disabled = false; // Enable start button again
}
}
// TOUCH TILES
function touchStart(e) {
e.preventDefault();
currTile = e.target;
}
function touchMove(e) {
e.preventDefault();
let touch = e.touches[0];
let targetElement = document.elementFromPoint(touch.clientX, touch.clientY);
if (targetElement && targetElement.tagName === "IMG") {
otherTile = targetElement;
}
}
function touchEnd(e) {
if (currTile && otherTile && currTile !== otherTile) {
swapTiles();
turns += 1;
document.getElementById("turns").innerText = turns;
if (checkIfSolved()) {
document.getElementById("message").innerText = "Congratulations! You've solved the puzzle!";
stopTimer();
resetTimer();
header.style.display = "block"; // Show the header again
document.getElementById("startButton").disabled = false; // Enable start button again
}
}
}
function swapTiles() {
if (currTile.src.includes("White")) {
return;
}
let currImg = currTile.src;
let otherImg = otherTile.src;
currTile.src = otherImg;
otherTile.src = currImg;
}
// Check if the puzzle is solved
function checkIfSolved() {
let tiles = document.getElementById("board").getElementsByTagName("img");
for (let i = 0; i < tiles.length; i++) {
let tileNum = tiles[i].src.match(/\/(\d+)\.jpg$/);
if (!tileNum || parseInt(tileNum[1]) !== i + 1) {
return false;
}
}
return true;
}
// TIMER FUNCTIONS
function startTimer() {
startTime = Date.now() - elapsedTime;
timer = setInterval(updateTimer, 1000);
}
function updateTimer() {
elapsedTime = Date.now() - startTime;
let seconds = Math.floor(elapsedTime / 1000) % 60;
let minutes = Math.floor(elapsedTime / 60000);
document.getElementById("timer").innerText = `${pad(minutes)}:${pad(seconds)}`;
}
function stopTimer() {
clearInterval(timer);
}
function resetTimer() {
clearInterval(timer);
elapsedTime = 0;
document.getElementById("timer").innerText = "00:00";
}
function pad(number) {
return number < 10 ? '0' + number : number;
}