2015-11-08 109 views
2

這個問題圍繞着康威的人生遊戲以及如何在新一代同時實施所有規則。遊戲遵循三代人的規則,即:具有三個活的鄰居的死亡細胞變爲活的,具有一個活的鄰居的活細胞變爲死亡,並且具有三個以上活的鄰居的活細胞變得死亡。原始的一代是隨機的。我想我的問題,這是我的新世代都在同一時間,而不是一次全部執行規則之一,是這種方法:康威的人生遊戲新價值

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 

同樣在我的遊戲動畫中,活動細胞在動畫中保持活躍狀態​​,這不應該發生。這不是我的主要問題,但如果你知道如何解決這個問題,那也是有幫助的。

+0

「我想我的問題是這種方法:」 - 什麼問題?你沒有描述你預期會發生什麼以及發生了什麼。 –

+1

謝謝,我累了,沒有注意到。 –

回答

1

你的輸出對我來說看起來很好。注意,你實際上做的「環繞式」的邊界,所以這

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 

正確的環繞。 然而,從預期的結果看,你想把它當作實際的邊界。我的意思是:

,其中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; 
} 
+1

非常感謝你!這正是我需要的。我真的沒有注意到我正在對邊界進行全面整理,但我確實知道新一代的輸出並不是完全隨機的,因爲它們有時是有意義的。 –