// PUT YOUR HEADER DOCUMENTATION HERE!

import java.util.Scanner;
import java.util.Random;


public class life
{

    // the size of the grid (GRIDSIZE x GRIDSIZE)
    final private static int GRIDSIZE = 18;

    /********************************************************************************/
    public static void main ( String args[] )
    {
        boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
        char choice;
        int x = 1;
        Scanner sc = new Scanner ( System.in );

        do
        {
            System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
            choice = sc.next().charAt(0);
        } while ( choice != 'r' && choice != 'q' && choice != 'g' );

        clearGrid (board);
        setup(board,choice);

        do
        {
            System.out.printf ("Viewing generation #%d:\n\n", x++);
            displayGrid(board);
            genNextGrid(board);
            System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
            choice = sc.next().charAt(0);
        } while ( choice != 'q' );

    }

    /********************************************************************************/
    public static void setup (boolean[][] board, char which )
    {
        Random randomNumbers = new Random();

        clearGrid(board);

        if ( which == 'q' )
        {
            // Set up the Queen Bee Shuttle pattern
            board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true;
            board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
            board[11][1] = true;
        }
        else if ( which == 'g' )
        {
            // Set up a Glider
            board [17][0] = true; board[16][1] = true; board[15][1] = true;
            board[16][2] = true;
            board [17][2] = true;
        }
        else
        {
            // set up random
            for (int row = 0; row < board.length; row++ )
            {
                for (int col = 0; col < board[row].length; col++ )
                {
                    if ( randomNumbers.nextInt() % 2 == 0 )
                        board[row][col] = true;
                }
            }
        }

    }

    /********************************************************************************/
    public static void displayGrid (boolean[][] grid)
    {
        // Start printing the top row of numbers
        System.out.print ("   ");
        for (int x = 1; x <= grid.length; x++)
        {
            if ((x / 10) != 0)
                System.out.printf ( "%d", x / 10 );
            else
                System.out.print ( " " );
        }

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

        for (int x = 1; x <= grid.length; x++)
        {
            System.out.printf ( "%d", x % 10 );
        }
        System.out.println();

        for (int r = 0; r < grid.length; r++)
        {
            System.out.printf ( "%d", r+1 );
            if (r + 1 < 10)
                System.out.print ( "  " );
            else
                System.out.print ( " " );
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == true)
                    System.out.print ( "*" );
                else
                    System.out.print ( " " );
            }
            System.out.println();
        }
    }


    /*******************************************************************************/

    // put the three methods you must write here and make sure to document
    // them!

    public static void clearGrid ( boolean[][] board )
    {
        int col=0;
        for (int row = 0; row <18; row++)
             {

            while(col<18)
             {
                 board [row][col] = false ;
                    col++;
             }
             }

    }

    public static void genNextGrid ( boolean[][] board )
    {
        int row = 0;
        int col = 0;
        boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE];

        int count = countNeighbors ( board , row, col );

        switch (count)
        {
            case 0-1:
            {
                if(board[row][col]==true | board[row][col]==false)
                    temp[row][col]=false;
                break;
            }
            case 2:
            {
                if(board[row][col]==true)
                    temp[row][col]=true;


                if(board[row][col]==false)
                    temp[row][col]=false;
                break;
            }
            case 3:
            {
                if(board[row][col]==false | board[row][col]==false)
                    temp[row][col]=true;
                break;
            }
            case 4-8:
                if(board[row][col]==false | board[row][col]==false)
                    temp[row][col]=false;
                break;
        }

        board[row][col]=temp[row][col];
        
                for(int r=0; r<GRIDSIZE; r++)
                for(int c=0;c<GRIDSIZE;c++)
                displayGrid(board);

  
    }

    public static int countNeighbors ( final boolean[][] board, final int row, final int col )
    {
        int c = 0;
       if(row==0 && col==0)//used to count top left corner neighboors
       {
           if (board[row+1][col]==true)
               c++;
           if (board[row][col+1]==true)
               c++;
           if(board[row+1][col+1]==true)
               c++;
       }//end of top left corner count

       if (row==0&& col!=0 && col!=18)//used to count top row neighbooors
       {
           for(int m=-1; m<=2;m++)
           {
               if(board[row+1][col+m]==true)
                   c++;
           }
           if(board[row][col-1]==true)
               c++;
           if(board[row][col+1]==true)
               c++;
       }//end of top row count

       if(row==0 && col==18)// used to count top left corner nieghboors
       {
           if (board[row+1][col]==true)
               c++;
           if (board[row][col-1]==true)
               c++;
           if(board[row+1][col-1]==true)
               c++;

       }//end of top left corner count

       if(col==0 && row!=0 && row!=18)// used to count left colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col+1]==true)
                   c++;
           }
           if(board[row-1][col]==true)
               c++;
           if(board[row+1][col]==true)
               c++;
       }//end of left colum count

       if(col==0 && row==18)//used to count bottom left corner nieghboors
       {
           if (board[row-1][col]==true)
               c++;
           if (board[row][col+1]==true)
               c++;
           if(board[row-1][col+1]==true)
               c++;
       }//end of bottom  left corner count

       if(col==18&& row!=0 && row!=18)//used to count right colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col-1]==true)
                   c++;
           }
           if(board[row-1][col]==true)
               c++;
           if(board[row+1][col]==true)
               c++;

       }//end of right colum count

       if(col==18&& row==18)// used to count bottom right corner nieghboors
       {
           if (board[row-1][col]==true)
               c++;
           if (board[row][col-1]==true)
               c++;
           if(board[row-1][col-1]==true)
               c++;

       }//end of bottom right corner count

       if (row==18 && col!=0  && col!=18)//used to count bottom row neighboors
       {
          for(int m=-1; m<=2;m++)
           {
           if(board[row-1][col+m]==true)
                   c++;
           }
           if(board[row][col-1]==true)
               c++;
           if(board[row][col+1]==true)
               c++;
       }//end of bottom row count

       if(row!=0 && row!= 18 && col !=0 && col != 18)//used to count all inbetween
       {
        for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col-1]==true)
               c++;
       }
              for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col+1]==true)
               c++;
       }
         if (board[row-1][col]==true)
               c++;
         if (board[row+1][col]==true)
               c++;
       }//end of everything else count


        return c;
    }

}

