-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8puzzle.js
408 lines (360 loc) · 10.8 KB
/
8puzzle.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
class PQ {
//dummy fill position 0 for simpler sink and swim operations
//heap will contains nodes
heap
size
constructor() {
this.size = 0
this.heap = [{ previos: null, board: null, moves: 0, priority: Number.MAX_SAFE_INTEGER }]
}
sink(idx) {
let c1 = idx * 2;
let c2 = c1 + 1
if (c2 <= this.size) {
let smaller = c1
if (this.heap[smaller].priority > this.heap[c2].priority)
smaller = c2
if (this.heap[idx].priority > this.heap[smaller].priority) {
this.swap(smaller, idx, this.heap)
this.sink(smaller)
}
} else if (c1 <= this.size && this.heap[idx].priority > this.heap[c1].priority) {
this.swap(c1, idx, this.heap)
this.sink(c1)
}
}
swim(idx) {
while (idx > 0) {
let parent = Math.floor(idx / 2);
if (parent > 0 && this.heap[parent].priority > this.heap[idx].priority)
this.swap(parent, idx, this.heap);
else
break;
idx = parent;
}
}
push(val) {
this.heap.push(val)
this.size++;
this.swim(this.size)
}
pop() {
this.swap(1, this.size, this.heap)
let max = this.heap[this.size]
this.size--
this.heap.pop()
this.sink(1)
return max
}
swap(i, j) {
let t = this.heap[i]
this.heap[i] = this.heap[j]
this.heap[j] = t
}
}
class Node {
previos;
board;
moves;
priority;
constructor(b, p, m) {
this.board = b;
this.previos = p;
this.moves = m;
this.priority = m + b.manhattan;
}
}
class Board {
board;
size;
hamming;
manhattan;
blankI;
blankJ;
// create a board from an n-by-n array of tiles,
// where tiles[row][col] = tile at (row, col)
constructor(tiles) {
this.manhattan = 0;
this.hamming = 0;
this.size = tiles.length;
this.board = emptyArray(this.size, this.size);
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
this.board[i][j] = tiles[i][j];
if (this.board[i][j] == 0) {
this.blankI = i;
this.blankJ = j;
}
let boardNum = this.abs(this.board[i][j] - (i * this.size + j + 1));
if (boardNum != 0 && this.board[i][j] != 0) {
this.hamming++;
this.manhattan += boardNum % this.size + parseInt(boardNum / this.size);
}
}
}
}
exchangeBlank(i, j) {
let temp = this.board[i][j];
this.board[i][j] = 0;
this.board[this.blankI][this.blankJ] = temp;
this.blankI = i;
this.blankJ = j;
}
abs(num) {
if (num < 0) {
return -num;
}
return num;
}
// string representation of this board
toString() {
let out = this.size;
for (let i = 0; i < this.size; i++) {
out += "\n";
for (let j = 0; j < this.size; j++) {
out += this.board[i][j];
}
}
return out;
}
// board dimension n
dimension() {
return this.size;
}
// is this board the goal board?
isGoal() {
if (this.hamming == 0) {
return true;
}
return false;
}
equals(y) {
if (this.size != y.length)
return false;
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
if (y[i][j] != this.board[i][j])
return false;
}
}
return true;
}
// all neighboring boards
neighbors() {
let n = [];
let moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]
moves.forEach(move => {
let ni = this.blankI + move[0];
let nj = this.blankJ + move[1];
if (ni < this.size && ni >= 0 && nj < this.size && nj >= 0) {
this.exchangeBlank(ni, nj);
let b = new Board(this.board);
this.exchangeBlank(ni - move[0], nj - move[1]);
n.push(b);
}
})
return n;
}
exchange(i, j, si, sj) {
let temp = this.board[i][j];
this.board[i][j] = this.board[si][sj];
this.board[si][sj] = temp;
}
randomInt(size = this.size) {
return Math.floor((Math.random() * size))
}
// a board that is obtained by exchanging any pair of tiles
twin() {
let fi = this.randomInt();
let fj = this.randomInt();
while (fi === this.blankI && fj === this.blankJ) {
fi = this.randomInt();
fj = this.randomInt();
}
let si = this.randomInt();
let sj = this.randomInt();
while ((si === this.blankI && sj === this.blankJ) || (fi === si && fj === sj)) {
si = this.randomInt();
sj = this.randomInt();
}
this.exchange(si, sj, fi, fj);
let b = new Board(this.board);
this.exchange(si, sj, fi, fj);
return b;
}
}
class Solver {
Solution;
stk = [];
// find a solution to the initial board (using the A* algorithm)
constructor(initial) {
this.Solution = null;
let pq = new PQ();
pq.push(new Node(initial, null, 0));
while (pq.size > 0) {
let node = pq.pop();
if (node.moves > 50) {
console.log("huge");
break;
}
if (node.board.isGoal() == true) {
this.Solution = node;
let copy = node;
while (copy != null) {
this.stk.push(copy.board.board);
copy = copy.previos;
}
break;
}
node.board.neighbors().forEach(n => {
if (node.previos != null && node.previos.board.equals(n.board) == false) {
pq.push(new Node(n, node, node.moves + 1));
} else if (node.previos == null) {
pq.push(new Node(n, node, node.moves + 1));
}
})
}
}
// min number of moves to solve initial board; -1 if unsolvable
moves() {
if (this.Solution == null)
return -1;
return this.Solution.moves;
}
// sequence of boards in a shortest solution; null if unsolvable
solution() {
let ans = [...this.stk]
return ans.reverse()
}
}
const row = 3
const col = 3
const images = {
1: "./doge/1.png",
2: "./doge/2.png",
3: "./doge/3.png",
4: "./doge/4.png",
5: "./doge/5.png",
6: "./doge/6.png",
7: "./doge/7.png",
8: "./doge/8.png",
9: "./doge/9.png",
0: ""
}
let tiles = [[1, 2, 3], [4, 5, 6,], [7, 8, 0]]
window.addEventListener('load', () => {
checkoverflow()
document.querySelector('#shuffle').addEventListener('click', generateGrid)
document.querySelector('#start').addEventListener('click', solve);
// document.querySelector('.box').style.gridTemplateColumns = `repeat(${col},1fr)`
generateGrid()
});
async function solve() {
const board = new Board(tiles);
const solver = new Solver(board);
const solution = solver.solution()
let prevNum = null;
for (const board of solution) {
let curri, currj;
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 0) {
curri = i;
currj = j;
break;
}
}
}
const currNum = (curri * col + currj + 1) % 9;
if (prevNum !== null) {
console.log("hello");
const currBox = document.querySelector(`.box${currNum}`)
const prevBox = document.querySelector(`.box${prevNum}`)
// console.log(currBox,prevBox);
currBox.classList.add(`box${prevNum}`)
currBox.classList.remove(`box${currNum}`)
prevBox.classList.add(`box${currNum}`)
prevBox.classList.remove(`box${prevNum}`)
// console.log(currBox, prevBox);
//tricky situation
//reference is changed here because of changing of classes
//currBox has become prevBox and prevBox has becom currBox
currBox.classList.remove('blankBox')
prevBox.classList.add('blankBox')
}
prevNum = currNum
await sleep(500);
}
tiles = solution[solution.length-1]
document.querySelector('.box0').innerHTML = '<img src="./doge/9.png" />'
}
function isValid(arr) {
let flat = JSON.parse(JSON.stringify(arr)).flat()
let inversion = 0;
for (let i = 0; i < flat.length; i++) {
for (let j = i + 1; j < flat.length; j++) {
if (flat[i] === 0 || flat[j] === 0) continue;
if (flat[j] < flat[i]) inversion++
}
}
if (inversion % 2 === 0) return true;
return false;
}
function generateGrid() {
do {
tiles = shuffle(tiles);
console.log("Turn");
}
while (!isValid(tiles))
const box = document.querySelector('.box');
box.innerHTML = ''
console.log(tiles);
for (let i = 0; i < col; i++) {
for (let j = 0; j < col; j++) {
let div = document.createElement('div');
let id = tiles[i][j];
div.classList = `box${(i * col + j + 1) % 9} childBox`;
div.innerHTML = `<img src="${images[id]}" />`;
box.appendChild(div);
}
}
}
function getRowCol(num, columns) {
let row = Math.floor(num / columns);
let col = num % columns;
return [row, col];
}
function shuffle(array, n = 9) {
let arr = JSON.parse(JSON.stringify(array))
for (let k = n - 1; k > 0; k--) {
let rcol = Math.floor(Math.random() * (k + 1))
let [oi, oj] = getRowCol(k, col);
let [ni, nj] = getRowCol(rcol, col);
let temp = arr[oi][oj];
arr[oi][oj] = arr[ni][nj];
arr[ni][nj] = temp;
}
return arr;
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function emptyArray(r, c) {
let arr = new Array(r)
for (let i = 0; i < r; i++) {
arr[i] = new Array(c);
arr[i].fill(0);
}
return arr
}
function checkoverflow(box = document.querySelector('.box')) {
let makeWidth = screen.width * 0.90
if (makeWidth > screen.height * 0.90) {
makeWidth = screen.height * 0.90
}
makeWidth = Math.floor(makeWidth)
makeWidth = makeWidth>400?400:makeWidth
box.style.width = `${makeWidth}px`
box.style.height = `${makeWidth}px`
console.log(makeWidth);
}