-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.js
81 lines (73 loc) · 2.07 KB
/
index2.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
// distance: number of pixels a puzzle piece will move
const distance = 100;
// below is a 4x4 double array to emulate the gameboard
const puzzlePieces = [
[
{ name: ".box1", x: 0, y: 0 },
{ name: ".box5", x: 0, y: 1 },
{ name: ".box9", x: 0, y: 2 },
{ name: ".box13", x: 0, y: 3 }
],
[
{ name: ".box2", x: 1, y: 0 },
{ name: ".box6", x: 1, y: 1 },
{ name: ".box10", x: 1, y: 2 },
{ name: ".box14", x: 1, y: 3 }
],
[
{ name: ".box3", x: 2, y: 0 },
{ name: ".box7", x: 2, y: 1 },
{ name: ".box11", x: 2, y: 2 },
{ name: ".box15", x: 2, y: 3 }
],
[
{ name: ".box4", x: 3, y: 0 },
{ name: ".box8", x: 3, y: 1 },
{ name: ".box12", x: 3, y: 2 },
{ name: ".blank", x: 3, y: 3 }
]
];
const puzzle = {
pieces: puzzlePieces,
currentPiece: null,
initialize: function() {
// attach click event handlers for each piece
const boxes = [...document.querySelectorAll('[class^="box"]')];
boxes.forEach(box => {
box.addEventListener("click", () => {
this.currentPiece = box;
this.slide();
});
});
// shuffle board
// this.shuffle();
// show puzzle pieces
this.display();
},
display: function() {
// initialize pieces to their proper order
for (let i = 0; i <= 3; i++) {
for (let j = 0; j <= 3; j++) {
const pieceDOM = document.querySelector(puzzlePieces[i][j].name);
TweenLite.set(pieceDOM, { x: i * 100, y: j * 100 });
}
}
},
slide: function() {
// is there a blank square immediately above, left, below, or right of clicked square? Return direction.
const direction = this.findDirection();
// if so, move direction
this.move(direction);
// check if arranged in order. If so, "winner!"
this.checkWinner();
},
findDirection: function() {
// check for blank square above, left, below, or right of clicked square,
// then return a direction "up", "down", "left", or "right"
// move direction
// check if winner
},
move: function() {},
checkWinner: function() {}
};
puzzle.initialize();