-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop-jigsaw.js
More file actions
67 lines (64 loc) · 2 KB
/
oop-jigsaw.js
File metadata and controls
67 lines (64 loc) · 2 KB
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
// each piece is like a graph node with 4 edges
class Piece {
constructor(row, col) {
this._position = {row: row, col: col}
this.name = Math.random().toString(36).substring(7)
this.up = null
this.down = null
this.left = null
this.right = null
}
}
class Puzzle {
constructor(n) {
this.jigsawGraph = genPieces(n)
this.N = n
}
genPieces(n) {
let graph = {}
let array = []
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++){
array.push(new Piece(i,j))
}
}
array.sort(() => Math.random() > 0.5 ? 1 : -1)
array.forEach((piece) => {
graph[piece.name] = piece
})
return graph
}
match(piece1, piece2, direction) {
if (piece1 === null || piece2 === null) {
return false
}
else if (direction === 'up') {
return (piece1._position.row === piece2._position.row + 1) &&
(piece1._position.col === piece2._position.col )
}
else if (direction === 'left') {
return (piece1._position.row === piece2._position.row) &&
(piece1._position.col === piece2._position.col + 1 )
}
else if (direction === 'right') {
return (piece1._position.row === piece2._position.row) &&
(piece1._position.col === piece2._position.col - 1)
}
else if (direction === 'down') {
return (piece1._position.row === piece2._position.row - 1) &&
(piece1._position.col === piece2._position.col )
}
else {
throw 'error, no direction'
}
}
checkPiece(piece) {
// check up; check null if at top row
if (piece._position.row===0 && piece.up !== null){
return false
}
else if (piece._position.row !== 0 && !this.match(piece, piece.up, 'up')) {
return false
}
}
}