索引越界和多次运行

时间:2014-02-24 18:39:10

标签: java arrays conways-game-of-life

我正在撰写康威的生命游戏计划,这让我几周感到非常沮丧。到目前为止,我已经编写了大部分代码,但仍然存在一些问题。我提前为众多问题道歉。

我的生命游戏程序以输入文本文件开头,该文件包含30 x 30行 - 和X. - 表示死细胞和X活着。

我现在的问题是:

  1. 当生命周围检查数组时,如何处理arrayoutofbounds异常?我想做的就是忽略这些价值观。
  2. 如何更改代码以针对gens变量的值多次运行?
  3. 为什么打印copygrid总是打印地址,即使我使用Arrays.toString?
  4. 谢谢,任何帮助都会受到赞赏,因为我很困惑并且没时间了。

    我的代码:

    package assignment2;
    
    /**
     *
     * @author jaw209
     */
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    public class Assignment2 {
    
        private static int livecount;
        static char[][] copygrid = {{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};        
    
    
        public static void main(String[] args) throws FileNotFoundException {
    
            String inputfile;
            String output;
            String generations;
    
    //Input file = L:\Java 2\Assignment2\Sample input.txt        
    
    //Inputs from file to char array        
            inputfile = JOptionPane.showInputDialog ("Where is the input file?   Ex: C:\\users\\public\\desktop\\input.txt ");
            Scanner input = new Scanner (new FileReader(inputfile)); 
            char[] chararray = new char[900];
            String allvalues = null;
    
            do {
                String values = input.next();
                allvalues = allvalues + values;
            }
            while(input.hasNextLine());
    
            chararray = allvalues.toCharArray();
    
    //Reads values in chararray into multi array       
            char[][] grid1 =  new char[30][30]; 
            int i = 4;
            for(int row = 0; row < 30; row++){
                for(int col = 0; col < 30; col++){
                    grid1 [row][col] = chararray[i]; 
                    i++;
                        System.out.print(grid1[row][col]);
                            }
                        System.out.println();
                        }
    
    //Finds how many generations should be calculated        
            generations = JOptionPane.showInputDialog ("How many generations should be calculated?");
            int gens = Integer.parseInt(generations);
    
    //Runs the cycle once      
            for (int row = 0; row < 30; row++){
                for (int col = 0; col < 30; col ++){
                    if (status(grid1[row][col])){
                        liveSurrounding(grid1, row, col);
                        moves(row, col);
                    }
                    else if (!status(grid1[row][col])){
                        //run 3 checker
                      if (liveSurrounding(grid1, row, col) == 3){
                      copygrid[row][col] = 'X';
                      }     
                    }
                }
            }
            System.out.println(copygrid);
    
    
    //Output to File        
      output = JOptionPane.showInputDialog ("Where is the output file?");
      PrintWriter out = new PrintWriter(output);      
      out.println(Arrays.toString(copygrid));
        }
    
    //Check to see if cell is live or dead    
        public static boolean status(char value){  
                if (value == 'X'){
                    return true;
                } else {
                return false;
                }
            }
    
    //See if neighbor is alive or dead    
        public static int liveSurrounding(char [][] grid, int a, int b){
    
            livecount = 0;
    
            if (grid[a-1][b] == 'X'){
                livecount++;
            }
            if (grid[a-1][b+1] == 'X'){
                livecount++;
            } 
            if (grid[a][b+1] == 'X'){
                livecount++;
            }
            if (grid[a+1][b+1] == 'X'){
                livecount++;
            }
            if (grid[a+1][b] == 'X'){
                livecount++;
            }
            if (grid[a+1][b-1] == 'X'){
                livecount++;
            }
            if (grid[a][b-1] == 'X'){
                livecount++;
            }
            if (grid[a-1][b-1] == 'X'){
                livecount++;
            }
            return livecount;
        }   
    
    //Adjust alive cells for each condition    
        public static char[][] moves(int a, int b){
    
            switch(livecount){
                case 0: copygrid[a][b] = 'X'; break;
                case 1: copygrid[a--][b] = 'X'; break;
                case 2: copygrid[a][b++] = 'X'; break;
                case 3: copygrid[a][b--] = 'X'; break;
                case 4: copygrid[a++][b] = 'X'; break;
                case 5: copygrid[a--][b++] = 'X'; break;
                case 6: copygrid[a++][b--] = 'X';
                case 7: copygrid[a--][b--] = 'X'; break;
                case 8: copygrid[a++][b++] = 'X'; break;
                default:
            }
    
                        return copygrid;         
        }    
    }
    

4 个答案:

答案 0 :(得分:0)

你的问题确实不够准确,我们不打算在这里查看你的所有代码,也许CodeReview.SE的人愿意这样做,一旦你的代码有效......

要回答有关如何避免IndexOutOfBoundsException的问题,可以通过首先检查您要访问的元素是否在数组的边界内,然后访问它来避免这种情况,例如:

private void accessArray(final int[] array, final int index) {
    if (0 <= index && index < array.length) {
        int whatever = array[index];
    }
}

答案 1 :(得分:0)

回答#3:Arrays.toString通过在每个元素上调用被覆盖的toString来工作。二维数组是一个数组数组。因此,数组的每个元素本身就是一个数组,并且在数组上调用toString会为您提供一个地址,因为它看起来就像您已经知道的那样。

toString的{​​{3}}说:

  

如果数组包含其他数组作为元素,则转换为   字符串由Object.toString()方法继承自Object   描述他们的身份而不是他们的内容。

调查我认为你想要的documentation

答案 2 :(得分:0)

有些错误......

  1. char[] chararray = new char[900]; - &gt;你固定的长度?您的文件内容超过30个字符,然后IndexOutOfBoundsException将来
  2. 在此声明int i=4,然后 30 x 30 + 4 = 904 ,然后IndexOutOfBoundsException将会出现

    int i = 4; for(int row = 0; row&lt; 30; row ++){     for(int col = 0; col&lt; 30; col ++){        grid1 [row] [col] = chararray [i];        我++;        是System.out.print(GRID1 [行] [COL]);     } 的System.out.println(); }

  3. if (grid[a - 1][b] == 'X')如果a=00-1=-1 IndexOutOfBoundsException那么{{1}}将来.............同样会更多,更多。 ......

  4. 所以我说,回顾你的代码

答案 3 :(得分:0)

对于#2:使用此:

for(int gen=0;gen<gens;gen++){
    for (int row = 0; row < 30; row++){
        for (int col = 0; col < 30; col ++){
            if (status(grid1[row][col])){
                liveSurrounding(grid1, row, col);
                moves(row, col);
            }
            else if (!status(grid1[row][col])){
                //run 3 checker
              if (liveSurrounding(grid1, row, col) == 3){
              copygrid[row][col] = 'X';
              }     
            }
        }
    }
}