-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (64 loc) · 2.03 KB
/
index.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
var rows=3;
var columns=3;
var currTile;
var otherTile;
var turns=0;
// var imgOrder=["00", "01", "02", "10", "11", "12", "20", "21", "22"];
var imgOrder=["00", "12", "10", "20", "11", "21", "02", "01", "22"];
window.onload=function(){
for(var i=0; i<rows; i++){
for(var j=0; j<columns; j++){
//<img id="0-0" src="Images/00.png"></img>
var tile=document.createElement("img");
tile.id=i.toString()+"-"+j.toString();
tile.src="Images/"+imgOrder.shift()+".png";
//Drag Functionality
tile.addEventListener("dragstart", dragStart);
tile.addEventListener("dragover", dragOver);
tile.addEventListener("dragenter", dragEnter);
tile.addEventListener("dragleave", dragLeave);
tile.addEventListener("drop", dragDrop);
tile.addEventListener("dragend", dragEnd);
document.getElementById("board").append(tile);
}
}
}
function dragStart(){
currTile=this;
}
function dragOver(e){
e.preventDefault();
}
function dragEnter(e){
e.preventDefault();
}
function dragLeave(e){
e.preventDefault();
}
function dragDrop(){
otherTile=this;
}
function dragEnd(){
if(!otherTile.src.includes("22")){
return;
}
var currCoords=currTile.id.split("-");
var x1=parseInt(currCoords[0]);
var y1=parseInt(currCoords[1]);
var otherCoords=otherTile.id.split("-");
var x2=parseInt(otherCoords[0]);
var y2=parseInt(otherCoords[1]);
var moveLeft=x1==x2 && y2==y1-1;
var moveRight=x1==x2 && y2==y1+1;
var moveUp=x1-1==x2 && y2==y1;
var moveDown=x1+1==x2 && y2==y1;
var isAdjacent=moveLeft || moveRight || moveUp || moveDown;
if(isAdjacent){
var currImg=currTile.src;
var otherImg=otherTile.src;
currTile.src=otherImg;
otherTile.src=currImg;
turns++;
document.getElementById("turns").innerHTML=turns;
}
}