简体   繁体   English

2D 元胞自动机故障

[英]2D Cellular Automata Glitch

I'd like to make it so that my code generates a cave system in a game.我想让它让我的代码在游戏中生成一个洞穴系统。 I'm having trouble with the code;我的代码有问题; it seems that it makes meshes in the parts inbetween the caves and I don't want a mesh, I want an open area.似乎它在洞穴之间的部分制作了网格,我不想要网格,我想要一个开放区域。 Here is my code:这是我的代码:

package CATest;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CATest extends JPanel{
    static CATest w;
    static Random rand=new Random();
    JFrame jf=new JFrame();
    boolean tileMap[][];
    public static void main(String argsp[]){
        w=new CATest();
        w.jf.add(w);
        w.jf.setSize(100,400);
        w.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.jf.setVisible(true);
        w.tileMap=new boolean[100][400];
        for(int x=0;x<w.tileMap.length;x++)
            for(int y=0;y<w.tileMap[0].length;y++)
                if(rand.nextFloat()<0.7)
                    w.tileMap[x][y]=true;
        for(int step=0;step<7;step++){
            boolean newMap[][]=new boolean[100][400];
            for(int x=0;x<w.tileMap.length;x++)
                for(int y=0;y<w.tileMap[0].length;y++){
                    int count=0;
                    if(x-1>=0&&y-1>=0&&x+1<w.tileMap.length&&y+1<w.tileMap[0].length){
                        if(w.tileMap[x-1][y])
                            count++;
                        if(w.tileMap[x+1][y])
                            count++;
                        if(w.tileMap[x][y-1])
                            count++;
                        if(w.tileMap[x][y+1])
                            count++;
                        newMap[x][y]=count<=3;
                    }
                }
            w.tileMap=newMap;
        }
        w.repaint();
    }
    @Override
    public void paintComponent(Graphics g){
        for(int x=0;x<tileMap.length;x++)
            for(int y=0;y<tileMap[0].length;y++){
                if(tileMap[x][y])
                    g.setColor(Color.black);
                else g.setColor(Color.white);
                g.drawRect(x,y,1,1);
            }
    }
}

Here's a picture of what happens: cellular automata As you can see, there is a mesh where there should be a cave.这是发生了什么的图片:元胞自动机如您所见,应该有一个洞穴的地方有一个网格。 Please, help.请帮忙。

I omitted the fact that the center tile needed to be accounted for.我忽略了需要考虑中心图块的事实。 So instead of所以而不是

if(w.tileMap[x-1][y])
    count++;
if(w.tileMap[x+1][y])
    count++;
if(w.tileMap[x][y-1])
    count++;
if(w.tileMap[x][y+1])
    count++;

it should be它应该是

if(w.tileMap[x][y])
    count++;
if(w.tileMap[x-1][y])
    count++;
if(w.tileMap[x+1][y])
    count++;
if(w.tileMap[x][y-1])
    count++;
if(w.tileMap[x][y+1])
    count++;

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

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