-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzlegame.js
244 lines (170 loc) · 5.58 KB
/
puzzlegame.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const loadViewSkills = () => {
showScore.innerText=`asdf`;
showScore.classList.remove('scoreArea');
clearInterval(timer);
}
let seconds = 0;
let minutes = 0;
let hours = 0;
let interval;
function startTimer() {
clearInterval(interval);
interval = setInterval(function() {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
}
}
let timeString = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
document.getElementById("timer").innerHTML = timeString;
}, 1000);
}
function stopTimer() {
clearInterval(interval);
}
var rows = 3;
var columns = 3;
var currTile;
var otherTile; //blank tile
var turns = 0;
var imgOrderOriginal = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
//var imgOrder = ["1", "3", "2", "4", "5", "6", "7", "8", "9"];
var imgOrder = ["4", "2", "8", "5", "1", "6", "7", "9", "3"];
window.onload = function() {
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
for (let r=0; r < rows; r++) {
for (let c=0; c < columns; c++) {
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
console.log(imgOrder);
console.log(imgOrderOriginal);
//<img id="0-0" src="1.jpg">
let tile = document.createElement("img");
tile.id = r.toString() + "-" + c.toString();
tile.src = imgOrder.shift() + ".jpg";
//DRAG FUNCTIONALITY
tile.addEventListener("dragstart", dragStart); //click an image to drag
tile.addEventListener("dragover", dragOver); //moving image around while clicked
tile.addEventListener("dragenter", dragEnter); //dragging image onto another one
tile.addEventListener("dragleave", dragLeave); //dragged image leaving anohter image
tile.addEventListener("drop", dragDrop); //drag an image over another image, drop the image
tile.addEventListener("dragend", dragEnd); //after drag drop, swap the two tiles
document.getElementById("board").append(tile);
}
}
}
function dragStart() {
currTile = this; //this refers to the img tile being dragged
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
}
function dragOver(e) {
e.preventDefault();
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
}
function dragEnter(e) {
e.preventDefault();
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
}
function dragLeave() {
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
}
function dragDrop() {
otherTile = this; //this refers to the img tile being dropped on
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
}
function dragEnd() {
if (imgOrder === imgOrderOriginal){
loadViewSkills();
}
startTimer();
if (!otherTile.src.includes("3.jpg")) {
return;
}
let currCoords = currTile.id.split("-"); //ex) "0-0" -> ["0", "0"]
let r = parseInt(currCoords[0]);
let c = parseInt(currCoords[1]);
let otherCoords = otherTile.id.split("-");
let r2 = parseInt(otherCoords[0]);
let c2 = parseInt(otherCoords[1]);
let moveLeft = r == r2 && c2 == c-1;
let moveRight = r == r2 && c2 == c+1;
let moveUp = c == c2 && r2 == r-1;
let moveDown = c == c2 && r2 == r+1;
let isAdjacent = moveLeft || moveRight || moveUp || moveDown;
if (isAdjacent) {
let currImg = currTile.src;
let otherImg = otherTile.src;
currTile.src = otherImg;
otherTile.src = currImg;
turns += 1;
document.getElementById("turns").innerText = turns;
}
}
// home.addEventListener('click', () => {
// <a href = "file:///C:/elitmus/home/home.html"> </a>
// })
// var rows = 3;
// var columns = 3;
// var curTile;
// var otherTile;
// var turns = 0;
// //var imgOrder = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
// var imgOrder = ["4", "2", "8", "5", "1", "6", "7", "9", "3"];
// window.onload = function() {
// for(let r = 0; r < rows; r++){
// for(let c = 0; c < columns; c++) {
// let tile = document.createElement("img");
// tile.id = r.toString() + "-" + c.toString();
// tile.src = imgOrder.shift() + ".jpg";
// tile.addEventListener("dragstart", dragStart); //click an image to drag
// tile.addEventListener("dragover", dragOver); //moving image around while clicked
// tile.addEventListener("dragenter", dragEnter); //dragging image onto another one
// tile.addEventListener("dragleave", dragLeave); //dragged image leaving anohter image
// tile.addEventListener("drop", dragDrop); //drag an image over another image, drop the image
// tile.addEventListener("dragend", dragEnd);
// document.getElementById("board").append(title);
// }
// }
// }
// function dragStart(){
// curTile = this;
// }
// function dragOver(e) {
// e.preventDefault();
// }
// function dragEnter(e) {
// e.preventDefault();
// }
// function dragLeave() {
// }
// function dragDrop() {
// otherTile = this;
// }
// function dragEnd() {
// let currImg = currTile.src;
// let otherImg = otherTile.src;
// currTile.src = otherImg;
// otherTile.src = currImg;
// }