康威的生命游戏新价值观

Conway's Game of Life New Values

问题围绕康威的生命游戏以及如何为新一代同时执行所有规则。游戏遵循新世代的三个规则,即:恰好有三个活邻居的死细胞变为活细胞,恰好有一个活邻居的活细胞变为死细胞,具有三个以上活邻居的活细胞变为死细胞。原始生成是随机的。我认为我的问题是我的新一代一次一个地执行规则而不是一次执行所有规则,在这种方法中:

public static int[][] nextgeneration(int[][] lastgen){
    int[][] nextgen = new int[lastgen.length][lastgen[0].length];
    for(int i = 0; i < lastgen.length; i++){
        for(int j = 0; j < lastgen[i].length; j++){
             if(aliveneighbors(lastgen, i, j) == 3){
                nextgen[i][j] = 1;
            }
        else if(aliveneighbors(lastgen, i, j) == 1){
                nextgen[i][j] = 0;
            }
        else if(aliveneighbors(lastgen, i, j) >  3){
                nextgen[i][j] = 0;
            }         
        else    nextgen[i][j] = lastgen[i][j];
        }
    }
    return nextgen;

这是我的完整代码,以防问题不在该方法中:

import java.util.Random;
public class Life {
public static int[][] origin(int a, int b) {
    int[][] randomMatrix = new int [a][b];
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            Random random = new Random();
            int abc = random.nextInt(2);
        randomMatrix[i][j] = abc;
        }
    }
    return randomMatrix;
    }
public static void print(int[][] a) {
    for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a.length; j++){
             System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
}
public static void show(int[][] b) {
    int N = b.length;
    StdDraw.setXscale(0, N-1);
    StdDraw.setYscale(0, N-1);
    for(int i = 0; i < b.length; i++){
        for(int j = 0; j < b.length; j++){
            if(b[i][j] == 1){
                StdDraw.setPenColor(StdDraw.RED);
                StdDraw.filledSquare(j, N-i-1, .5);

            }
            else if(b[i][j] == 0){
                StdDraw.setPenColor(StdDraw.BLACK);
                StdDraw.filledSquare((double)j, (double)-i, .5);
            }
        }
    }
}
public static int[][] nextgeneration(int[][] lastgen){
    int[][] nextgen = new int[lastgen.length][lastgen[0].length];
    for(int i = 0; i < lastgen.length; i++){
        for(int j = 0; j < lastgen[i].length; j++){
             if(aliveneighbors(lastgen, i, j) == 3){
                nextgen[i][j] = 1;
            }
        else if(aliveneighbors(lastgen, i, j) == 1){
                nextgen[i][j] = 0;
            }
        else if(aliveneighbors(lastgen, i, j) >  3){
                nextgen[i][j] = 0;
            }         
        else    nextgen[i][j] = lastgen[i][j];
        }
    }
    return nextgen;
}
public static int aliveneighbors(int[][] board, int x, int y){
    int count = 0;
    int up;
    int down;
    int left;
    int right;
    {
    if(x > 0)   
        up = x - 1;
    else
        up = board.length - 1;

    if(x < (board.length - 1))
        down = x + 1;
    else
        down = 0;

    if(y > 0) 
        left = y - 1;
    else
        left = board[x].length - 1;

    if(y < (board[x].length - 1))
        right = y + 1;
    else
        right = 0;
    //Count the live neighbors
    if(board[up][left] == 1)
        count++;

    if(board[up][y] == 1)
        count++;

    if(board[up][right] == 1)
        count++;

    if(board[x][left] == 1)
        count++;

    if(board[x][right] == 1)
        count++;

    if(board[down][left] == 1)
        count++;

    if(board[down][y] == 1)
        count++;

    if(board[down][right] == 1)
        count++;

    return count;
}
}
public static void main(String[] args) {
    int[][] b = origin(5, 5);
    int gens = 5;
    for (int i = 0; i < gens; i++) {
        System.out.println();
        int nextboard[][] = nextgeneration(b);
        b = nextboard; //I feel like this could be a problem as well
        System.out.println("Generation " + i + ":");
        print(nextgeneration(b));
        show(nextgeneration(b)); //This line of code seems useless
        //print(b); This one also seems useless and makes output confusing
        show(b);
    }
}
}

这是我的输出:

Generation 0:
0 1 1 0 0 
0 1 1 0 0 
0 0 0 1 1 
1 1 0 1 1 
1 0 0 0 0 

Generation 1:
1 0 1 0 0 
1 1 0 0 0 
0 0 0 0 0 
0 1 1 1 0 
0 0 0 1 0 

Generation 2:
1 0 1 0 1 
1 1 0 0 0 
1 0 0 0 0 
0 0 1 1 0 
0 0 0 1 1 

Generation 3:
0 0 1 0 0 
0 0 0 0 0 
1 0 1 0 1 
0 0 1 1 0 
1 1 0 0 0 

Generation 4:
0 1 0 0 0 
0 1 0 1 0 
0 1 1 0 1 
0 0 1 1 0 
0 1 0 1 0 

我期待这样的事情:

Generation 0:
0 1 1 0 0 
0 1 1 0 0 
0 0 0 1 1 
1 1 0 1 1 
1 0 0 0 0 

Generation 1:
0 1 1 0 0 
0 1 0 0 0 
1 0 0 0 1 
1 1 1 1 1 
1 1 0 0 0 

Generation 2:
0 1 1 0 0 
1 1 1 0 0 
1 0 0 0 1 
0 0 1 1 1 
1 0 0 1 0

另外,在我的游戏动画中,活细胞在动画中保持存活,这不应该发生。这不是我的主要问题,但如果你知道如何解决它也会有所帮助。

我觉得你的输出没问题。请注意,您实际上对边界进行了“环绕”,所以这个

Generation 0:
0 1 1 0 0 

上边框为:

1 0 0 0 0 

和左边框:

0
0
1
1
0

计算结果如下:

0 1 0 0 0 0 
0 0 1 1 0 0 
0 0 1 1 0 0 

所以这个输出:

Generation 1:
1 0 1 0 0 
1 1 0 0 0 

环绕式是正确的。 然而,从预期结果来看,您似乎希望将其视为实际边框。我的意思是:

010

000

x=1,y=0,只有 5 个邻居。

在那种情况下你需要这样的东西:

    public static int aliveneighbors(int[][] board, int x, int y){
    int width = board.length;
    int height = board[0].length;
    int count = 0;
     
     
    boolean isNotLower =  (y-1) >= 0;
    boolean isNotUpper = (y+1) < height;
        
    if (x-1 >= 0) {
        
       if( isNotLower && (board[x-1][y-1] == 1) )
        count++;
       if(board[x-1][y] == 1)
        count++;
       if(isNotUpper && (board[x-1][y+1] == 1) )
        count++;            
    }
    
    if (x+1 < width) {
       if( isNotLower && (board[x+1][y-1] == 1) )
        count++;
       if(board[x+1][y] == 1)
        count++;
       if( isNotUpper && (board[x+1][y+1] == 1) )
        count++;            
    }
    
    if( isNotUpper && (board[x][y+1] == 1) )
        count++;
    if(isNotLower && (board[x][y-1] == 1) )
        count++;
        
     return count;
}