JAVA - Conway 的生命游戏删除所有活细胞?

标签 java

我正在为一个学校项目开发康威的生命游戏。我不是直接寻找代码。我想找出我的代码有什么问题。

在康威的生命游戏中,如果一个细胞有 3 个存活的邻居,它就会从死亡状态变为存活状态。如果它有两个或三个活着的邻居,它就会保持活力。如果这些都不是真的,它就死了。

我的 LifeView 类有一个方法可以显示细胞模拟,然后显示给定点周围有多少活细胞。

这是我得到的输出:

How many rows is your simulation?
5
How many columns is your simulation?
5
How many generations is your simulation?
3
xxxxx
xxxxx
xx0xx
xx0xx
xx0xx

00000
01110
02120
03230
02120


xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

00000
00000
00000
00000
00000


xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

00000
00000
00000
00000
00000

这是错误的,因为第二代活细胞应该是穿过第一代活细胞中心的水平线。不是穿过那个中心,而是所有细胞都死了。我很困惑为什么它不起作用。

主类:

package gameOfLife;

import java.util.Scanner;

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Scanner numReader = new Scanner(System.in);
    System.out.println("How many rows is your simulation?");
    int rows = numReader.nextInt();
    System.out.println("How many columns is your simulation?");
    int columns = numReader.nextInt();
    System.out.println("How many generations is your simulation?");
    int generations = numReader.nextInt();


    LifeModel model = new LifeModel(rows,columns);
    LifeView life = new LifeView(model);

    for(int i=0; i<generations; i++)
    {
         life.displayLife();
         model.nextGeneration();
    }

}

LifeView 类:

package gameOfLife;

import java.util.Scanner;

public class LifeView {

private LifeModel model;

public LifeView(LifeModel model)
{
    this.model = model;
}


public void displayLife()
{
    for(int i=0; i < model.getWorld().length; i++)
    {
        for(int j=0; j < model.getWorld()[0].length; j++)
        {

            if(model.getWorld()[i][j])
            {
                System.out.print("0");
            }
            else
            {
                System.out.print("x");
            }
        }
        System.out.println("");
    }
    System.out.println("");

    for(int i=0; i < model.getWorld().length; i++)
    {
        for(int j=0; j < model.getWorld()[0].length; j++)
        {

            System.out.print(model.numLivingNeighbors(i,j));
        }
        System.out.println("");
    }
    System.out.println("");
    System.out.println("");
}
}

生命模型类: 包gameOfLife;

public class LifeModel
{
private boolean[][] world;
private int numRows;
private int numCols;
private boolean[][] tempWorld;



public LifeModel(int rows, int cols)
{
    this.numRows=rows;
    this.numCols=cols;
    world = new boolean[rows][cols];
    initWorld();
    tempWorld = world;
}

private void initWorld()
{

    boolean done = false;

    while(!done)
    {
        int i = (int) (Math.random()*numRows);
        int j = (int) (Math.random()*numCols);
        if(j>0 && i>0 && i<numRows-1 && j<numCols-1)
        {
            /*
             world[i-1][j-1] = true;
             world[i-1][j] = true;
             world[i-1][j+1] = true;
             world[i][j+1] = true;
             world[i+1][j] = true;
             */
             world[i][j]=true;
             world[i+1][j]=true;
             world[i-1][j]=true;
             done = true;
        }
    }


}

public void nextGeneration()
{
    //tempWorld = new boolean[numRows+2][numCols+2];

    int rows = world.length;
    int columns = world[0].length;

    for(int i=0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            toggleCell(i,j);
        }
    }
    world = tempWorld;
}

public void toggleCell(int r, int c)
{
    int count = numLivingNeighbors(r,c);
    if(!world[r][c] && count==3)
    {
        tempWorld[r][c] = true;
    }
    else if(world[r][c] && (count>=2 && count<=3))
    {
        tempWorld[r][c] = true;
    }
    else
    {
        tempWorld[r][c] = false;
    }
}

