Exploring Dungeons


Hey there, it's me again! I finished the procedural map generator for dungeons and a fog of war system, now I want to be able to explore these dungeons. To start off, I created a method called EnterDungeon which takes a Character as a parameter and places them in the dungeon, setting their position to the dungeon entrance. The player can move around the dungeon using the WASD keys on their keyboard.

Adding movement seemed pretty easy, so I wanted to add in some money and treasure to pick up. I used the $ symbol for the money, with dark yellow for copper, white for silver, and yellow for gold. Adding money and allowing it to be collected was pretty easy too, so I added treasure. I used the symbol for the treasure. I didn't want the treasure to be too easy to get, so I got ChatGPT to create a method to find the farthest empty tile from the entrance. This is the method:

public static Point FindFurthest(Tile[,] map, int startX, int startY, Tile target) 
{
     int width = map.GetLength(0);
     int height = map.GetLength(1);
     int maxDistance = 0;
     int furthestX = startX, furthestY = startY;
     var visited = new bool[width, height];
     var queue = new Queue<Tuple<int, int>>();
     queue.Enqueue(new Tuple<int, int>(startX, startY));
     visited[startX, startY] = true;
     int[] dx = { -1, 0, 1, 0 };
     int[] dy = { 0, 1, 0, -1 };
     while (queue.Count > 0)
     {
         var current = queue.Dequeue();
         int x = current.Item1, y = current.Item2;
         int distance = Math.Abs(x - startX) + Math.Abs(y - startY);
         if (distance > maxDistance)
         {
             maxDistance = distance;
             furthestX = x;
             furthestY = y;
         }
         for (int i = 0; i < 4; i++)
         {
             int nx = x + dx[i];
             int ny = y + dy[i];
             if (nx >= 0 && nx < width && ny >= 0 && ny < height && !visited[nx, ny] && map[nx, ny] == target)
             {
                 queue.Enqueue(new Tuple<int, int>(nx, ny));
                 visited[nx, ny] = true;
             }
         }
     }
     return new Point(furthestX, furthestY); 
} 

This code defines a method called FindFurthest that searches for the furthest tile from a starting tile that matches a specified target tile. It takes in a two-dimensional array of tiles called map, a starting X and Y position on the map, and the target tile that we want to search for. It returns a Point that represents the furthest tile position found on the map.

The method uses a queue and a boolean array called visited to keep track of which tiles have already been searched. It starts by enqueuing the starting tile and marking it as visited.

Then, it enters a loop that dequeues a tile from the front of the queue and iterates over its neighboring tiles. It calculates the Manhattan distance from the current tile to the starting tile and checks if it's the furthest distance found so far. If it is, it updates the furthest tile found.

The loop then enqueues any neighboring tiles that have not been visited and match the target tile.

The algorithm continues to loop through the queue until it has searched every possible tile on the map that matches the target tile. Once it has finished, it returns the coordinates of the furthest tile found.

That worked out pretty good, I did not have to fix or change the code . All I needed now was a way to exit, For that I just check if the player position equals the entrance position and ask the player if they wish to leave. And that is it, short one this time, but dungeons are  mostly done, I just need random encounters and boss battles, but that will be another story.

Leave a comment

Log in with itch.io to leave a comment.