public bool GetSuccessors(AStarSearch astarsearch, PuzzleState parentState) { SticksNode parent_node = parentState as SticksNode; Coordinate pcoord = parent_node == null ? new Coordinate(-1,-1) : parent_node._coord; Coordinate ncoord; // push each possible move except allowing the search to go backwards int[] xx = { 0, 1, 1, 0, -1, -1 }; int[] yy = { 1, 0, -1, -1, 0, 1 }; for (int i = 0; i < xx.Length; i++) { ncoord = _coord.Xform(xx[i], yy[i]); if (pcoord != ncoord && _map.ContainsKey(ncoord) && GetCost(ncoord) < 9) { astarsearch.AddSuccessor(new SticksNode(_map, ncoord, _unit)); } } return true; }
// This generates the successors to the given Node. It uses a helper function called // AddSuccessor to give the successors to the AStar class. The A* specific initialisation // is done for each node internally, so here you just set the state information that // is specific to the application public bool GetSuccessors(AStarSearch astarsearch, PuzzleState parentState) { MapSearchNode parent_node = parentState as MapSearchNode; int parent_x = -1; int parent_y = -1; if (parent_node != null) { parent_x = (int)parent_node.x; parent_y = (int)parent_node.y; } MapSearchNode NewNode; // push each possible move except allowing the search to go backwards if ((GetMap(x - 1, y) < 9) && !((parent_x == x - 1) && (parent_y == y)) ) { NewNode = new MapSearchNode(x - 1, y); astarsearch.AddSuccessor(NewNode); } if ((GetMap(x, y - 1) < 9) && !((parent_x == x) && (parent_y == y - 1)) ) { NewNode = new MapSearchNode(x, y - 1); astarsearch.AddSuccessor(NewNode); } if ((GetMap(x + 1, y) < 9) && !((parent_x == x + 1) && (parent_y == y)) ) { NewNode = new MapSearchNode(x + 1, y); astarsearch.AddSuccessor(NewNode); } if ((GetMap(x, y + 1) < 9) && !((parent_x == x) && (parent_y == y + 1)) ) { NewNode = new MapSearchNode(x, y + 1); astarsearch.AddSuccessor(NewNode); } return true; }
// This generates the successors to the given Node. It uses a helper function called // AddSuccessor to give the successors to the AStar class. The A* specific initialisation // is done for each node internally, so here you just set the state information that // is specific to the application public void FindSuccessors(AStarSearch curSearch, GridPosition parent_node, bool allowDiagonal) { Vec3di pos_parent = new Vec3di(-1, -1, -1); if (parent_node != null) { pos_parent = parent_node.pos; } Vec3di PosUpLeft = new Vec3di(pos.x - 1, pos.y + 1, pos.z); Vec3di PosUpRight = new Vec3di(pos.x + 1, pos.y + 1, pos.z); Vec3di PosDownLeft = new Vec3di(pos.x - 1, pos.y - 1, pos.z); Vec3di PosDownRight = new Vec3di(pos.x + 1, pos.y - 1, pos.z); Vec3di PosUp = new Vec3di(pos.x, pos.y + 1, pos.z); Vec3di PosLeft = new Vec3di(pos.x - 1, pos.y, pos.z); Vec3di PosRight = new Vec3di(pos.x + 1, pos.y, pos.z); Vec3di PosDown = new Vec3di(pos.x, pos.y - 1, pos.z); Vec3di PosAbove = new Vec3di(pos.x, pos.y, pos.z + 1); Vec3di PosBelow = new Vec3di(pos.x, pos.y, pos.z - 1); if (allowDiagonal) { if ((grid.GetAt(PosUpLeft) != grid.BorderCode) && PosUpLeft.Compare(pos_parent) == false) { // we have pos UpLeft, now check if at lest Up or left are maze if (grid.GetAt(PosLeft) != grid.BorderCode || grid.GetAt(PosUp) != grid.BorderCode) { curSearch.AddSuccessor(new GridPosition(grid, PosUpLeft)); } } if ((grid.GetAt(PosUpRight) != grid.BorderCode) && PosUpRight.Compare(pos_parent) == false) { // we have pos UpLeft, now check if at lest Up or left are maze if (grid.GetAt(PosRight) != grid.BorderCode || grid.GetAt(PosUp) != grid.BorderCode) { curSearch.AddSuccessor(new GridPosition(grid, PosUpRight)); } } if ((grid.GetAt(PosDownLeft) != grid.BorderCode) && PosDownLeft.Compare(pos_parent) == false) { // we have pos UpLeft, now check if at lest Up or left are maze if (grid.GetAt(PosLeft) != grid.BorderCode || grid.GetAt(PosDown) != grid.BorderCode) { curSearch.AddSuccessor(new GridPosition(grid, PosDownLeft)); } } if ((grid.GetAt(PosDownRight) != grid.BorderCode) && PosDownRight.Compare(pos_parent) == false) { // we have pos UpLeft, now check if at lest Up or left are maze if (grid.GetAt(PosRight) != grid.BorderCode || grid.GetAt(PosDown) != grid.BorderCode) { curSearch.AddSuccessor(new GridPosition(grid, PosDownRight)); } } } // push each possible move except allowing the search to go backwards if ((grid.GetAt(PosLeft) != grid.BorderCode) && PosLeft.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosLeft)); } if ((grid.GetAt(PosDown) != grid.BorderCode) && PosDown.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosDown)); } if ((grid.GetAt(PosRight) != grid.BorderCode) && PosRight.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosRight)); } if ((grid.GetAt(PosUp) != grid.BorderCode) && PosUp.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosUp)); } if ((grid.GetAt(PosAbove) != grid.BorderCode) && PosAbove.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosAbove)); } if ((grid.GetAt(PosBelow) != grid.BorderCode) && PosBelow.Compare(pos_parent) == false) { curSearch.AddSuccessor(new GridPosition(grid, PosBelow)); } }