public int numLivingNeighbors(int r, int c)
{
    int count = 0;
    boolean newCells[][] = world;
    for(int i = -1; i<=1; i++)
    {
        for(int j = -1; j<=1; j++)
        {
            if(i!=0 || j!=0)
            {
                int row = r + i;
                int column = c + j;
                if(row>=0 && row < newCells.length && column>=0 && column<newCells[0].length && newCells[row][column])
                {
                    count++;
                }
            }
        }
    }
    return count;
}

public void userChange()
{

}

public boolean[][] getWorld()
{
    return world;
}


}

非常感谢任何帮助!

最佳答案

您的 LifeModel 类只有几个小问题。

在您的构造函数中,您将 tempWorld 设置为引用与实际游戏世界相同的数组。这将导致对 tempWorld 的任何修改也会影响 gameWorld。

public LifeModel(int rows, int cols)
{
    this.numRows=rows;
    this.numCols=cols;
    world = new boolean[rows][cols];
    initWorld();
    //tempWorld = world;  // You can remove this line.
}

然后在下一代中你有一行“//tempWorld = new boolean[numRows+2][numCols+2];”注释掉了。您确实需要在这里创建一个新的临时数组,这样您就不会在阅读时更改游戏板。但是,我不确定 +2 应该是什么,所以我删除了它。你应该:

public void nextGeneration()
{
    tempWorld = new boolean[numRows][numCols]; // Keep it the same size

    int rows = world.length;
    int columns = world[0].length;

    for(int i=0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            toggleCell(i,j);
        }
    }
    world = tempWorld;
}

在我进行这些更改后,它对我来说非常有效。我在下面包含了我在我的机器上使用的完整 LifeModel 类。

package gameOfLife;

public class LifeModel
{
private boolean[][] world;
private int numRows;
private int numCols;
private boolean[][] tempWorld;



public LifeModel(int rows, int cols)
{
    this.numRows=rows;
    this.numCols=cols;
    world = new boolean[rows][cols];
    initWorld();
}

private void initWorld()
{

    boolean done = false;

    while(!done)
    {
        int i = (int) (Math.random()*numRows);
        int j = (int) (Math.random()*numCols);
        if(j>0 && i>0 && i<numRows-1 && j<numCols-1)
        {
            /*
             world[i-1][j-1] = true;
             world[i-1][j] = true;
             world[i-1][j+1] = true;
             world[i][j+1] = true;
             world[i+1][j] = true;
             */
             world[i][j]=true;
             world[i+1][j]=true;
             world[i-1][j]=true;
             done = true;
        }
    }


}

public void nextGeneration()
{
    tempWorld = new boolean[numRows][numCols];

    int rows = world.length;
    int columns = world[0].length;

    for(int i=0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            toggleCell(i,j);
        }
    }
    world = tempWorld;
}

public void toggleCell(int r, int c)
{
    int count = numLivingNeighbors(r,c);
    if(!world[r][c] && count==3)
    {
        tempWorld[r][c] = true;
    }
    else if(world[r][c] && (count>=2 && count<=3))
    {
        tempWorld[r][c] = true;
    }
    else
    {
        tempWorld[r][c] = false;
    }
}

public int numLivingNeighbors(int r, int c)
{
    int count = 0;
    boolean newCells[][] = world;
    for(int i = -1; i<=1; i++)
    {
        for(int j = -1; j<=1; j++)
        {
            if(i!=0 || j!=0)
            {
                int row = r + i;
                int column = c + j;
                if(row>=0 && row < newCells.length && column>=0 && column<newCells[0].length && newCells[row][column])
                {
                    count++;
                }
            }
        }
    }
    return count;
}

public void userChange()
{

}

public boolean[][] getWorld()
{
    return world;
}


}

关于JAVA - Conway 的生命游戏删除所有活细胞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627284/

相关文章:

java - android 中不同的 jar 有何意义?

java - 使用 removeif 过滤包含对象的列表

java - 我可以从哪些地方了解更多有关 API 的信息?

java - 带有空格的类路径,用于在 Windows 上使用 Runtime.getRuntime().exec() 启动 java 进程

java - 如何判断InputStream中是否包含JSON数据?

Java 正则表达式找不到我的 Char 类

Java 摘要式身份验证 POST XML

java - 在请求范围内使用工厂方法 Autowiring

Java 简单二十一点游戏,java.lang.NullPointerException

java - 非线程安全标准集合的安全发布