HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Iterating over Dungeon Map

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mapoveriteratingdungeon

Problem

I'm working on an RPG so of course I have to have some dungeons. I've got the dungeon generation working, with the output being a DungeonTileType[][].

Now I wanted to create the code for the player to be able to enter the dungeon. To do so, I created a Dungeon class that will hold all of the information necessary for the game to work.

Here it is:

public class Dungeon {

    private final Player player;
    private final DungeonTileType[][] dungeonTiles;

    private MapPoint startPoint;
    private MapPoint endPoint;

    public Dungeon(Player player, DungeonTileType[][] dungeonTiles) {
        this.player = player;
        this.dungeonTiles = dungeonTiles;

        for (int x = 0; x < dungeonTiles.length; x++) {
            DungeonTileType[] row = dungeonTiles[x];
            for (int y = 0; y < row.length; y++) {
                DungeonTileType type = row[y];
                if (type == DungeonTileType.START) {
                    this.startPoint = new MapPoint(x, y);
                } else if (type == DungeonTileType.END) {
                    this.endPoint = new MapPoint(x, y);
                }
            }
        }

        this.player.setDungeonStartPosition(this.startPoint);
    }

    public DungeonTileType[][] getTiles() {
        return this.dungeonTiles;
    }

    public Player getPlayer() {
        return this.player;
    }

    public MapPoint getStartPoint() {
        return this.startPoint;
    }

    public MapPoint getEndPoint() {
        return this.endPoint;
    }
}


I'm using a maze generating algorithm as the first step for the dungeon creation, so I can make use of the start point of the maze to position the player correctly once he enters the dungeon. I could also use the end point to create stairs to another level at some point.

What I really don't like is that I cannot use for : in in this situation, because I need to have the X and Y coordinates of a given tile. One solution would be to have a DungeonTile class th

Solution

I'm working on an RPG so of course I have to have some dungeons. I've got the dungeon generation working, with the output being a DungeonTileType[][].

Well there's your problem. To simplify eliminate the search, you should return something like this:

class Dungeon {
    DungeonTileType[][] tiles;
    MapPoint start;
    MapPoint end;
}


Think about it, you currently have some code that generates a map that you then search through the entire map to find out where you placed that one exit and that one start tile.

When you place the start and end tiles, you should save the position that you placed them at.

As for general two dimensional looping, I wrote a Iterator for looping through 2D arrays a while ago that you can use if you'd like.

Code Snippets

class Dungeon {
    DungeonTileType[][] tiles;
    MapPoint start;
    MapPoint end;
}

Context

StackExchange Code Review Q#105765, answer score: 8

Revisions (0)

No revisions yet.