cant seem to get it to generate to next generation. Has to do with genNextGrid, but I have no clue where to go from there

Recommended Answers

All 6 Replies

board[row][col]=temp[row][col]; shouldn't this be in a row/col loop?
and, FYI...
if (expr == true) is just long way to say if (expr) and
if (expr == false) is just long way to say if (!expr)

I think the problem is with countNeighbors.

// PUT YOUR HEADER DOCUMENTATION HERE!

import java.util.Scanner;
import java.util.Random;


public class jhiciano_Life
{

    // the size of the grid (GRIDSIZE x GRIDSIZE)
    final private static int GRIDSIZE = 18;

    /********************************************************************************/
    public static void main ( String args[] )
    {
        boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
        char choice;
        int x = 1;
        Scanner sc = new Scanner ( System.in );

        do
        {
            System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
            choice = sc.next().charAt(0);
        } while ( choice != 'r' && choice != 'q' && choice != 'g' );

        clearGrid (board);
        setup(board,choice);

        do
        {
            System.out.printf ("Viewing generation #%d:\n\n", x++);
            displayGrid(board);
            genNextGrid(board);
            System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
            choice = sc.next().charAt(0);
        } while ( choice != 'q' );

    }

    /********************************************************************************/
    public static void setup (boolean[][] board, char which )
    {
        Random randomNumbers = new Random();

        clearGrid(board);

        if ( which == 'q' )
        {
            // Set up the Queen Bee Shuttle pattern
            board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true;
            board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
            board[11][1] = true;
        }
        else if ( which == 'g' )
        {
            // Set up a Glider
            board [17][0] = true; board[16][1] = true; board[15][1] = true;
            board[16][2] = true;
            board [17][2] = true;
        }
        else
        {
            // set up random
            for (int row = 0; row < board.length; row++ )
            {
                for (int col = 0; col < board[row].length; col++ )
                {
                    if ( randomNumbers.nextInt() % 2 == 0 )
                        board[row][col] = true;
                }
            }
        }

    }

