2D阵列 - 错误的“墙”放置?

时间:2016-12-23 20:32:27

标签: 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 =墙

Xposition = 25; Yposition = 25;

宽度= 5; height = 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 =墙

Xposition = 20; Yposition = 25;

宽度= 10; height = 5;

正如你所看到的那样,对齐正好了。墙壁位于房间本身以外的其他位置....奇怪的是,这只发生在值(xpos,ypos,width和height)彼此不同时。

--- --- UPDATE

当我反转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

坐标和尺寸与上面的示例相同。

我的代码中存在问题?通常墙应该在房间周围......

2 个答案:

答案 0 :(得分:1)

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

dungeon[yPos][xPos] = 1;

应该是

dungeon[xPos][yPos] = 1;

答案 1 :(得分:0)

我终于得到了解决方案,我的Wall Algorythm被打破了,我写了一个新的:

  // Vertical Walls 
  for(int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomHeigth; y++){

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

      dungeon[y][ (int) roomCornerDownLeft.x + roomWidth ] = 2;

  } 

  // Horizontal Walls
  for(int x = (int) roomCornerDownLeft.x; x <= roomCornerDownLeft.x + roomWidth; x++){

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

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

  }