java - 二维数组 - 错误的 "Wall "放置?

标签 java arrays algorithm multidimensional-array libgdx

晚上好,圣诞快乐!

我想制作自己的 Dungeon-Generator。所以我决定首先在一些空房间中生成,并在同一步骤中放置墙壁。

public void generateRoom(byte[][] dungeon){

    if(roomCornerDownLeft.x + roomWidth < dungeon[0].length && roomCornerDownLeft.y + roomHeigth  < dungeon.length ){


        setRoomFloor(dungeon);

        setWalls(dungeon);


    }

}


public void setRoomFloor(byte[][] dungeon){


    origin = new Vector2(roomCornerDownLeft.x + roomWidth / 2, roomCornerDownLeft.y + roomHeigth / 2);  


    roomHeigth += 1;   // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles height

    roomWidth += 1;    // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles width


    roomCornerUpRight = new Vector2(roomCornerDownLeft.x + roomWidth, roomCornerDownLeft.y + roomHeigth);


    for(int yPos = (int) roomCornerDownLeft.y; yPos <= roomCornerUpRight.y; yPos++){

        for(int xPos = (int) roomCornerDownLeft.x; xPos <= roomCornerUpRight.x ; xPos++){

            dungeon[yPos][xPos] = 1;

        }

    }



}


public void setWalls(byte[][] dungeon){

  // Vertical walls
  for (int i = (int) roomCornerDownLeft.x; i <= roomCornerDownLeft.x + roomHeigth; i++) {

        dungeon[i][(int) roomCornerDownLeft.y] = 2; // North wall

        dungeon[i][(int) roomCornerDownLeft.y + (roomWidth )] = 2; // South wall

  }

  // horizontal walls
    for (int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomWidth; y++) {

        dungeon[(int) roomCornerDownLeft.x][y] = 2; // North wall

        dungeon[(int) (roomCornerDownLeft.x + ( roomHeigth ))][y] = 2; // South wall

    }

}

但是我用它解决了一些奇怪的问题。放置“房间地板”没有任何问题。位置和大小刚刚好。但是当我试图在它周围建一堵墙时,它有时会起作用。这是一个例子:

    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000

1 = 地板 2 = 墙

X位置= 25; Y位置= 25;

宽度 = 5; 高度 = 5;

它工作正常吧?所以现在让我们尝试使用其他一些值:

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000222222222222111110000000000000
        00000000000000000000200001111112111110000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

1 = 地板 2 = 墙

X位置= 20; Y位置= 25;

宽度 = 10; 高度 = 5;

如您所见,对齐不再正确了。墙壁位于与房间本身完全不同的位置……奇怪的是,只有当值(xpos、ypos、width 和 height)彼此不同时才会发生这种情况。

---更新---

当我反转 x 轴和 y 轴时,我得到这个:

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

坐标和大小与上面的示例相同。

我的代码哪里出了问题?通常墙壁应该在房间周围......

最佳答案

setRoomFloor 方法的内部 for 循环中,您反转了 x 轴上的位置和 y 轴上的位置。

dungeon[yPos][xPos] = 1;

应该是

dungeon[xPos][yPos] = 1;

关于java - 二维数组 - 错误的 "Wall "放置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307531/

相关文章:

arrays - 如何在Kotlin中使ByteArray保持不变

JavaScript:插入对象内的数组?

python - 复杂度与运行时间的实际增长不匹配? (Python)

c++ - OpenCV>>相机相对位姿估计

algorithm - 寻找所有回文子串的动态规划

java - 如何查看 OpenJML 错误消息?

java - java中使用jsoup提取数据

java - java客户端/服务器应用程序没有输出

java - Spring 和 Jackson Json : serialising two different sets of fields

php - mysql中数组的数据类型