    /********************************************************************************/
    public static void displayGrid (boolean[][] grid)
    {
        // Start printing the top row of numbers
        System.out.print ("   ");
        for (int x = 1; x <= grid.length; x++)
        {
            if ((x / 10) != 0)
                System.out.printf ( "%d", x / 10 );
            else
                System.out.print ( " " );
        }

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

        for (int x = 1; x <= grid.length; x++)
        {
            System.out.printf ( "%d", x % 10 );
        }
        System.out.println();

        for (int r = 0; r < grid.length; r++)
        {
            System.out.printf ( "%d", r+1 );
            if (r + 1 < 10)
                System.out.print ( "  " );
            else
                System.out.print ( " " );
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == true)
                    System.out.print ( "*" );
                else
                    System.out.print ( " " );
            }
            System.out.println();
        }
    }


    /*******************************************************************************/

    // put the three methods you must write here and make sure to document
    // them!

    public static void clearGrid ( boolean[][] board )
    {
        int col=0;
        for (int row = 0; row <=18; row++)
             {

            while(col<18)
             {
                 board [row][col] = false ;
                    col++;
             }
             }

    }

    public static void genNextGrid ( boolean[][] board )
    {
        
        boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE];

        
 for (int row=0; row<=17; row++)//start of row
 {
       for (int col=0; col<=17; col++)
       {
       
        temp[row][col]=board[row][col];
[I][B]       int count = countNeighbors ( board , row, col );[/B][/I]

        switch (count)
        {
            case 0:
            {
                if(board[row][col]==true || board[row][col]==false)
                    temp[row][col]=false;
                break;
            }
            case 2:
            {
                if(board[row][col]==true)
                    temp[row][col]=true;


                if(board[row][col]==false)
                    temp[row][col]=false;
                break;
            }
            case 3:
            {
                if(board[row][col]==false || board[row][col]==false)
                    temp[row][col]=true;
                break;
            }
            case 4:
                if(board[row][col]==false || board[row][col]==false)
                    temp[row][col]=false;
                break;
        }
             
                 board [row][col]=temp[row][col];
             
            
       }
 }



    }

    public static int countNeighbors ( final boolean[][] board, final int row, final int col )
    {
        int c = 0;
       if(row==0 && col==0)//used to count top left corner neighboors
       {
           if (board[row+1][col]==true){
               c++;}
           if (board[row][col+1]==true){
               c++;}
           if(board[row+1][col+1]==true){
               c++;}
       }//end of top left corner count

       if (row==0 && col>0 && col<=17)//used to count top row neighbooors
       {
[I] [B]          for(int m = -1; m < 2 ; m++)
           {
               if(board[row+1][col+m] == true){
               c++;}
           }[/B][/I]
           if(board[row][col-1]==true){
               c++;}
           if(board[row][col+1]==true){
               c++;}
       }//end of top row count

       if(row==0 && col==18)// used to count top left corner nieghboors
       {
           if (board[row+1][col]==true){
               c++;}
           if (board[row][col-1]==true){
               c++;}
           if(board[row+1][col-1]==true){
               c++;}

       }//end of top left corner count

       if(col==0 && row!=0 && row!=18)// used to count left colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col+1]==true){
               c++;}
           }
           if(board[row-1][col]==true){
               c++;}
           if(board[row+1][col]==true){
               c++;}
       }//end of left colum count

       if(col==0 && row==18)//used to count bottom left corner nieghboors
       {
           if (board[row-1][col]==true){
               c++;}
           if (board[row][col+1]==true){
               c++;}
           if(board[row-1][col+1]==true){
               c++;}
       }//end of bottom  left corner count

       if(col==18&& row!=0 && row!=18)//used to count right colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col-1]==true){
               c++;}
           }
           if(board[row-1][col]==true){
               c++;}
           if(board[row+1][col]==true){
               c++;}

       }//end of right colum count

       if(col==18&& row==18)// used to count bottom right corner nieghboors
       {
           if (board[row-1][col]==true){
               c++;}
           if (board[row][col-1]==true){
               c++;}
           if(board[row-1][col-1]==true){
               c++;}

       }//end of bottom right corner count

       if (row==18 && col!=0  && col!=18)//used to count bottom row neighboors
       {
          for(int m=-1; m<=2;m++)
           {
           if(board[row-1][col+m]==true){
               c++;}
           }
           if(board[row][col-1]==true){
               c++;}
           if(board[row][col+1]==true){
               c++;}
       }//end of bottom row count

       if(row!=0 && row!= 18 && col !=0 && col != 18)//used to count all inbetween
       {
        for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col-1]==true){
               c++;}
       }
              for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col+1]==true){
               c++;}
       }
         if (board[row-1][col]==true){
               c++;}
         if (board[row+1][col]==true){
               c++;}
       }//end of everything else count

        return c;
    }

}

