2013-03-06 5 views
-4

나는 Conway의 삶의 게임을 쓰려고합니다. 지금 나는 살아있는 세포를 플러스 기호로 나타내고 죽은 것들은 마이너스 기호로 나타낸다. 내 첫 세대가 괜찮아 보인다면 한계를 벗어난다. 또한 나는 그들이 인쇄되고있는 사용자에 대한 생성 카운터를 추가하고 싶습니다. 여기에 오류 코드 여기Conway 생명의 게임

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' 예외는 어디에서 볼 수 있습니까? 문제가 무엇인지 알아 내기 위해 무엇을 시도 했습니까? 또한, 두 번째 질문에 관한 한, 어떤 해결책을 시도 했습니까? –

+1

코드에서 사람들에게 오류를 발견하도록 요청하는 것은 특히 생산성이 높지 않습니다. 디버거를 사용하거나 인쇄 문을 추가하여 프로그램의 진행 상황을 추적하고 발생할 것으로 예상되는 것과 비교하여 문제를 격리해야합니다. 이 둘이 갈라지면 문제를 발견했습니다. (필요한 경우, [최소 시험 경우] (http://sscce.org)를 구성해야합니다. –

+0

* 어디에서 * 예외가 있습니까? 우리가 추측하거나 프로젝트를 만들어 실행 시키거나 ...? –

답변

3

당신이 좌표가 (i, j)가 범위를 벗어하지 있는지 확인하기 위해 사용하는 inbounds 방법은 잘못된 것입니다 :

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

사실, 마지막 체크에, 당신은 world[0].lengthi을 비교하고, 때 j해야한다 :

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

예. 내가 찾던 오류를 해결했습니다. 나는 그것이 단지 couldnt하는 변수 중의 1 개가 어느 것을 발견하는 것처럼 보인다고 상상했다. – user2092325

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; 

.

관련 문제