Jonathan
Jonathan

Reputation: 205

Conway's game of life in looping through the algorithm

The code is not finished yet, but I am having some weird problem in the algorithm. When it loops through it crashes at the same spot at neighbors5 every time unless I make the game a 3 by 3. I am not sure what is wrong with the loop it looks like the algorithm should work and it was programmed correctly so far.

    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++;}

   }





            }
        } 
    }
}

Upvotes: 1

Views: 139

Answers (1)

kraskevich
kraskevich

Reputation: 18566

Look at this piece of code:

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

You access (i, j), (i - 1, j - 1), (i - 2, j - 1) cells.
If i == 1 it obviously crashes because it tries to access an element with index-1.

Upvotes: 1

Related Questions