So now i get a array out of bounds error the bold italized lines. Have no clue why. Added a loop int he genNextGrid Method. But in the for statements I cant set the limit to GRIDSIZE for some reason, i get syntax errors ?

// PUT YOUR HEADER DOCUMENTATION HERE!

import java.util.Scanner;
import java.util.Random;


public class jhiciano_Life
{

    // the size of the grid (GRIDSIZE x GRIDSIZE)
    final private static int GRIDSIZE = 18;

    /********************************************************************************/
    public static void main ( String args[] )
    {
        boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
        char choice;
        int x = 1;
        Scanner sc = new Scanner ( System.in );

        do
        {
            System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
            choice = sc.next().charAt(0);
        } while ( choice != 'r' && choice != 'q' && choice != 'g' );

        clearGrid (board);
        setup(board,choice);

        do
        {
            System.out.printf ("Viewing generation #%d:\n\n", x++);
            displayGrid(board);
            genNextGrid(board);
            System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
            choice = sc.next().charAt(0);
        } while ( choice != 'q' );

    }

    /********************************************************************************/
    public static void setup (boolean[][] board, char which )
    {
        Random randomNumbers = new Random();

        clearGrid(board);

        if ( which == 'q' )
        {
            // Set up the Queen Bee Shuttle pattern
            board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true;
            board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
            board[11][1] = true;
        }
        else if ( which == 'g' )
        {
            // Set up a Glider
            board [17][0] = true; board[16][1] = true; board[15][1] = true;
            board[16][2] = true;
            board [17][2] = true;
        }
        else
        {
            // set up random
            for (int row = 0; row < board.length; row++ )
            {
                for (int col = 0; col < board[row].length; col++ )
                {
                    if ( randomNumbers.nextInt() % 2 == 0 )
                        board[row][col] = true;
                }
            }
        }

    }

