-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
142 lines (132 loc) · 4.2 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
//Initial References
const moves = document.getElementById("moves");
const container = document.querySelector(".container");
const startButton = document.getElementById("start-button");
const coverScreen = document.querySelector(".cover-screen");
const result = document.getElementById("result");
let currentElement = "";
let movesCount,
imagesArr = [];
const isTouchDevice = () => {
try {
//We try to create TouchEvent (it would fail for desktops ad throw error)
document.createEvent("TouchEvent");
return true;
} catch (e) {
return false;
}
};
//Random number for image
const randomNumber = () => Math.floor(Math.random() * 8) + 1;
//Get row and column value from data-position
const getCoords = (element) => {
const [row, col] = element.getAttribute("data-position").split("_");
return [parseInt(row), parseInt(col)];
};
//row1, col1 are image co-ordinates while row2 amd col2 are blank image co-ordinates
const checkAdjacent = (row1, row2, col1, col2) => {
if (row1 == row2) {
//left/right
if (col2 == col1 - 1 || col2 == col1 + 1) {
return true;
}
} else if (col1 == col2) {
//up/down
if (row2 == row1 - 1 || row2 == row1 + 1) {
return true;
}
}
return false;
};
//Fill array with random value for images
const randomImages = () => {
while (imagesArr.length < 8) {
let randomVal = randomNumber();
if (!imagesArr.includes(randomVal)) {
imagesArr.push(randomVal);
}
}
imagesArr.push(9);
};
//Generate Grid
const gridGenerator = () => {
let count = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let div = document.createElement("div");
div.setAttribute("data-position", `${i}_${j}`);
div.addEventListener("click", selectImage);
div.classList.add("image-container");
div.innerHTML = `<img src="./img/parts/image_part_00${
imagesArr[count]
}.jpg" class="image ${
imagesArr[count] == 9 ? "target" : ""
}" data-index="${imagesArr[count]}"/>`;
count += 1;
container.appendChild(div);
}
}
};
//Click the image
const selectImage = (e) => {
e.preventDefault();
//Set currentElement
currentElement = e.target;
//target(blank image)
let targetElement = document.querySelector(".target");
let currentParent = currentElement.parentElement;
let targetParent = targetElement.parentElement;
//get row and col values for both elements
const [row1, col1] = getCoords(currentParent);
const [row2, col2] = getCoords(targetParent);
if (checkAdjacent(row1, row2, col1, col2)) {
//Swap
currentElement.remove();
targetElement.remove();
//Get image index(to be used later for manipulating array)
let currentIndex = parseInt(currentElement.getAttribute("data-index"));
let targetIndex = parseInt(targetElement.getAttribute("data-index"));
//Swap Index
currentElement.setAttribute("data-index", targetIndex);
targetElement.setAttribute("data-index", currentIndex);
//Swap Images
currentParent.appendChild(targetElement);
targetParent.appendChild(currentElement);
//Array swaps
let currentArrIndex = imagesArr.indexOf(currentIndex);
let targetArrIndex = imagesArr.indexOf(targetIndex);
[imagesArr[currentArrIndex], imagesArr[targetArrIndex]] = [
imagesArr[targetArrIndex],
imagesArr[currentArrIndex],
];
//Win condition
if (imagesArr.join("") == "123456789") {
setTimeout(() => {
//When games ends display the cover screen again
coverScreen.classList.remove("hide");
container.classList.add("hide");
result.innerText = `Total Moves: ${movesCount}`;
startButton.innerText = "RestartGame";
}, 1000);
}
//Increment a display move
movesCount += 1;
moves.innerText = `Moves: ${movesCount}`;
}
};
//Start button click should display the container
startButton.addEventListener("click", () => {
container.classList.remove("hide");
coverScreen.classList.add("hide");
container.innerHTML = "";
imagesArr = [];
randomImages();
gridGenerator();
movesCount = 0;
moves.innerText = `Moves: ${movesCount}`;
});
//Display start screen first
window.onload = () => {
coverScreen.classList.remove("hide");
container.classList.add("hide");
};