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
|
namespace(function() {
function isCellBridgePathFriendly(puzzle, color, pos) {
if (pos.x%2 === 0 && pos.y%2 === 0) return false
var cell = puzzle.getCell(pos.x, pos.y)
return cell == null || cell.color == null || cell.color === color
}
function makeMinimalTree(graph, root, required) {
var seen = Array(graph.length).fill(false)
var result = Array(graph.length).fill(false)
result[root] = true
function dfs(node) {
seen[node] = true
result[node] = required[node]
for (var child of graph[node]) {
if (!seen[child]) {
dfs(child)
result[node] = result[node] || result[child]
}
}
}
dfs(root)
return result
}
function isTreeUnique(graph, isInTree) {
var seen = isInTree.slice()
function dfs(node) {
seen[node] = true
var reachableTreeNode = null
for (var child of graph[node]) {
var candidate = null
if (isInTree[child]) {
candidate = child
} else if (!seen[child]) {
candidate = dfs(child)
}
if (candidate != null && candidate !== reachableTreeNode) {
if (reachableTreeNode == null) {
reachableTreeNode = candidate
} else {
return -1
}
}
}
return reachableTreeNode
}
for (var i = 0; i < graph.length; i++) {
if (!seen[i]) {
if (dfs(i) === -1) return false
}
}
return true
}
function puzzleCellsAdjacent(first, second, pillar) {
if (pillar && first.y == second.y && Math.abs(second.x - first.x) === puzzle.width - 1)
return true
return Math.abs(second.x - first.x) + Math.abs(second.y - first.y) === 1
}
function bridgeTest(region, puzzle, color, bridges) {
var nodes = region.cells.filter(pos => isCellBridgePathFriendly(puzzle, color, pos))
var graph = Array.from(Array(nodes.length), () => [])
for (var ir = 1; ir < nodes.length; ir++) {
var right = nodes[ir]
for (var il = 0; il < ir; il++) {
var left = nodes[il]
if (puzzleCellsAdjacent(left, right, puzzle.pillar)) {
graph[il].push(ir)
graph[ir].push(il)
}
}
}
var isBridge = nodes.map(node => bridges.some(bridge => node.x === bridge.x && node.y === bridge.y))
var isInTree = makeMinimalTree(graph, isBridge.indexOf(true), isBridge)
for (var i = 0; i < nodes.length; i++) {
if (isBridge[i] && !isInTree[i]) return false
}
return isTreeUnique(graph, isInTree)
}
window.validateBridges = function(puzzle, region, regionData) {
var bridges = {}
for (var pos of region) {
var cell = puzzle.getCell(pos.x, pos.y)
if (cell == null) continue
// Count color-based elements
if (cell.color != null) {
if (cell.type === 'bridge') {
if (bridges[cell.color] == null) {
bridges[cell.color] = []
}
bridges[cell.color].push(pos)
}
}
}
for (var color in bridges) {
var total = 0
var discardable = 0
for (var x=1; x < puzzle.width; x+=2) {
for (var y=1; y < puzzle.height; y+=2) {
var cell = puzzle.getCell(x, y)
if (cell != null) {
if (cell.type === 'bridge' && cell.color === color) total++
if (cell.type === 'nega') discardable++
}
}
}
if (bridges[color].length != total) {
if (bridges[color].length >= total - discardable) {
// TODO: Negations in other regions can validate the solution
for (var bridge of bridges[color]) {
regionData.addInvalid(bridge)
}
} else {
for (var bridge of bridges[color]) {
regionData.addVeryInvalid(bridge)
}
}
} else if (!window.bridgeTest(region, puzzle, color, bridges[color])) {
for (var bridge of bridges[color]) {
regionData.addInvalid(bridge)
}
}
}
}
var DIRECTIONS = [
{'x': 0, 'y':-1},
{'x': 1, 'y':-1},
{'x': 1, 'y': 0},
{'x': 1, 'y': 1},
{'x': 0, 'y': 1},
{'x':-1, 'y': 1},
{'x':-1, 'y': 0},
{'x':-1, 'y':-1},
]
window.validateArrows = function(puzzle, region, regionData) {
for (var pos of region) {
var cell = puzzle.getCell(pos.x, pos.y)
if (cell == null) continue
if (cell.type != 'arrow') continue
dir = DIRECTIONS[cell.rot]
var count = 0
var x = pos.x + dir.x
var y = pos.y + dir.y
for (var i=0; i<100; i++) { // 100 is arbitrary, it's just here to avoid infinite loops.
var line = puzzle.getLine(x, y)
console.spam('Testing', x, y, 'for arrow at', pos.x, pos.y, 'found', line)
if (line == null && (x%2 !== 1 || y%2 !== 1)) break
if (line > window.LINE_NONE) count++
if (count > cell.count) break
x += dir.x * 2
y += dir.y * 2
if (puzzle.matchesSymmetricalPos(x, y, pos.x + dir.x, pos.y + dir.y)) break // Pillar exit condition (in case of looping)
}
if (count !== cell.count) {
console.log('Arrow at', pos.x, pos.y, 'crosses', count, 'lines, but should cross', cell.count)
regionData.addInvalid(pos)
}
}
}
window.validateSizers = function(puzzle, region, regionData) {
var sizers = []
var regionSize = 0
for (var pos of region) {
if (pos.x%2 === 1 && pos.y%2 === 1) regionSize++ // Only count cells for the region
var cell = puzzle.getCell(pos.x, pos.y)
if (cell == null) continue
if (cell.type == 'sizer') sizers.push(pos)
}
console.debug('Found', sizers.length, 'sizers')
if (sizers.length == 0) return // No sizers -- no impact on sizer validity
var sizerCount = regionSize / sizers.length
if (sizerCount % 1 != 0) {
console.log('Region size', regionSize, 'is not a multiple of # sizers', sizers.length)
for (var sizer of sizers) {
regionData.addInvalid(sizer)
}
return
}
if (puzzle.sizerCount == null) puzzle.sizerCount = sizerCount // No other sizes have been defined
if (puzzle.sizerCount != sizerCount) {
console.log('sizerCount', sizerCount, 'does not match puzzle sizerCount', puzzle.sizerCount)
for (var sizer of sizers) {
regionData.addInvalid(sizer)
}
}
}
})
|