Iterating over Dungeon Map 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 &lt; dungeonTiles.length; x++) {
            DungeonTileType[] row = dungeonTiles[x];
            for (int y = 0; y &lt; 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 that keeps track of both the type and the position, but I like the simplicity of the matrix, especially when I go to serialize things. I also thought about a list of lists, because then I could use indexOf(), but again I lose the simplicity of the matrix in that situation.  

I would love to get some ideas for alternative approaches.
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.