地图生成器返回奇怪的结果

时间:2016-05-15 00:59:14

标签: java procedural-generation

我正在用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;

}

我不知道它有什么问题。

1 个答案:

答案 0 :(得分:0)

我得到了它的工作

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;

}