    /********************************************************************************/
    public static void displayGrid (boolean[][] grid)
    {
        // Start printing the top row of numbers
        System.out.print ("   ");
        for (int x = 1; x <= grid.length; x++)
        {
            if ((x / 10) != 0)
                System.out.printf ( "%d", x / 10 );
            else
                System.out.print ( " " );
        }

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

        for (int x = 1; x <= grid.length; x++)
        {
            System.out.printf ( "%d", x % 10 );
        }
        System.out.println();

        for (int r = 0; r < grid.length; r++)
        {
            System.out.printf ( "%d", r+1 );
            if (r + 1 < 10)
                System.out.print ( "  " );
            else
                System.out.print ( " " );
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == true)
                    System.out.print ( "*" );
                else
                    System.out.print ( " " );
            }
            System.out.println();
        }
    }


    /*******************************************************************************/

    // put the three methods you must write here and make sure to document
    // them!

    public static void clearGrid ( boolean[][] board )
    {
        int col=0;
        for (int row = 0; row <=18; row++)
             {

            while(col<18)
             {
                 board [row][col] = false ;
                    col++;
             }
             }

    }

    public static void genNextGrid ( boolean[][] board )
    {
        
        boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE];

        
 for (int row=0; row==GRIDSIZE; row++)//start of row
 {
       for (int col=0; col==GRIDSIZE; col++)
       {
       
        temp[row][col]=board[row][col];
        int count = countNeighbors ( board , row, col );

        switch (count)
        {//start of count switch
            case 0:
            {
                if(board[row][col]==true || board[row][col]==false){
                    temp[row][col]=false;}
                break;
            }
            case 2:
            {
                if(board[row][col]==true){
                    temp[row][col]=true;}


                if(board[row][col]==false){
                    temp[row][col]=false;}
                break;
            }
            case 3:
            {
                if(board[row][col]==false || board[row][col]==false){
                    temp[row][col]=true;}
                break;
            }
            case 4:
                if(board[row][col]==false || board[row][col]==false){
                    temp[row][col]=false;}
                break;
        }//end of switch statement
             
                 board [row][col]=temp[row][col];
             
            
       }
 }



    }

    public static int countNeighbors ( final boolean[][] board, final int row, final int col )
    {
        int c = 0;

       if(row==0 && col==0)//used to count top left corner neighboors
       {
           if (board[row+1][col]==true){
               c++;}
           if (board[row][col+1]==true){
               c++;}
           if(board[row+1][col+1]==true){
               c++;}
       }//end of top left corner count

      if(row==0 && col==18)// used to count top left corner nieghboors
       {
           if (board[row+1][col]==true){
               c++;}
           if (board[row][col-1]==true){
               c++;}
           if(board[row+1][col-1]==true){
               c++;}

       }//end of top left corner count

       if (row==0 && col>0 && col<=GRIDSIZE)//used to count top row neighbooors
       {
           if(board[row+1][col-1]==true){
               c++;}
           if(board[row+1][col+1]==true){
               c++;}
           if(board[row+1][col] == true){
               c++;}
           
           if(board[row][col-1]==true){
               c++;}
           if(board[row][col+1]==true){
               c++;}
       }//end of top row count


       if(col==0 && row!=0 && row!=18)// used to count left colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col+1]==true){
               c++;}
           }
           if(board[row-1][col]==true){
               c++;}
           if(board[row+1][col]==true){
               c++;}
       }//end of left colum count

       if(col==0 && row==18)//used to count bottom left corner nieghboors
       {
           if (board[row-1][col]==true){
               c++;}
           if (board[row][col+1]==true){
               c++;}
           if(board[row-1][col+1]==true){
               c++;}
       }//end of bottom  left corner count

       if(col==18&& row!=0 && row!=18)//used to count right colum neighboors
       {
           for(int m=-1; m<=2; m++ )
           {
               if(board[row+m][col-1]==true){
               c++;}
           }
           if(board[row-1][col]==true){
               c++;}
           if(board[row+1][col]==true){
               c++;}

       }//end of right colum count

       if(col==18&& row==18)// used to count bottom right corner nieghboors
       {
           if (board[row-1][col]==true){
               c++;}
           if (board[row][col-1]==true){
               c++;}
           if(board[row-1][col-1]==true){
               c++;}

       }//end of bottom right corner count

       if (row==18 && col!=0  && col!=18)//used to count bottom row neighboors
       {
          for(int m=-1; m<=2;m++)
           {
           if(board[row-1][col+m]==true){
               c++;}
           }
           if(board[row][col-1]==true){
               c++;}
           if(board[row][col+1]==true){
               c++;}
       }//end of bottom row count

       if(row!=0 && row!= 18 && col !=0 && col != 18)//used to count all inbetween
       {
        for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col-1]==true){
               c++;}
       }
              for(int a=1; a>=-1;a--)
       {
           if (board[row-a][col+1]==true){
               c++;}
       }
         if (board[row-1][col]==true){
               c++;}
         if (board[row+1][col]==true){
               c++;}
       }//end of everything else count

        return c;
    }

}

fixed the errors I had, but still cant not get the method genNextGrid to generate

import java.util.Scanner;
import java.util.Random;


