简体   繁体   English

随机生成算法无法正确生成

[英]Random spawn algorithm does not generate correctly

I have this code to spawn a 'tree' in my Java game; 我有这段代码可以在Java游戏中生成“树”。 it works and spawns a 'tree'. 它可以工作并生成一棵“树”。 I decided to make a random number generator up to 30 which would spawn that many 'trees'. 我决定制作一个最多30个随机数生成器,该生成器会生成许多“树”。 However, when I run my code, I get no error but the 'trees' don't spawn. 但是,当我运行代码时,没有任何错误,但是“树”没有生成。 The spawning algorithm can be found below. 生成算法可以在下面找到。

private void generateLevel() {
    int dungeonCoord = dungeonSpawn.nextInt(height * width);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            tiles[x + y * width] = Tile.GRASS.getId();
            tiles[dungeonCoord] = Tile.TUNNEL.getId();
             while(tCount < tRan)   {
                 System.out.println(tRan);
                 tCount ++;
                 int treeX = treeSpawn.nextInt(width * 5);
                 if(treeX < 256)    {
                     treeX = 256;
                }else   {
                    tiles[treeX] = Tile.LOG.getId();
                    tiles[treeX + width] = Tile.LOG.getId();
                    tiles[treeX + width + width] = Tile.LOG.getId();
                    tiles[treeX - width] = Tile.LEAVES.getId();
                    tiles[treeX - width] = Tile.LEAVES.getId();
                    tiles[treeX - width - width] = Tile.LEAVES.getId();
                    tiles[treeX - width - width + 1] = Tile.LEAVES.getId();
                    tiles[treeX - width - width - 1] = Tile.LEAVES.getId();
                    tiles[treeX - width + 1] = Tile.LEAVES.getId();
                    tiles[treeX - width + 2] = Tile.LEAVES.getId();
                    tiles[treeX - width - 1] = Tile.LEAVES.getId();
                    tiles[treeX - width - 2] = Tile.LEAVES.getId();
                    tiles[treeX + 1] = Tile.LEAVES.getId();
                    tiles[treeX - 1] = Tile.LEAVES.getId();
                    tiles[treeX - width - width - width] = Tile.LEAVES.getId(); 
                }
            }
        }
    }
}

How everything is declared: 如何声明所有内容:

private byte[] tiles;
public int width;
public int height;
public boolean generateTree = true;
Random treeSpawn = new Random();
Random dungeonSpawn = new Random();
Random numTrees = new Random();
int tCount = 0;
int tRan = numTrees.nextInt(30);

The treeSpawn boolean is for later on. treeSpawn布尔值将在以后提供。

This answer comes from what I can ascertain in the comments. 这个答案来自我在评论中可以确定的。

The code, as follows: 代码如下:

if(treeX < 256)    {
    treeX = 256;
} else   {

means that, if treeX is less than 256, your code doesn't even attempt to draw the tree. 意味着,如果treeX小于256,则您的代码甚至不会尝试绘制树。 In order to draw the tree, you need to remove the else (which is ignored when your if statement is evaluated to be true ) so your while loop looks as follows: 为了绘制树,您需要删除else (在if语句被评估为true时将被忽略),因此while循环如下所示:

while(tCount < tRan)   {
    System.out.println(tRan);
    tCount ++;
    int treeX = treeSpawn.nextInt(width * 5);

    if(treeX < 256)    {
        treeX = 256;
    }

    tiles[treeX] = Tile.LOG.getId();
    tiles[treeX + width] = Tile.LOG.getId();
    tiles[treeX + width + width] = Tile.LOG.getId();
    tiles[treeX - width] = Tile.LEAVES.getId();
    ... // Rest of the tree drawing code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM