Skip to content

Instantly share code, notes, and snippets.

@seanhagen
Created December 20, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhagen/d97867c66aa3377f438033398ed73109 to your computer and use it in GitHub Desktop.
Save seanhagen/d97867c66aa3377f438033398ed73109 to your computer and use it in GitHub Desktop.
Some code that's not working right
// this is the node thing for getting from the start position to the last key
type path struct {
name string
node *tile
steps int
parent *path
children []path
keysFound []string
final bool
mapNow grid
}
// traverse takes a root node ( the '@' node ) and creates a graph
// the graph won't be even or whatever the term is for each branch being the
// same length because it'll stop adding nodes whenever there are no 'gettable'
// keys left from where it ends up
func traverse(in path) path {
if in.final {
return in
}
tmpGrid := in.mapNow.getCopy() /// make a copy
keys := tmpGrid.getableKeys(in.node)
// for all those keys
for _, k := range keys {
ng := tmpGrid.getCopy() /// do it again to be safe -- apparently messing up
// because when we do a second loop for [e d] it's not getting the right map
d := ng.tileToTileSteps(in.node, k)
kn := k.key
kf := []string{kn}
if len(in.keysFound) > 0 {
kf = append(kf, in.keysFound...)
}
ng2 := ng.removeKey(kn) // another copy
keysLeft := ng2.keys()
gettableKeys := ng2.getableKeys(k)
k.key = ""
np := path{
name: kn,
node: k,
parent: &in,
steps: in.steps + d,
children: []path{},
mapNow: ng2.getCopy(), // do it again again, to be super safe?
final: len(keysLeft) <= 0,
keysFound: kf,
}
if len(keysLeft) > 0 && len(gettableKeys) > 0 {
np = traverse(np)
}
in.children = append(in.children, np)
}
return in
}
// should be creating a copy of the map/grid/whatever, but maybe isn't???
func (g grid) getCopy() grid {
out := make(grid)
for n, t := range g {
nei := map[string]edge{}
for nn, e := range t.neighbours {
nei[nn] = e
}
nt := &tile{
id: t.id,
key: t.key,
door: t.door,
coord: t.coord,
neighbours: nei,
}
out[n] = nt
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment