康威在循环算法中的生命游戏

时间:2014-09-08 18:56:34

标签: java conways-game-of-life

代码尚未完成,但我在算法中遇到了一些奇怪的问题。当它循环时,它每次都会在neighbor5的同一个地方崩溃,除非我把游戏设为3比3.我不确定循环有什么问题,看起来算法应该正常工作并且到目前为止它已经正确编程了。 / p>

    package dsa.hw1;
    import java.util.Random;
    import java.util.Scanner;
    public class DSAHW1 {
    public static void main(String[] args) {

Scanner kb = new Scanner(System.in);
System.out.println("what would you like to be the height?");
int height = kb.nextInt();
System.out.println("what would you like to be the width?");
int width = kb.nextInt();


boolean[][] GameBoard = new boolean[height][width];

Random number = new Random();
for(int i =0; i < GameBoard.length; i++){
    for(int j = 0; j < GameBoard[i].length; j++){
    int theRandomNumber = number.nextInt(2);
        System.out.print(theRandomNumber);
        if(theRandomNumber == 1){
            GameBoard[i][j] = true; 
    }
        else{
            GameBoard[i][j] = false; 
        }

    }
}
System.out.println("");
String[][] Display = new String[height][width];
for(int i =0; i < GameBoard.length; i++){
    for(int j = 0; j < GameBoard[i].length; j++){
    if(GameBoard[i][j]){
        Display[i][j] = "*";
    }
    else{
        Display[i][j] = "-";
    }
        System.out.print(Display[i][j] + " ");

    }
    System.out.println("");
}

int heightAlg = height -1;
int widthAlg = width - 1;

//algorithm
int neighbors1 = 0, neighbors2 = 0, neighbors3 = 0, neighbors4 = 0, neighbors5 = 0;
int neighbors6 = 0, neighbors7 = 0, neighbors8 = 0, neighbors9 = 0, neighbors10 = 0;

for(int i =0; i < widthAlg; i++){
    for(int j = 0; j < heightAlg; j++){
   if(i == 0 & j == 0){     

       if(GameBoard[i][j++])        {neighbors1++;}
       if(GameBoard[i++][j])        {neighbors1++;}
       if(GameBoard[i++][j++])      {neighbors1++;}
   }

   else if( i == 0 && j > 0 && j < widthAlg ){

       if(GameBoard[i][j--])        {neighbors2++;}
       if(GameBoard[i++][j--])      {neighbors2++;}
       if(GameBoard[i][j++])        {neighbors2++;}
       if(GameBoard[i++][j])        {neighbors2++;}
       if(GameBoard[i++][j++])      {neighbors2++;}

   }

   else if(i == 0 && j == widthAlg){

       if(GameBoard[i][j--])        {neighbors3++;}
       if(GameBoard[i++][j--])      {neighbors3++;}
       if(GameBoard[i++][j])        {neighbors3++;}

   }

   else if( i > 0 && i < heightAlg && j == 0 ){


       if(GameBoard[i--][j])        {neighbors4++;}
       if(GameBoard[i][j++])        {neighbors4++;}
       if(GameBoard[i--][j++])      {neighbors4++;}
       if(GameBoard[i++][j])        {neighbors4++;}
       if(GameBoard[i++][j])        {neighbors4++;}

   }

    else if( i > 0 && i < heightAlg && j > 0 && j < widthAlg ){

       if(GameBoard[i--][j--])      {neighbors5++;}
       if(GameBoard[i--][j])        {neighbors5++;}
       if(GameBoard[i][j++])        {neighbors5++;}
       if(GameBoard[i--][j++])      {neighbors5++;}
       if(GameBoard[i++][j])        {neighbors5++;}
       if(GameBoard[i++][j])        {neighbors5++;}

   }
   else if( i > 0 && i < heightAlg && j == widthAlg ){


       if(GameBoard[i--][j])        {neighbors6++;}
       if(GameBoard[i--][j--])      {neighbors6++;}
       if(GameBoard[i++][j])        {neighbors6++;}
       if(GameBoard[i][j--])        {neighbors6++;}
       if(GameBoard[i++][j--])      {neighbors6++;}

   }

   else if( i==0 && j == heightAlg ){


       if(GameBoard[i--][j])        {neighbors7++;}
       if(GameBoard[i][j++])        {neighbors7++;}
       if(GameBoard[i--][j--])      {neighbors7++;}

   }

   else if( i == heightAlg && j > 0 && j < widthAlg ){


       if(GameBoard[i][j--])        {neighbors8++;}
       if(GameBoard[i--][j])        {neighbors8++;}
       if(GameBoard[i--][j++])      {neighbors8++;}
       if(GameBoard[i--][j])        {neighbors8++;}
       if(GameBoard[i][j++])        {neighbors8++;}

   }

   else if( i == heightAlg  && j == widthAlg ){


       if(GameBoard[i][j--])        {neighbors9++;}
       if(GameBoard[i--][j--])      {neighbors9++;}
       if(GameBoard[i--][j])        {neighbors9++;}

   }





            }
        } 
    }
}

1 个答案:

答案 0 :(得分:1)

看看这段代码:

       if(GameBoard[i--][j--])      {neighbors5++;}
       if(GameBoard[i--][j])        {neighbors5++;}
       if(GameBoard[i][j++])        {neighbors5++;}

您可以访问(i,j),(i - 1,j - 1),(i - 2,j - 1)单元格。
如果i == 1显然崩溃了,因为它试图访问索引为-1的元素。