2

To preface, I'll elaborate a bit more about what the current project is. I'm working on a game that puts the player in a room-based dungeon. Rooms are entered/exited through doors.

Here is the generator() method I have for my floor creator.

The turn(int, int, int) method takes in a direction (randomly created number 0 - 3), and two starting locations, and x and y coordinate, and returns an array that contains the previous numbers, but depending on the direction, one was modified by increment/decrements of one. For example, if the direction was 0, that would mean up, and the yCoordinate would be subtracted by 1. In the array, returned, the first value is he X, the second is the Y.

    int roomTag = 1; //tags the room as created

    int currentX = startLocation; //the column that navigates the map
    int currentY = startLocation;

    map[currentY][currentX] = roomTag;

    int rooms = 0;

    while(rooms < maxRooms) {
        int direction = randomRange(0, 4);

        currentX = turn(direction, currentX, currentY)[0];
        currentY = turn(direction, currentX, currentY)[1];
        if(currentX > 0 && currentY > 0  && currentX < maxSize && currentY < maxSize) {
            if(map[currentY][currentX] != roomTag) {
                map[currentY][currentX] = roomTag;
                roomList[currentY][currentX] = new NormalRoom(this, currentX, currentY, rooms);
                rooms++;
            }
        } else {
            currentX = startLocation;
            currentY = startLocation;
        }
    }

    roomList[startLocation][startLocation] = new StartRoom(this, startLocation, startLocation, 0);

As of now, the procedural generation works great. It generates the dungeon differently each time, and all rooms are connected horizontally or vertically to at least one other room.

Rooms simply hold the information about the doors, location, and enemies, if applicable. Rooms have a method for searching for which doors to create. Here is the method that is called upon instantiation of the room. It is called first to get the amount of doors that are to be created, then sets the boolean created to true, then runs the method again to create/add the doors to the array.

    int doorIndex = doorCount - 1;
    int[][] tempMap = floor.getMap();

    if(yLocation > 0) {
        for(int i = 0; i < tempMap.length; i++) {
            for(int j = 0; j < tempMap.length; j++) {
                if(i == tempMap.length/2 && j == tempMap.length/2) System.out.print("() ");
                else if(tempMap[j][i] == 1) System.out.print("[] ");
                else System.out.print("    ");
            }
            System.out.println(" ");
        }
        if(tempMap[yLocation - 1][xLocation] == 1) {
            System.out.println("UP DOOR IN: (" + yLocation + ", " + xLocation + ")");
            if(created) {
                door[doorIndex] = new Door(0);
                doorIndex--;
            } else {
                doorCount++;
            }
        }
    }
    if(yLocation < floor.getMap().length - 1) {
        if(tempMap[yLocation + 1][xLocation] == 1) {
            System.out.println("DOWN DOOR IN: (" + yLocation + ", " + xLocation + ")");
            if(created) {
                door[doorIndex] = new Door(2);
                doorIndex--;
            } else {
                doorCount++;
            }
        }
    }
    if(xLocation < floor.getMap().length - 1) {
        if(tempMap[yLocation][xLocation + 1] == 1) {
            System.out.println("RIGHT DOOR IN: (" + yLocation + ", " + xLocation + ")");
            if(created) {
                door[doorIndex] = new Door(1);
                doorIndex--;
            } else {
                doorCount++;
            }
        }
    }
    if(xLocation > 0) {
        if(tempMap[yLocation][xLocation - 1] == 1) {
            System.out.println("LEFT DOOR IN: (" + yLocation + ", " + xLocation + ")");
            if(created) {
                door[doorIndex] = new Door(3);
            } else {
                doorCount++;
            }
        }
    }

    if(created) {
        for(int i = 0; i < door.length; i++) {
            door[i].instantiate();
        }
    }

The problem seems to be that it never creates left doors, and rarely creates up doors. I cannot seem to find out why it is doing this. I have been stuck on this for awhile now and cannot seem to find what is causing it, as I cannot find any consistencies in the issue.

I leaned on the side of posting less code, but if more is needed, let me know.

Thanks in advance.

Edit: I may have found the solution. Given the fact the method is called during instantiation of the rooms, it cannot find the other rooms as they don't exist yet. The rooms can only create doors for rooms that were created before it. If they were created after, the map would't have them listed as existing yet.

I will try to amend the problem with this in mind.

EDIT 2: This was the problem. Creating the rooms after creating the map fixed it. I simply made it so the room creator was separate from the map creator, then I created the rooms based on the map.

jlars789
  • 155
  • 9
  • Are you sure your if statements are correct? Your ylocation seems to make up and right doors, and your xlocation if statements make left and down doors, seems unusual to me, as I expect y to map to up/down, and X to left/right – Ferrybig Jan 14 '19 at 17:27
  • @Ferrybig I had them print the wrong things, sorry. In the door object creation, 0 = Up, 1 = Right, 2 = Down, 3 = Left – jlars789 Jan 14 '19 at 17:33

1 Answers1

1

The problem was that I was instantiating the rooms as I was creating them, and part of the rooms' instantiation was finding the amount of and the location of the doors. The problem with this is that I was asking the algorithm to find rooms that weren't created yet.

To fix this, I used the map I created earlier, that was a 2DArray, with the rooms marked with 1s, and everything else marked with a 0. After the map was fully created, I iterated through the map, and put a Room in the coordinates marked with a 1 in a separate 2DArray that contained Room objects.

The doors now accurately lead to new rooms as they are supposed to.

jlars789
  • 155
  • 9