public bool IsValidPosition(HexCoord coord) { if (!coord.IsValidCoordinate() || 0 > coord.x || coord.x >= width || 0 > coord.y || coord.y >= height) { return(false); } return(openTiles[coord.x][coord.y]); }
public static void Main() { HexBoard board = new HexBoard(10, 10); foreach (var c in board.PerformBFS(HexCoord.CreateXY(5, 5), 3, _ => true, _ => true)) { Console.WriteLine(c); } }
/// <summary> /// Perform a BFS on the grid. /// </summary> /// <param name="start">where to start</param> /// <param name="startingEnergy">how much energy to start with</param> /// <param name="canPassThrough">are you allowed to pass through this tile?</param> /// <param name="canLandOn">are you allowed to finish on this tile? must imply passability</param> /// <returns>dudes that you can land on</returns> public IEnumerable <Tuple <HexCoord, int> > PerformBFS(HexCoord start, int startingEnergy, Predicate <HexCoord> canPassThrough, Predicate <HexCoord> canLandOn) { var toVisit = new Queue <Tuple <HexCoord, int> >(); var visited = new HashSet <HexCoord>(); toVisit.Enqueue(Tuple.Create(start, startingEnergy)); while (toVisit.Count > 0) { var pair = toVisit.Dequeue(); var coord = pair.Item1; var energyLeft = pair.Item2; visited.Add(coord); if (energyLeft < 0) { continue; } if (IsValidPosition(coord) && canPassThrough(coord)) { if (canLandOn(coord)) { yield return(Tuple.Create(coord, energyLeft)); } int newEnergy = energyLeft - 1; foreach (var n in coord.GetNeighbors().Where(c => !visited.Contains(c))) { toVisit.Enqueue(Tuple.Create(n, newEnergy)); } HexCoord wormholeEndpoint; if (wormholes.TryGetValue(coord, out wormholeEndpoint)) { toVisit.Enqueue(Tuple.Create(wormholeEndpoint, newEnergy)); } } } }
public void DeleteWormholePair(HexCoord a, HexCoord b) { wormholes.Remove(a); wormholes.Remove(b); }
public void CreateWormholePair(HexCoord a, HexCoord b) { wormholes[a] = b; wormholes[b] = a; }
public int DistanceFrom(HexCoord other) { return((Math.Abs(x - other.x) + Math.Abs(y - other.y) + Math.Abs(z - other.z)) / 2); }