Java 如何在程序生成的地牢中找到房间中的门数量?

Java 如何在程序生成的地牢中找到房间中的门数量?,java,procedural-generation,Java,Procedural Generation,在前言中,我将详细阐述一下当前项目是什么。我正在做一个游戏,把玩家放在一个基于房间的地牢里。房间是通过门进出的 下面是我为地板创建者提供的generator()方法 turn(int,int,int)方法接受一个方向(随机创建的数字0-3)、两个起始位置以及x和y坐标,并返回一个包含前面数字的数组,但根据方向,其中一个被一的增量/减量修改。例如,如果方向为0,则表示向上,Y坐标将减去1。在返回的数组中,第一个值是X,第二个值是Y int roomTag = 1; //tags the r

在前言中,我将详细阐述一下当前项目是什么。我正在做一个游戏,把玩家放在一个基于房间的地牢里。房间是通过门进出的

下面是我为地板创建者提供的generator()方法

turn(int,int,int)方法接受一个方向(随机创建的数字0-3)、两个起始位置以及x和y坐标,并返回一个包含前面数字的数组,但根据方向,其中一个被一的增量/减量修改。例如,如果方向为0,则表示向上,Y坐标将减去1。在返回的数组中,第一个值是X,第二个值是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);
int-roomTag=1//将房间标记为已创建
int currentX=零位//导航地图的列
int currentY=零位;
map[currentY][currentX]=roomTag;
内部房间=0;
while(房间0&¤tY>0&¤tX
到目前为止,程序生成的效果非常好。它每次生成的地下城都不同,所有房间都与至少一个其他房间水平或垂直连接

房间只保存有关门、位置和敌人的信息(如果适用)。房间具有搜索要创建的门的方法。下面是在房间实例化时调用的方法。首先调用它以获取要创建的门的数量,然后将创建的布尔值设置为true,然后再次运行该方法以创建门/将门添加到数组中

    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();
        }
    }
intdoorindex=doorCount-1;
int[]tempMap=floor.getMap();
如果(位置>0){
对于(int i=0;i0){
if(tempMap[yLocation][xLocation-1]==1){
System.out.println(“左门输入:(“+Y位置+”,“+X位置+”));
如果(已创建){
门[门索引]=新门(3);
}否则{
门数++;
}
}
}
如果(已创建){
对于(int i=0;i
问题似乎是它从来不会创建左门,也很少创建向上门。我似乎不知道它为什么这样做。我已经在这个问题上纠缠了一段时间,似乎找不到导致它的原因,因为我在这个问题上找不到任何一致性

我倾向于发布更少的代码,但如果需要更多,请告诉我

提前谢谢

编辑:我可能已经找到了解决方案。鉴于该方法是在房间实例化期间调用的,它无法找到其他房间,因为它们还不存在。房间只能为在此之前创建的房间创建门。如果它们是在之后创建的,地图将不会将它们列为现有

我会考虑到这一点,设法修正这个问题


编辑2:这就是问题所在。创建地图后创建房间修复了它。我只是将房间创建者与地图创建者分开,然后根据地图创建房间

问题是,我在创建房间时实例化了房间,房间实例化的一部分是查找门的数量和位置。问题是,我要求算法查找尚未创建的房间

为了解决这个问题,我使用了我之前创建的地图,它是一个2DArray,房间用1s标记,其他的都用0标记。在完全创建地图之后,我遍历了地图,并将一个房间放在一个单独的二维数组中,该数组包含房间对象,其中的坐标标记为1


门现在准确地指向了新房间,正如它们应该的那样。

您确定您的if语句正确吗