private Node evaluatePath(Board.direction d) { Board hold = new Board(data); if (hold.play(d) == false) { return(null); } Node n = new Node(this, hold, depth + 1); if (!history.ContainsKey(n.data)) { history.Add(n.data, n.depth); children.Add(n); association.Add(d, children.IndexOf(n)); } else { if (history[n.data] > n.depth) { history.Remove(n.data); history.Add(n.data, n.depth); } else { // return history[n.data]; return(null); } } return(n); }
override public bool move(Board.direction dir) { switch (dir) { case Board.direction.up: y += speed; break; case Board.direction.down: y -= speed; break; case Board.direction.left: x -= speed; break; case Board.direction.right: x += speed; break; default: return(false); } return(true); }
override public bool move(Board.direction dir) { switch (dir) { case Board.direction.up: // TODO: Remove this ++y; break; case Board.direction.down: --y; break; case Board.direction.left: --x; break; case Board.direction.right: ++x; break; case Board.direction.up2right: // and use the following y += 2; ++x; break; case Board.direction.up2left: y += 2; --x; break; case Board.direction.upRight2: ++y; x += 2; break; case Board.direction.upLeft2: ++y; x -= 2; break; case Board.direction.downLeft: --y; --x; break; case Board.direction.downRight: --y; ++x; break; default: return(false); } return(true); }
public Node getChildren(Board.direction dir) { if (isLeaf)//no children { return(null); } if (dir == Board.direction.up)//only one that won't have one { return(null); } if (!association.ContainsKey(dir)) { return(evaluatePath(dir)); } return(children[association[dir]]); }
public bool play(Board.direction dir) { foreach (Spider s in spiders) { s.move(dir); } foreach (Ant a in ants) { a.move(a.Face); } bool flag = !checkEvent(); if (!original && !flag) { return(flag); } updateGrid(); return(flag); }
public Stack <Board.direction> findSolutionDFS() //Call this, it probably won't work but it has before. I think the history dictionary is broken and it can only go out one way as a result. { Node n = head; n = DFS(n); Stack <Board.direction> s = new Stack <Board.direction>(); if (n != null) { Node p; int i; //loop up from solution node and collect the decisions while (n.Parent != null) { p = n.Parent; for (i = 0; i < p.getChildren().Count; ++i) { if (p.getChildren()[i] == n) { break; } } Board.direction d = Board.direction.up; foreach (KeyValuePair <Board.direction, int> dir in p.association) { if (dir.Value == i) { d = dir.Key; break; } } s.Push(d); n = n.Parent; } } return(s); }
public Ant(Board.direction dirFace) : base(dirFace) { }
public Ant(int setX, int setY, Board.direction dirFace) : base(setX, setY) { face = dirFace; }
///////////// // Methods // ///////////// abstract public bool move(Board.direction dir);
public Creature(Creature c) : this(c.x, c.y) { face = c.face; }
public Creature(Board.direction dirFace) { face = dirFace; }
public Stack <Board.direction> findSolutionBFS() { Array directions = Enum.GetValues(typeof(Board.direction)); Node n = head; bool solved = false; Queue <Node> current = new Queue <Node>(); current.Enqueue(n); Queue <Node> next = new Queue <Node>(); while (!solved && current.Count != 0) //loop down until a solution is found { foreach (Node nCur in current) { foreach (Board.direction d in directions) { Node nChild = nCur.getChildren(d); if (nChild != null) { if (nChild.IsLeaf) { if (nChild.isWinState()) { n = nChild; solved = true; break; } } next.Enqueue(nChild); } } if (solved) { break; } } current = next; next = new Queue <Node>(); } Stack <Board.direction> s = new Stack <Board.direction>(); if (solved) { Node p; int i; //loop up from solution node and collect the decisions while (n.Parent != null) { p = n.Parent; for (i = 0; i < p.getChildren().Count; ++i) { if (p.getChildren()[i] == n) { break; } } Board.direction d = Board.direction.up; foreach (KeyValuePair <Board.direction, int> dir in p.association) { if (dir.Value == i) { d = dir.Key; break; } } s.Push(d); n = n.Parent; } } return(s); }