forked from foolswood/Hexagongame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.es
43 lines (42 loc) · 1.25 KB
/
solver.es
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
var solver = function(start_state, moves_func) {
ways_to = {}
var traverse = function(state, route) {
var moves = moves_func(state)
var next, next_json
for (var m=0; m<moves.length; m++) {
next = moves[m]
next_json = JSON.stringify(next)
if (ways_to[next_json] === undefined) {
ways_to[next_json] = [route]
var new_route = route.slice(0)
new_route.push(next)
traverse(next, new_route)
} else {
ways_to[next_json].push(route)
}
}
}
traverse(start_state, [start_state])
return ways_to
}
get_std_moves_func = function(iface) {
var hexes = {}
var hex
for (var h=0; h<iface.hexes.length; h++) {
hex = iface.hexes[h]
hexes[hex.position] = hex
}
return function(state) {
var divs = hexes[state[0]].dividers
var col = hexes[state[0]].colour
var valid = []
var divCol
for (var d=0; d<divs.length; d++) {
divCol = divs[d][1].colour
if (state[1] == "w" || divCol == "w" || divCol == state[1]) {
valid.push([divs[d][0], col])
}
}
return valid
}
}