-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathpuzzles.coffee
372 lines (358 loc) · 11.1 KB
/
pathpuzzles.coffee
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
# These widths must match those in pathpuzzles.styl
majorWidth = 3/36
minorWidth = 1/36
plusLength = 9/36
wallLength = 1 - 2*plusLength # 18/36 = 1/2
halfWallLength = wallLength / 2
class Puzzle
constructor: (@cell) ->
@width = (@cell[0].length + 1) // 2
@height = (@cell.length + 1) // 2
@edges = {}
@rowSums = {}
@colSums = {}
for row, i in @cell
unless isNaN (value = parseInt row[0], 10)
@rowSums[i / 2] = value
for col, j in @cell[0]
unless isNaN (value = parseInt col, 10)
@colSums[j / 2] = value
cellDegrees: ->
degree = {}
increment = (x, y) ->
degree[[x,y]] ?= 0
degree[[x,y]]++
for key, value of @edges when value == true
[x, y] = (parseFloat(z) for z in key.split ',')
increment Math.floor(x), Math.floor(y)
increment Math.ceil(x), Math.ceil(y)
degree
actualSums: ->
rowSums = {}
colSums = {}
for key of @cellDegrees()
[x, y] = (parseFloat(z) for z in key.split ',')
if 0 < x < @width and 0 < y < @height
rowSums[y] ?= 0
rowSums[y]++
colSums[x] ?= 0
colSums[x]++
### Old code assumed correct degrees:
for key, value of @edges when value == true
[x, y] = (parseFloat(z) for z in key.split ',')
for [vx, vy] in [[Math.floor(x), Math.floor(y)],
[Math.ceil(x), Math.ceil(y)]]
if 0 < vx < @width and 0 < vy < @height
rowSums[vy] ?= 0
rowSums[vy]++
colSums[vx] ?= 0
colSums[vx]++
###
{rowSums, colSums}
checkSolved: ->
## Check degrees are all 2
for key, value of @cellDegrees() when value != 2
[x, y] = (parseFloat(z) for z in key.split ',')
if 0 < x < @width and 0 < y < @height
return false
#console.log 'pass degree'
## Check row/column sums
{rowSums, colSums} = @actualSums()
for i, target of @rowSums
return false unless target == (rowSums[i] ? 0)
for j, target of @colSums
return false unless target == (colSums[j] ? 0)
#console.log 'pass sums'
## Check connectivity
ends =
for key, value of @edges when value == true
[x, y] = (parseFloat(z) for z in key.split ',')
continue if 1 <= x <= @width-1 and 1 <= y <= @height-1
[x, y]
here = ends[0]
visited = {}
visited[here] = true
if here[0] < 1 or here[1] < 1
last = (Math.floor(z) for z in here)
here = (Math.ceil(z) for z in here)
else if here[0] > @width-1 or here[1] > @height-1
last = (Math.ceil(z) for z in here)
here = (Math.floor(z) for z in here)
else
throw new Error 'assertion error: invalid end'
loop
for dir in [[-0.5,0], [+0.5,0], [0,-0.5], [0,+0.5]]
neighbor = add here, dir
next = add neighbor, dir
continue if eq next, last # prevent revisiting previous vertex
break if @edges[neighbor] == true
unless @edges[neighbor] == true
throw new Error 'assertion error: no neighbor'
visited[neighbor] = true
break if eq neighbor, ends[1]
last = here
here = next
for key, value of @edges when value == true
return false unless visited[key]
#console.log 'pass connectivity'
true
class Display
constructor: (@svg, @puzzle) ->
@background = @svg.rect @puzzle.width + 0.5, @puzzle.height + 0.5
.move -0.5, -0.5
.addClass 'background'
@solutionGroup = @svg.group()
.addClass 'solution'
@userGroup = @svg.group()
.addClass 'user'
@puzzleGroup = @svg.group()
.addClass 'puzzle'
@errorsGroup = @svg.group()
.addClass 'errors'
@drawPuzzle()
@drawErrors()
drawPuzzle: ->
@puzzleGroup.clear()
@solutionGroup.clear()
@background.size @puzzle.width + 0.5, @puzzle.height + 0.5
neighbor = (di, dj) => @puzzle.cell[i+di]?[j+dj] == '+'
@rowRect = {}
@colRect = {}
for row, i in @puzzle.cell
y = i / 2
for cell, j in row
x = j / 2
switch cell
when '+' # grid vertex
@puzzleGroup.line(
if neighbor 0, -2 then x - plusLength else x
y
if neighbor 0, +2 then x + plusLength else x
y
)
@puzzleGroup.line(
x
if neighbor -2, 0 then y - plusLength else y
x
if neighbor +2, 0 then y + plusLength else y
)
when 'x' # wall
if i % 2 == 1 and j % 2 == 0
@puzzleGroup.line x - halfWallLength, y, x + halfWallLength, y
else if j % 2 == 1 and i % 2 == 0
@puzzleGroup.line x, y - halfWallLength, x, y + halfWallLength
when 's' # solution
if i % 2 == 1 and j % 2 == 0
@solutionGroup.line x, y - 0.5, x, y + 0.5
else if j % 2 == 1 and i % 2 == 0
@solutionGroup.line x - 0.5, y, x + 0.5, y
else
int = parseInt cell, 10
unless isNaN int
rect =
@puzzleGroup.rect 0.5, 0.66
.center x, y
if x == 0
@rowRect[y] = rect
else if y == 0
@colRect[x] = rect
@puzzleGroup.text cell
.attr 'x', x
.attr 'y', y + 8/36
@svg.viewbox
x: -0.5 - majorWidth/2
y: -0.5 - majorWidth/2
width: @puzzle.width + 0.5 + majorWidth
height: @puzzle.height + 0.5 + majorWidth
drawSolution: ->
@solutionGroup.clear()
drawErrors: ->
@errorsGroup.clear()
for key, value of @puzzle.cellDegrees() when value > 2
[x, y] = (parseFloat(z) for z in key.split ',')
@errorsGroup.circle 0.33, 0.33
.center x, y
{rowSums, colSums} = @puzzle.actualSums()
setCorrect = (rect, bool) ->
return unless rect?
if bool
rect.addClass 'correct'
else
rect.removeClass 'correct'
for i, target of @puzzle.rowSums
setCorrect @rowRect[i], target == (rowSums[i] ? 0)
for j, target of @puzzle.colSums
setCorrect @colRect[j], target == (colSums[j] ? 0)
eq = (u,v) -> u[0] == v[0] and u[1] == v[1]
add = (u,v) -> [u[0] + v[0], u[1] + v[1]]
sub = (u,v) -> [u[0] - v[0], u[1] - v[1]]
perp = (v) -> [-v[1], v[0]]
edge2dir = (edge) ->
[
edge[0] - Math.floor edge[0]
edge[1] - Math.floor edge[1]
]
class Player extends Display
constructor: (...args) ->
super ...args
@highlightEnable()
highlightEnable: ->
@lines = {}
rt2o2 = Math.sqrt(2)/2
@highlight = @svg.rect rt2o2, rt2o2
.center 0, 0
.addClass 'target'
.opacity 0
event2coord = (e) =>
pt = @svg.point e.clientX, e.clientY
rotated =
x: rt2o2 * (pt.x + pt.y)
y: rt2o2 * (-pt.x + pt.y)
rotated.x /= rt2o2
rotated.y /= rt2o2
rotated.x -= 0.5
rotated.y -= 0.5
rotated.x = Math.round rotated.x
rotated.y = Math.round rotated.y
rotated.x += 0.5
rotated.y += 0.5
rotated.x *= rt2o2
rotated.y *= rt2o2
coord = [
0.5 * Math.round 2 * rt2o2 * (rotated.x - rotated.y)
0.5 * Math.round 2 * rt2o2 * (rotated.x + rotated.y)
]
if 0 < coord[0] < @puzzle.width and 0 < coord[1] < @puzzle.height and
@puzzle.cell[coord[1] * 2][coord[0] * 2] != 'x'
coord
else
null
event2cell = (e) =>
pt = @svg.point e.clientX, e.clientY
[
Math.round pt.x
Math.round pt.y
]
event2corner = (e) =>
pt = @svg.point e.clientX, e.clientY
[
0.5 + Math.round pt.x - 0.5
0.5 + Math.round pt.y - 0.5
]
@svg.on 'pointermove', (e) =>
e.preventDefault()
edge = event2coord e
if edge?
@highlight
.transform
rotate: 45
translate: edge
.opacity 0.333
if e.buttons and @last? # drag behavior
unless (
switch @last.state
when true # path
cell = event2cell e
cell[0] in [Math.floor(@last.edge[0]),
Math.ceil(@last.edge[0])] and
cell[1] in [Math.floor(@last.edge[1]),
Math.ceil(@last.edge[1])]
when false # wall
corner = event2corner e
corner[0] in [0.5 + Math.floor(@last.edge[0] - 0.5),
0.5 + Math.ceil(@last.edge[0])] and
corner[1] in [0.5 + Math.floor(@last.edge[1] - 0.5),
0.5 + Math.ceil(@last.edge[1] - 0.5)]
when undefined # erase
false # always
)
@toggle edge, @last.state
else
@highlight.opacity 0
@svg.on 'pointerleave', (e) =>
@highlight.opacity 0
@last = undefined
@svg.on 'pointerdown', (e) =>
e.preventDefault()
e.target.setPointerCapture e.pointerId
edge = event2coord e
return unless edge?
@toggle edge,
switch e.button
when 0 # left click
null # => cycle through path, wall, empty
when 1 # middle click
false # => wall
when 2 # right click
undefined # => empty
when 5 # pen eraser
undefined # => empty
else
null
for ignore in ['click', 'contextmenu', 'auxclick', 'dragstart', 'touchmove']
@svg.on ignore, (e) -> e.preventDefault()
toggle: (...args) ->
for copy in @linked ? [@]
copy.toggleSelf ...args
toggleSelf: (edge, state) ->
if @lines[edge]?
@lines[edge].remove()
delete @lines[edge]
dir = edge2dir edge
if state == null
@puzzle.edges[edge] =
switch @puzzle.edges[edge]
when undefined
true
when true
false
when false
undefined
else
@puzzle.edges[edge] = state
if @puzzle.edges[edge] == false and
not document.getElementById('walls').checked
@puzzle.edges[edge] = undefined
@last =
edge: edge
state: @puzzle.edges[edge]
if @puzzle.edges[edge]?
if @puzzle.edges[edge] == false
dir = perp dir
p = sub edge, dir
q = add edge, dir
@lines[edge] = @userGroup.line p..., q...
.addClass if @puzzle.edges[edge] then 'path' else 'wall'
@drawErrors()
if @puzzle.checkSolved()
@svg.addClass 'solved'
else
@svg.removeClass 'solved'
fontGUI = ->
app = new FontWebappHTML
root: '#output'
sizeSlider: '#size'
sizeName: 'size'
charWidth: 225
charPadding: 5
charKern: 0
lineKern: 22.5
spaceWidth: 112.5
shouldRender: (changed) ->
changed.text
renderChar: (char, state, parent) ->
char = char.toUpperCase()
letter = window.font[char]
return unless letter?
svg = SVG().addTo parent
box = new Player svg, new Puzzle letter
linkIdenticalChars: (glyphs) ->
glyph.linked = glyphs for glyph in glyphs
document.getElementById('reset').addEventListener 'click', ->
app.render()
document.getElementById('nohud')?.addEventListener 'click', ->
app.furls.set 'hud', false
window?.onload = ->
if document.getElementById 'output'
fontGUI()
module?.exports = {Puzzle}