public classLife
{

    // the size of the grid (GRIDSIZE x GRIDSIZE)
    final private static int GRIDSIZE = 18;

    /********************************************************************************/
    public static void main ( String args[] )
    {
        boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
        char choice;
        int x = 1;
        Scanner sc = new Scanner ( System.in );

        do
        {
            System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
            choice = sc.next().charAt(0);
        } while ( choice != 'r' && choice != 'q' && choice != 'g' );

        clearGrid (board);
        setup(board,choice);

        do
        {
            System.out.printf ("Viewing generation #%d:\n\n", x++);
            displayGrid(board);
            genNextGrid(board);
            System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
            choice = sc.next().charAt(0);
        } while ( choice != 'q' );

    }

    /********************************************************************************/
    public static void setup (boolean[][] board, char which )
    {
        Random randomNumbers = new Random();

        clearGrid(board);

        if ( which == 'q' )
        {
            // Set up the Queen Bee Shuttle pattern
            board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true;
            board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
            board[11][1] = true;
        }
        else if ( which == 'g' )
        {
            // Set up a Glider
            board [17][0] = true; board[16][1] = true; board[15][1] = true;
            board[16][2] = true;
            board [17][2] = true;
        }
        else
        {
            // set up random
            for (int row = 0; row < board.length; row++ )
            {
                for (int col = 0; col < board[row].length; col++ )
                {
                    if ( randomNumbers.nextInt() % 2 == 0 )
                        board[row][col] = true;
                }
            }
        }

    }

    /********************************************************************************/
    public static void displayGrid (boolean[][] grid)
    {
        // Start printing the top row of numbers
        System.out.print ("   ");
        for (int x = 1; x <= grid.length; x++)
        {
            if ((x / 10) != 0)
                System.out.printf ( "%d", x / 10 );
            else
                System.out.print ( " " );
        }

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

        for (int x = 1; x <= grid.length; x++)
        {
            System.out.printf ( "%d", x % 10 );
        }
        System.out.println();

        for (int r = 0; r < grid.length; r++)
        {
            System.out.printf ( "%d", r+1 );
            if (r + 1 < 10)
                System.out.print ( "  " );
            else
                System.out.print ( " " );
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == true)
                    System.out.print ( "*" );
                else
                    System.out.print ( " " );
            }
            System.out.println();
        }
    }


    /*******************************************************************************/
// Method Name : clearGrid
// Parameters : board(boolean)
// Return value(s) : none
// Partners : 
// Description : resets the grid to false 

    public static void clearGrid ( boolean[][] board )
    {
        int col=0;
        for (int row = 0; row <=18; row++)
             {

            while(col<18)
             {
                 board [row][col] = false ;
                    col++;
             }
             }

    }
// Method Name : genNextGrid
// Parameters : board(boolean)
// Return value(s) : none
// Partners : 
// Description : applies rules to temp array and save it under old array

/*******************************************************************************/
    
public static void genNextGrid ( boolean[][] board )
    {     
        boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE];
     
 for (int row=0; row<GRIDSIZE; row++)//start of row
 {
       for (int col=0; col<GRIDSIZE; col++)
       {
       
        temp[row][col]=board[row][col];
        int count = countNeighbors ( temp , row, col );

        switch (count)
        {//start of count switch
            case 0: case 1:{
                if(temp[row][col] || !temp[row][col]){
                    temp[row][col]=false;}
                break;}

            case 2: {
                if(temp[row][col]){
                    temp[row][col]=true;}

                if(!temp[row][col]){
                    temp[row][col]=false;}
                break;}

            case 3:{
                if(!temp[row][col] || !temp[row][col]){
                    temp[row][col]=true;}
                break;}

            case 4: case 5: case 6: case 7: case 8:{
                if(!temp[row][col]|| !temp[row][col]){
                    temp[row][col]=false;}
                break;}   
        }//end of switch statement
             
                 board [row][col]=temp[row][col];                    
       }//end of for row
    }//end of for col
}
    /*******************************************************************************/

// Method Name : conutNieghborrs
// Parameters : board(boolean), int row, int col
// Return value(s) : ncount
// Partners : 
// Description : get data fro array and counts number of neighbors around array
    public static int countNeighbors ( final boolean[][] board, final int row, final int col )
    {

      int ncount = 0;
        for(int r = row-1; r <= row+1; r++) {
            if(row < 0 || row >= GRIDSIZE ) {
                continue;
            }
            for(int c = col - 1; c <= col + 1; c++) {
                if(col < 0 || col >= GRIDSIZE || (r == row && c == col)) {
                    continue;
                }
                if(board[row][col]) {
                    ncount++;
                }
            }
        }
        return ncount;
    }
}

still can not get it to generate to next Generation

^ bump

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.