-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (143 loc) · 5.37 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
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
window.onload = function () {
// Global variables needed for the game
var board = [], boardSize = 4, num, puzzleHolder, emptyLoc = [];
// Get the puzzleHolder div
puzzleHolder = document.querySelector(".puzzle-holder");
// Initalize the board for playing
var init = function () {
// Max number for the board. - 1 because one tile should be 0 - empty tile
num = (boardSize * boardSize) - 1;
for (var i = 0; i < boardSize; i++) {
// Push the array for the first row
board[i] = [];
// Loop through the row and push the numbers. Also decrease the number after each push
for (var j = 0; j < boardSize; j++) {
board[i].push(num);
num--;
}
}
}
// Draw the board to the DOM
var draw = function () {
var isEmpty = false;
// Reset the puzzle holder before rendering
puzzleHolder.innerHTML = "";
for (var i = 0; i < boardSize; i++) {
for (var j = 0; j < boardSize; j++) {
// Call the helper function to create each puzzle piece, and append it to the holder div
if (board[i][j] === 0) {
isEmpty = true;
}
else {
isEmpty = false;
}
puzzleHolder.append(createPuzzlePiece(board[i][j], isEmpty));
}
}
}
// Helper function to create each puzzle piece
var createPuzzlePiece = function (tile, isEmpty) {
// Puzzle piece to be pushed to the puzzle holder
var puzzlePiece = document.createElement("div");
// Add a class to it, and add the value
puzzlePiece.className = "one-piece puzzle-" + tile;
puzzlePiece.innerHTML = tile;
if (isEmpty) {
puzzlePiece.className += " empty-tile";
puzzlePiece.innerHTML = " ";
}
puzzlePiece.onclick = move.bind(this, tile);
return puzzlePiece;
}
// Accept a tile, and move it if the move is legal
var move = function (tile) {
// Loop through the board and store the empty tile location once found
for (var k = 0; k < boardSize; k++) {
for (var l = 0; l < boardSize; l++) {
if (board[k][l] == 0) {
emptyLoc[0] = l;
emptyLoc[1] = k;
}
}
}
// Temp value to store the tile when swapping with empty tile
var tempVal;
for (var i = 0; i < boardSize; i++) {
for (var j = 0; j < boardSize; j++) {
// If it's the tile that the user selected
if (board[i][j] === tile) {
// Find out if the move is legal and swap the tiles. Also update the location of the empty tile after swapping!
if (((emptyLoc[0] + 0 === j) && (emptyLoc[1] + 1 === i)) || ((emptyLoc[0] + 0 === j) && (emptyLoc[1] - 1 === i))
|| ((emptyLoc[0] - 1 === j) && (emptyLoc[1] + 0 === i)) || ((emptyLoc[0] + 1 === j) && (emptyLoc[1] + 0 === i))) {
tempVal = board[i][j];
board[i][j] = 0;
board[emptyLoc[1]][emptyLoc[0]] = tempVal;
emptyLoc[0] = j;
emptyLoc[1] = i;
document.querySelector(".puzzle-" + tile).className += " animate";
window.setTimeout(function() {
draw();
}, 300);
return true;
}
}
}
}
}
// Check if the game is won
var won = function () {
var isWon = false;
for (var k = 0; k < boardSize; k++) {
for (var l = 0; l < boardSize; l++) {
if (l < boardSize - 1) {
if (l + 1 === boardSize - 1 && k === boardSize - 1) {
if (board[k][l + 1] === 0) {
isWon = true;
}
else {
isWon = false;
}
}
else {
if (((board[k][l] + 1) == board[k][l + 1])) {
isWon = true;
}
else {
isWon = false;
break;
}
}
}
}
}
return isWon;
}
// Win the game btn
var winBtn = document.getElementById("winTheGame");
var resetBtn = document.getElementById("resetTheGame");
winBtn.onclick = function () {
board = [];
num = 1;
for (var i = 0; i < boardSize; i++) {
// Push the array for the first row
board[i] = [];
// Loop through the row and push the numbers. Also decrease the number after each push
for (var j = 0; j < boardSize; j++) {
if(num === 16) {
board[i].push(0);
}
else {
board[i].push(num);
num++;
}
}
}
draw();
}
resetBtn.onclick = function () {
init();
draw();
}
init();
draw();
}