2015-09-22 64 views
-3

在我的java編程程序不能正常運行。我的教授讓我們下載一個console.java來幫助使用控制檯來顯示程序的輸出。我認爲當我調用更新方法和checkCells時程序停滯不前。我的教授說,爲行和列調用matrix.length是不正確的,但如果我不使用matrix.length,我很難調用列。生活的遊戲,java對象

任何輸入讚賞

import java.util.*; 

public class Life { 

    private int birthLow = 0; 
    private int birthHigh = 0; 
    private int liveLow = 0; 
    private int liveHigh = 0; 
    private boolean[][] matrix; 

    public Life(long seed, int rows, int cols, int birthLow2, int birthHigh2, int liveLow2, int liveHigh2) { 

     boolean initalMatrix[][] = new boolean[rows][cols]; 

     seedArray(initalMatrix, rows, cols, seed); 

     birthLow = birthLow2; 
     birthHigh = birthHigh2; 
     liveLow = liveLow2; 
     liveHigh = liveHigh2; 
     matrix = initalMatrix; 

     if ((rows < 1) && (cols < 1)) { 

      throw new IllegalArgumentException("Rows must be positive, not " + rows); 
     } 
     if ((rows > 9) && (cols < 9)) { 

      throw new IllegalArgumentException("Rows and cols cant go over 9, not " + rows + cols); 
     } 
     if (birthLow < 1 || (birthHigh > 9) || (liveLow < 1) || (liveHigh > 9)) { 

      throw new IllegalArgumentException("birth rates can not be below 1 or above 9 " + birthLow + birthHigh); 
     } 

    } 

    public boolean[][] world() { 
     boolean[][] matrixClone = matrix.clone(); 
     for (int row = 0; row < matrix.length; row++) { 
      matrixClone[row] = matrix[row].clone(); 
     } 
     return matrixClone; 
    } 

    public void update() { 
     matrix = checkCells(matrix, matrix.length, matrix.length, birthLow, birthHigh, liveLow, liveHigh); 
    } 

    public static void seedArray(boolean[][] matrix, int rows, int cols, long seed) { 
     // generates a random seed to fill the matrix 
     Random s = new Random(seed); 

     for (int r = 1; r < rows - 1; r++) { 
      for (int c = 1; c < cols - 1; c++) { 

       boolean x = s.nextBoolean(); 
       matrix[r][c] = x; 
      } 
     } 
    } 

    public static void printBoard(boolean[][] matrix, int rows, int cols) { 
     for (int r = 0; r < rows; r++) { 
      for (int c = 0; c < cols; c++) { 

       if (matrix[r][c] == false && c == 0) { 
        System.out.print("- "); 
       } else if (matrix[r][c] == false && c > 0) { 
        System.out.print("- "); 
       } else if (matrix[r][c] == true) { 
        System.out.print("# "); 
       } 
      } 
      System.out.println(); 

     } 
     System.out.println(); 
    } 

    public static boolean[][] checkCells(boolean[][] matrix, int rows, int cols, int birthLow, int birthHigh, 
      int liveLow, int liveHigh) { 
     // clones matrix board 
     boolean[][] matrixClone = matrix.clone(); 
     for (int row = 0; row < matrix.length; row++) { 
      matrixClone[row] = matrix[row].clone(); 
     } 
     // determines if the living cell is going to live or die 
     for (int r = 1; r < rows; r++) { 
      for (int c = 1; c < cols; c++) { 
       if (neighbors(matrixClone, r, c) < liveLow || neighbors(matrixClone, r, c) > liveHigh || c == 0 
         || r == 0 || c == cols - 1 || r == rows - 1) { 
        matrix[r][c] = false; 
       } else if (neighbors(matrixClone, r, c) >= birthLow && neighbors(matrixClone, r, c) <= birthHigh) { 
        matrix[r][c] = true; 

       } 
      } 

     } 
     return matrixClone; 

    } 

    public static int neighbors(boolean[][] matrixClone, int r, int c) { 
     int neighbors = 0; 
     // checks all neighbors for life or death 
     for (int rn = (r - 1); rn <= (r + 1); rn++) { 
      for (int cn = (c - 1); cn <= (c + 1); cn++) { 
       try { 
        if (matrixClone[rn][cn] == true) { 
         neighbors++; 
        } 
        // catches the array if it checks out the perimeter 
       } catch (ArrayIndexOutOfBoundsException f) { 
        continue; 
       } 
      } 
     } 
     return neighbors; 
    } 

} 
+4

這100行代碼的相關性是什麼?它們與你所問的有什麼關係?你究竟在問什麼? –

+1

「如何調用列」?我不知道這意味着什麼。 – duffymo

+0

當我在方法更新中調用checkCells時,我使用了matrix.length兩次來填充數組的行和列。教授說,使用matrix.length兩次是不正確的。如何在更新方法中傳遞行和列? –

回答

0

不要使用matrix.length的列。你已經知道了。原因是它給你的行數。相反,只需保存rowcol的值並使用它們。

這樣做的一種方法是讓它們成爲字段,並在構造函數中初始化它們。