private HexWalker GetNextExteriorSpot(HexWalker h) { Hex currentSpot = h.currentSpot; Hex currentBoard = h.currentBoard; List <Hex> neighbors = currentSpot.Neighbors(); // First, push forward the current board piece. bool adjustBoardTile = true; while (adjustBoardTile) { int direction = h.clockwise ? 1 : -1; Hex nplus1 = neighbors[Mod((neighbors.IndexOf(currentBoard) + (1 * direction)), 6)]; Hex nplus2 = neighbors[Mod((neighbors.IndexOf(currentBoard) + (2 * direction)), 6)]; if (!pieces.ContainsKey(nplus1) && !pieces.ContainsKey(nplus2)) { // Path is clear - dont adjust the board tile adjustBoardTile = false; currentSpot = nplus1; } else { // This is too tight an area! currentBoard = pieces.ContainsKey(nplus2) ? nplus2 : nplus1; } } return(new HexWalker(currentSpot, currentBoard, h.clockwise)); }
private void ShowStepSpots(int steps, Tile selectedTile) { Hex currentSpot = selectedTile.Hex; Hex firstSpot = currentSpot; List <Hex> allNeighborSpots = currentSpot.Neighbors(); bool[] boardNeighbors = { false, false, false, false, false, false }; for (int i = 0; i < 6; i++) { boardNeighbors[i] = pieces.ContainsKey(allNeighborSpots[i]); if (boardNeighbors[i] && !boardNeighbors[Mod((i - 1), 6)]) { if (i == 5 && boardNeighbors[0]) { return; // Then we have a loop } // Found a spot to start highlighting from Debug.Log("i:" + i + " o:" + firstSpot + " b:" + allNeighborSpots[i]); HexWalker rwalker = new HexWalker(firstSpot, allNeighborSpots[i], true); HexWalker lwalker = new HexWalker(firstSpot, allNeighborSpots[i], false); for (int j = 0; j < steps; j++) { // Debug.Log(currentSpot); rwalker = GetNextExteriorSpot(rwalker); lwalker = GetNextExteriorSpot(lwalker); } ShowSpot(rwalker.currentSpot); ShowSpot(lwalker.currentSpot); } } }
public void CalculateDistancesBetweenTiles() { StringInputReader reader = new StringInputReader(); HexWalker walker = new HexWalker(reader); Assert.Equal(3, walker.ShortestDistanceFromStart("ne,ne,ne")); Assert.Equal(0, walker.ShortestDistanceFromStart("ne,ne,sw,sw")); Assert.Equal(2, walker.ShortestDistanceFromStart("ne,ne,s,s")); Assert.Equal(3, walker.ShortestDistanceFromStart("se,sw,se,sw,sw")); }
private void ShowExteriorSpots(Tile selectedTile) { // Get the first on the outside path. Hex mostRemoteHex = Hex.LongestHex(BoardHexes()); List <Hex> outerRing = Ring(mostRemoteHex.Length() + 1); List <Hex> neighbors = mostRemoteHex.Neighbors(); Hex firstSpot = outerRing.Intersect(neighbors).First(); HexWalker walker = new HexWalker(firstSpot, mostRemoteHex, true); do { // Debug.Log(currentSpot); ShowSpot(walker.currentSpot); walker = GetNextExteriorSpot(walker); } while (!walker.currentSpot.Equals(firstSpot)); }
static void Main(string[] args) { /** * I used the following extremely well-written guide by Amit Patel (Red Blob Games) * to understand how a hexogonal grid can be represented and how one traverses it: * * https://www.redblobgames.com/grids/hexagons * * It's one of the most nicely-presented resources on game development that I've * ever seen! */ FileInputReader reader = new FileInputReader(); HexWalker walker = new HexWalker(reader); // Part one Console.WriteLine(walker.ShortestDistanceFromStart("Inputs/day-11.txt")); // Part two Console.WriteLine(walker.FarthestDistanceFromStart("Inputs/day-11.txt")); }