This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.js
137 lines (124 loc) · 3.96 KB
/
maze.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
const EMPTY_PLACE_STRING = ''
const NUMBERS = ['1','2','3','4','5','6','7','8','9','10','j','q','k']
const SUITS = ['h', 'd', 'c', 's']
const div_id = 'field'
class MazeGame {
constructor(rules) {
this.rules = rules
this.field = []
for (let i = 0; i < SUITS.length; i++) {
for (let j = 0; j < NUMBERS.length; j++) {
this.field = this.field.concat(SUITS[i] + NUMBERS[j])
}
}
this.field.sort(() => Math.random() - 0.5)
this.field = rules.prepareField(this.field)
}
start() {
for (let i = 0; i < this.field.length; i++) {
this.createElem(i)
}
this.rebuildBlanks()
}
refresh() {
const div = document.getElementById(div_id)
while (div.firstChild) { div.removeChild(div.lastChild) }
this.start()
}
rebuildBlanks() {
for (let i = 0; i < this.field.length; i++) {
if (!this.field[i]) {
if (document.getElementById('blank_'+i)) {
document.getElementById('blank_'+i).remove()
}
this.createElem(i)
}
}
}
createElem(i) {
i = parseInt(i)
const card = this.field[i]
const y = Math.floor(i/9)
const x = i-(y*9)
const pos = {top: (y*98)+'px', left: (x*73)+'px'}
const elem = document.createElement('div')
elem.style.top = pos.top
elem.style.left = pos.left
if (card == '') {
elem.setAttribute('id', 'blank_'+i)
elem.setAttribute('data-idx', i)
elem.classList.add('empty')
const accepted = this.rules.acceptedBy(this.field, i)
elem.addEventListener('drop', (ev) => {
ev.preventDefault()
const card_id = ev.dataTransfer.getData('text/plain')
const idx1 = this.field.indexOf(card_id)
const idx2 = parseInt(ev.target.dataset.idx)
let tmp = this.field[idx1]
this.field[idx1] = this.field[idx2]
this.field[idx2] = tmp
this.refresh()
})
elem.addEventListener('dragenter', (ev) => {
if (accepted.includes(this.dragging)) {
ev.preventDefault()
ev.currentTarget.style.background = 'lightblue'
}
})
elem.addEventListener('dragover', (ev) => {
if (accepted.includes(this.dragging)) {
ev.preventDefault()
}
})
elem.addEventListener('dragleave', (ev) => {
ev.currentTarget.style.background = ''
})
} else {
elem.setAttribute('id', card)
elem.setAttribute('data-id', i)
elem.classList.add('card')
elem.style.backgroundImage = `url("cards_png/${card}.png")`
elem.setAttribute('draggable', true)
elem.addEventListener('dragstart', (ev) => {
this.dragging = card
ev.dataTransfer.setData('text/plain', ev.target.id)
})
elem.addEventListener('dragend', (ev) => {
this.dragging = null
ev.dataTransfer.clearData()
})
}
document.getElementById(div_id).append(elem)
}
}
class WikipediaRules {
prepareField(field) {
field.splice(8, 0, EMPTY_PLACE_STRING)
field.splice(17, 0, EMPTY_PLACE_STRING)
field = field.map((name, i) => {
return name.endsWith('k') ? EMPTY_PLACE_STRING : name
})
return field
}
acceptedBy(field, i) {
let accepted = []
const prev = i === 0 ? field[field.length - 1] : field[i - 1]
if (prev !== EMPTY_PLACE_STRING) {
const num = NUMBERS.indexOf(prev.slice(1)) + 1
if (num == 12) {
// We're on queen, any Ace can be put.
accepted = accepted.concat(SUITS.map(function(suit, i) { return suit + '1' }))
} else {
accepted = accepted.concat(prev[0] + NUMBERS[num])
}
}
const next = i + 1 === field.length ? field[0] : field[i + 1]
if (next !== EMPTY_PLACE_STRING) {
const num = NUMBERS.indexOf(next.slice(1)) - 1
if (num >= 0) {
accepted = accepted.concat(next[0] + NUMBERS[num])
}
}
return accepted
}
}