康威的生命游戏新价值观

时间:2015-11-08 06:01:58

标签: java conways-game-of-life

问题围绕康威的生命游戏以及如何为新一代同时实施所有规则。游戏遵循新一代的三个规则,其中:正好有三个活着的邻居的死细胞变为活着,一个活着的邻居的活细胞变得死亡,并且具有三个以上活着的邻居的活细胞变得死亡。原始一代是随机的。我认为我的问题,即我的新一代人一次一个地实施规则而不是一次实施规则,就是这种方法:

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

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

1 个答案:

答案 0 :(得分:1)

您的输出对我来说很好。注意,你实际上做了&#34;环绕&#34;边框,所以这个

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;
}