【问题标题】:Map generator returning weird results地图生成器返回奇怪的结果
【发布时间】:2016-05-15 00:59:14
【问题描述】:

我正在为我的自上而下的射击游戏开发一个 Java 地图生成器,并且我正在实施类似于核王座所做的事情。它创造了一个“步行者” 贯穿地图创建地砖。它有可能会 移动一格后旋转 90、-90 和 180 度。

助行器应该一直运行,直到放置所需数量的地砖。

该函数有时会花费很长时间,并且它工作的时间会返回一个地图,该地图的地砖比预期的要少,并且始终具有相同的疏离模式。这是一个示例输出:

 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
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000010000000000000000000000
00000000000000000000000000011000000000000000000000
00000000000000000000000000001111000000000000000000
00000000000000000000000000000001100000000000000000
00000000000000000000000000000000100000000000000000
00000000000000000000000000000000110000000000000000
00000000000000000000000000000000011000000000000000
00000000000000000000000000000000001100000000000000
00000000000000000000000000000000000111000000000000
00000000000000000000000000000000000001110000000000
00000000000000000000000000000000000000010000000000
00000000000000000000000000000000000000011000000000
00000000000000000000000000000000000000001100000000
00000000000000000000000000000000000000000111000000
00000000000000000000000000000000000000000001000000
00000000000000000000000000000000000000000001100000
00000000000000000000000000000000000000000000100000
00000000000000000000000000000000000000000000110000
00000000000000000000000000000000000000000000011000
00000000000000000000000000000000000000000000001000
00000000000000000000000000000000000000000000001110
00000000000000000000000000000000000000000000000011  

1 是地砖。 0 什么都没有。

该地图只有 44 个地砖,并且使用 50 个所需地砖调用了 generateMap 函数。

这是我的代码:

public static int[][]generateMap(int floorTiles){
    int map[][] = new int[floorTiles][floorTiles];
    int currentX, currentY;
    for (int x = 0; x<floorTiles;x++){
        for (int y = 0; y<floorTiles;y++){
            map[x][y] = 0;
        }
    }
    boolean movingThroughX = true;
    boolean Forward = true;
    Random rand = new Random();
    int counter = 0;
    int decide = 0;
    currentX = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1);
    currentY = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1);
    while(counter < floorTiles){
        if(map[currentX][currentY] ==0){
            map[currentX][currentY] = 1;
            counter++;
        }

        //aply movement
        if(movingThroughX){
            if(Forward){
                if(currentX<floorTiles-1){
                    currentX++;
                }
            }else{
                if(currentX>floorTiles+1){
                    currentX--;
                }
            }
        }else{
            if(Forward){
                if(currentY<floorTiles-1){
                    currentY++;
                }
            }else{
                if(currentY>floorTiles+1){
                    currentY--;
                }
            }
        }

        decide = rand.nextInt(100);
        if(decide<20){
            //keep walking forward
        }
        else if(decide<45){
            //turn 90degres
            if(movingThroughX){
                if(Forward){
                    movingThroughX = false;
                    Forward = false;
                }else{
                    movingThroughX = false;
                    Forward = true;
                }
            }else{
                if(Forward){
                    movingThroughX = true;
                    Forward = true;
                }else{
                    movingThroughX = false;
                    Forward = true;
                }
            }
        }else if(decide<70){
            //turn -90degres
            if(movingThroughX){
                if(Forward){
                    movingThroughX=false;
                    Forward = true;
                }else{
                    movingThroughX = false;
                    Forward = false;
                }
            }else{
                if(Forward){
                    movingThroughX = true;
                    Forward = false;
                }else{
                    movingThroughX = true;
                    Forward = true;
                }
            }
        }else{
            //turn 180 degres
            Forward = !Forward;
        }


    }
    return map;

}

我不知道它有什么问题。

【问题讨论】:

  • 您对movingThroughX/Forward 使用相同的值两次。不应该有4个不同的案例吗?
  • 你是对的,但那是问题所在,因为我仍然得到了上位的赞助人,而且地砖比预期的要少
  • 不应该有一个movingThroughX和一个movingThroughY吗?我只看到 X。另外,我强烈建议不要用布尔值表示方向,而应该使用“dx”和“dy”整数来表示下一步的位置变化。打印这些,您将很容易看到它应该移动的方向。这将使您的代码更容易调试。
  • 当移动 throughX 为假时,您的移动 ThroughY 因为我无法进行对角线移动,因为这会使地图空间无法到达
  • 另外,您的“applyMovement”代码看起来不正确。你应该做 currentX += dx; currentY += dy(但是您选择表示 dx 和 dy)。然后你应该检查数组边界。例如。设置 currentX = min(max(0,currentX),floorTiles-1)

标签: java procedural-generation


【解决方案1】:

我搞定了

public static int[][]generateMap(int floorTiles){
    int map[][] = new int[floorTiles][floorTiles];
    int currentX, currentY;
    for (int x = 0; x<floorTiles;x++){
        for (int y = 0; y<floorTiles;y++){
            map[x][y] = 0;
        }
    }
    int dx, dy;
    dx = 0;
    dy= 1;
    Random rand = new Random();
    int counter = 0;
    int decide = 0;
    currentX = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1);
    currentY = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1);
    while(counter < floorTiles){

        //rotate
        decide = rand.nextInt(100);
        if(decide<25){

        }else if(decide<50){
            if(dx == 1){
                dy = -1;
                dx = 0;
            }else if(dx == -1){
                dy = 1;
                dx = 0;
            }else if(dy == 1){
                dx = 1;
                dy = 0;
            }else{
                dx = -1;
                dy = 0;
            }
        }else if(decide<75){
            if(dx == 1){
                dy = 1;
                dx = 0;
            }else if(dx == -1){
                dy = -1;
                dx = 0;
            }else if(dy == 1){
                dx = -1;
                dy = 0;
            }else{
                dx = 1;
                dy = 0;
            }
        }else{
            dx = -dx;
            dy = -dy;
        }


        //aply movement
        currentX += dx; 
        currentY += dy;
        currentX = Math.min(Math.max(0,currentX),floorTiles-1);
        currentY = Math.min(Math.max(0,currentY),floorTiles-1);


        //place tiles

        if(map[currentX][currentY] == 0){
            map[currentX][currentY] = 1;
            counter++;
        }


    }
    return map;

}

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 2012-08-08
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多