2013-03-06 61 views
-4

我想寫康威的人生遊戲。現在我用加號表示活細胞,用負號表示死細胞。我的第一代會顯得很好,然後我會遇到一個超出界限的錯誤。另外,我想在打印時爲用戶添加生成計數器。這裏是錯誤代碼康威生命遊戲

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 
at Life.Neighbors(Life.java:56) 
at Life.nextGen(Life.java:35) 
at Life.main(Life.java:18) 

這裏是代碼至今

import java.util.Scanner; 

public class Life { 

    public static boolean[][] gen(){ 
     boolean[][] matrix = new boolean[10][10]; 
     for(int i = 0; i < matrix.length; i++) 
      for(int j = 0; j < matrix.length; j++) 
       if(Math.random() > 0.7) 
        matrix[i][j] = true; 
     return matrix; 
    } 

    public static void main(String[] args){ 
     boolean[][] world = gen(); 
     show(world); 
     System.out.println(); 
     world = nextGen(world); 
     show(world); 
     Scanner s = new Scanner(System.in); 
     while(s.nextLine().length() == 0){ 
      System.out.println(); 
      world = nextGen(world); 
      show(world); 

     } 
    } 

    public static boolean[][] nextGen(boolean[][] world){ 
     boolean[][] newWorld 
      = new boolean[world.length][world[0].length]; 
     int num; 
     for(int i = 0; i < world.length; i++){ 
      for(int j = 0; j < world[0].length; j++){ 
       num = Neighbors(world, i, j); 
       if(occupiedNext(num, world[i][j])) 
        newWorld[i][j] = true; 
      } 
     } 
     return newWorld; 
    } 

    public static boolean occupiedNext(int Neighbors, boolean occupied){ 
     if(occupied && (Neighbors == 2 || Neighbors == 3)) 
      return true; 
     else if (!occupied && Neighbors == 3) 
      return true; 
     else 
      return false; 
    } 

    public static int Neighbors(boolean[][] world, int row, int col) { 
     int num = world[row][col] ? -1 : 0; 
     for(int i = row - 1; i <= row + 1; i++) 
      for(int j = col - 1; j <= col + 1; j++) 
       if(inbounds(world, i, j) && world[i][j]) 
        num++; 

     return num; 
    } 

    public static boolean inbounds(boolean[][] world, int i, int j) { 
     return i >= 0 && i < world.length && j >= 0 && 
     i < world[0].length; 
    } 
    public static void show(boolean[][] matrix){ 
     String s = ""; 
     for(boolean[] row : matrix){ 
      for(boolean val : row) 
       if(val) 
        s += "+"; 
       else 
        s += "-"; 
      s += "\n"; 
     } 
     System.out.println(s); 

     } 
    } 
+0

請發佈您遇到問題的特定代碼部分。你在哪裏看到'ArrayIndexOutOfBounds'異常?你自己嘗試過什麼,找出問題所在?另外,就您的第二個問題而言,您嘗試了哪些解決方案? – 2013-03-06 17:22:38

+1

要求人們發現代碼中的錯誤並不是特別有效。您應該使用調試器(或者添加打印語句)來分析問題,追蹤程序的進度,並將其與預期發生的情況進行比較。只要兩者發生分歧,那麼你就發現了你的問題。 (如果有必要的話,你應該構建一個[最小測試用例](http://sscce.org)。) – 2013-03-06 17:22:40

+0

*你在哪裏得到異常?你希望我們只是猜測,或者創建一個項目並運行它,或者......? – 2013-03-06 17:22:48

回答

3

inbounds方法,您正在使用,以確保座標(i,j)是不是出界就是錯誤的:

public static boolean inbounds(boolean[][] world, int i, int j) { 
    return i >= 0 && i < world.length && j >= 0 && i < world[0].length; 
} 

事實上,在最後一次檢查,你與world[0].length比較i,當它應該是j

public static boolean inbounds(boolean[][] world, int i, int j) { 
    return i >= 0 && i < world.length && j >= 0 && j < world[0].length; 
} 
+0

是的,它解決了我一直試圖找到它的錯誤。我認爲這是其中一個變量,似乎無法找到哪一個 – user2092325 2013-03-06 17:29:57

2

快速觀察,你寫道:

public static boolean inbounds(boolean[][] world, int i, int j) { 
     return i >= 0 && i < world.length && j >= 0 && 
     i < world[0].length; 

我想也許是最後的比較應該是:

... && j < world[0].length; 

,而不是我。