Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default Game of life methods help

    I have been working on this project for a while now and its due soon. I can't seem to get the genNextgeneration method to work in my program. This is Conway's Game of Life and this method is suppose to generate the next generation. It needs to use the 2 dimensional array board that is passed to it as the "current generation" then it should create a second temporary 2 dimensional array that will hold the next generation. This method also needs to call on the method countNeighbors() to help decide if the cell will be alive or dead in the next generation. Once the next generation of the simulation into your temporary 2 dimensional array it needs to copy that array back over the current generation (called board) so the next generation can leave the method through the parameter....I am so confused I don't even know if I didn't it right. If anyone could help that'd be awesome!

    here's my code:

    Java Code:
    public static void clearGrid ( boolean[][] board )
        {
            for( int row = 0; row < board.length; row++ )
            {
                    for( int col = 0; col < board[row].length; col++ )
                    {
                            board[row][col] = false;
                    }
            }
        }
    
        public static void genNextGrid ( boolean[][] board )
        {
            boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE];
    
            for( int row = 0; row < 18; row++)
            {
                    for( int col = 0; col < 18; col++ )
                    {
                            int count = countNeighbors ( board, row, col);
                    }
            }
    
            for( int row = 0; row < 18; row++ )
            {
                    for( int col = 0; col < 18; col++ )
                    {
                            int count = countNeighbors( temp, row, col );
                            board[row][col] = temp[row][col];
    
                            switch( count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            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 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;
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing the same thing as before, only now you're trying to do both set temp's value and board's value in the second set of nested for loops. Again, don't do this. Set your temp array values in the first set and use this info to set your board values in the second set. This way, you won't be changing the board array prematurely which would prevent you from messing up the countNeighbors calculation. e.g.,

    Java Code:
          // the first set of nested for loops calculates the values for the temp
          // array and nothing else.  It DOES NOT CHANGE the board array
          for (int row = 0; row < 18; row++) {
             for (int col = 0; col < 18; col++) {
                int count = countNeighbors(board, row, col);
                // here use count to calc the proper value for the temp
                
                // temp[row][col] = .....etc...
    
             }
          }
    
          // the second set of nested for loops does nothing but
          // sets the board with the values held by temp
          for (int row = 0; row < 18; row++) {
             for (int col = 0; col < 18; col++) {
                // this is all that the second set of for loops should do
                board[row][col] = temp[row][col];
             }
          }
    A cool way to simplify your code is to use a truth table. Something like so:

    Java Code:
    public class ConwayLifeFoo {
       private static final boolean[] TRUTH_GRID_ALIVE = {
          false, false, true, true, false, false, false, false // alive to start with
       };
       private static final boolean[] TRUTH_GRID_DEAD = {
          false, false, true, false, false, false, false, false // dead to start with
       };
       private static Map<Boolean, boolean[]> truthMap = new HashMap<Boolean, boolean[]>();
       
       static {
          truthMap.put(Boolean.TRUE, TRUTH_GRID_ALIVE);
          truthMap.put(Boolean.FALSE, TRUTH_GRID_DEAD);
       }
    
       public static void genNextGrid(boolean[][] board) {
          
          // the safest way to initialize temp is to use the lengths from board.
          boolean[][] temp = new boolean[board.length][board[0].length];
    
          // the first set of nested for loops calculates the values for the temp
          // array and nothing else.  It DOES NOT CHANGE the board array
          for (int row = 0; row < 18; row++) {
             for (int col = 0; col < 18; col++) {
                int count = countNeighbors(board, row, col);
                // here use count to calc the proper value for the temp
                
                temp[row][col] = truthMap.get(board[row][col])[count];
                // use count to calc whether cell should be alive or dead in next gen
                // update the temp array only.
             }
          }
    
          // the second set of nested for loops does nothing but
          // sets the board with the values held by temp
          for (int row = 0; row < 18; row++) {
             for (int col = 0; col < 18; col++) {
                board[row][col] = temp[row][col];
             }
          }
       }

  3. #3
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay thank you for your help!!! I just have one question I understand the 2 loops better now, but in the first one you say to use count to to calc the value for temp. I just don't know how to do that...is that where I use my switch statement or is that all wrong? or would something like this work:

    Java Code:
    for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            int count = countNeighbors( board, row, col );
                            if( board[row][col] < 2 )
                            {
                                    temp[row][col] = false;
                            }
    
                            if( board[row][col] == 3 )
                            {
                                    temp[row][col] = true;
                            }
    
                            if( board[row][col] >= 4 )
                            {
                                    temp[row][col] = false;
                            }
    
                            board[row][col] = 0;
                    }board[row][col]= temp[row][col]
    then use the second loop.

    and very nice work on simplifying my code...if I could use that way I would...I'm just a beginner.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, that's where you can use your switch statement, but again in that loop, use board, but just set temp, not board.

  5. #5
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay here is where I'm at now...i don't know if I did it right but I set temp to board and then at the end set board to temp. but it still doesn't give me the next generation, ughhh I'm kind of confused with this assignment.

    Java Code:
     public static void genNextGrid ( boolean[][] board )
        {
            boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE];
    
            for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            temp[row][col] = board[row][col];
    
                            int count = countNeighbors( temp, row, col );
    
                            switch(count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            temp[row][col] = false;
                                            break;
                            }
                            board[row][col]= temp[row][col];
                    }
            }
    
            for( int row = 0; row < GRIDSIZE; row ++ )
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            board[row][col] = temp[row][col];
                    }
            }
        }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
            for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            temp[row][col] = board[row][col];
    
                            int count = countNeighbors( temp, row, col );
    
                            switch(count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            temp[row][col] = false;
                                            break;
                            }
                            board[row][col]= temp[row][col];  ///// **** WTF ???? ****
                    }
            }
    You're still setting the board from within the first set of loops!!!:
    :eek:

  7. #7
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    sorry I sent you the wrong code. I fixed that mistake and it still doesn't do anything. Is any of my other methods wrong to make it not produce the next generation...or some advice you could give me...thanks for your help. This is suppose to be our hardest project...I'm just frustrated since I've been working on it forever it seems. I'm thinking maybe my countNeighbors method

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bigskers76 View Post
    sorry I sent you the wrong code. I fixed that mistake and it still doesn't do anything.
    Until you show us the corrected code, we can assume nothing here.

  9. #9
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay now here is my code same except I deleted where I set board in the for loop. From what I could understand what I needed to do was use board but set temp not board and I believe that is what I did. let me know if I am wrong

    Java Code:
    for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            temp[row][col] = board[row][col];
    
                            int count = countNeighbors( temp, row, col );
    
                            switch(count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            temp[row][col] = false;
                                            break;
                            }
                    }
            }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Shouldn't this:
    Java Code:
    int count = countNeighbors(temp, row, col );
    be this?:
    Java Code:
    int count = countNeighbors(board, row, col );
    Big difference. Again, board holds the state of your grid now. temp is to hold the state of the grid in the future. You use the now state to help you determine the future state. Make sense?

    If this is still failing, you may need to post the whole code here. I'd recommend zipping it and uploading it if need be.

  11. #11
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    I don't know how to zip it...The code is really long the only methods we have to write is the last 3 methods. I'm thinking my countNeighbors method isn't right.

    Java Code:
    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();
            }
        }
    public static void clearGrid ( boolean[][] board )
        {
            for( int row = 0; row < board.length; row++ )
            {
                    for( int col = 0; col < board[row].length; col++ )
                    {
                            board[row][col] = false;
                    }
            }
        }
    
        public static void genNextGrid ( boolean[][] board )
        {
            boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE];
    
            for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            temp[row][col] = board[row][col];
    
                            int count = countNeighbors( board, row, col );
    
                            switch(count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            temp[row][col] = false;
                                            break;
                            }
                    }
            }
    
            for( int row = 0; row < GRIDSIZE; row ++ )
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            board[row][col] = temp[row][col];
                    }
            }
        }
    
        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;
        }
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm thinking my countNeighbors method isn't right.
    I think you're right. Note the comment:
    Java Code:
        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] ) // *********** here ****
                                    {
                                            ncount++;
                                                           }
                            }
                    }
                    return ncount;
        }
    instead of
    Java Code:
    if (board[row][col] )
    shouldn't that be:
    Java Code:
    if (board[r][c] )
    ?

    For me, cleaner is
    Java Code:
       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 (r >= 0 && r < GRIDSIZE) {
                for (int c = col - 1; c <= col + 1; c++) {
                   if (c >= 0 && c < GRIDSIZE && (r != row || c != col)) {
                      if (board[r][c]) {
                         ncount++;
                      }
                   }
                }
    
             }
          }
          return ncount;
       }

    Note that my debugging technique was to add println statements in the code, in particular here:
    Java Code:
       public static void genNextGrid(boolean[][] board) {
          boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE];
    
          for (int row = 0; row < GRIDSIZE; row++) {
             for (int col = 0; col < GRIDSIZE; col++) {
                temp[row][col] = board[row][col];
    
                int count = countNeighbors(board, row, col);
                System.out.printf(" %1s%d,  ", board[row][col]?"t":"f", count);
    
                switch (count) {
    well, actually a printf statement with a println statement after the inner loop.

  13. #13
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    YOU ARE MY HERO :) thank you so much I can't thank you enough!!!

  14. #14
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    YOU ARE MY HERO :) thank you so much I can't thank you enough!!!

    one more question before I leave you alone

    my grid looks like this but with all the other stuff it's suppose to
    my problem is it's suppose to 18 by 18 how do I fix that?
    111111
    1 2 3 4 5 6 7 8
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, have you tried sprinkling the code with judiciously labeled println statements and see what is happening at different locations in your code? Your best bet is to spend some time playing around with debugging techniques.

  16. #16
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay I'll do that thank you!

  17. #17
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay I hope this is the last time I have to ask for help :( this is due soon! I fixed my countNeighbors method because the one I had doesn't work for when it is checking row/ col 18 because you can't 18+1 when the grid is only 18..This is what I get when I click q and instead of asking for the next generation I get the error message. Thanks again!

    Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? q
    Viewing generation #1:

    111111111
    123456789012345678
    1
    2
    3
    4
    5
    6 **
    7 *
    8 *
    9 *
    10 *
    11 *
    12 **
    13
    14
    15
    16
    17
    18
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Life.countNeighbors(Life.java:260)
    at Life.genNextGrid(Life.java:144)
    at Life.main(Life.java:34)

    here's my code: my cleargrid and genNextGeneration methods haven't chaged.

    Java Code:
    public static int countNeighbors ( final boolean[][] board, final int row, final int col )
        {
            int n = 0;
    
            //checks upper left, cells around row=0, col=0, corners
            if( row == 0 && col ==0 )
            {
                    if(board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row+ 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //bottom left
            else if( col == 0 && row == GRIDSIZE - 1 )
            {
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col + 1] == true )
                    {
                            n++;
    }
            }
            //upper right
            else if( row == 0 && col == GRIDSIZE - 1 )
            {
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
            }
            //bottom right
            else if( row == GRIDSIZE - 1 && col == GRIDSIZE -1)
            {
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
            }
     //edges(not corners), top edge
            if( row == 0 )
            {
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //bottom edge
            else if( row == GRIDSIZE - 1 )
            {
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                                      n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if(board[row - 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //left edge
            else if( col == 0 )
            {
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col +1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
    n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //right edge
            else if( col == GRIDSIZE - 1 )
            {
                    if(board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
            }
    
            //cell with 8 surronding cells
     else
            {
                    //below
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col + 1] == true )
                    {
                            n++;
                    }
                    // same
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
                    //above
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
     if( board [row + 1][col] == true )
                    {
                            n++;
                    }
            }
            return n;
        }
    }
    Last edited by bigskers76; 12-12-2009 at 04:59 AM.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code? The line with the error message?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I see that you've modified your post to add some code. I suggest that you post it all, and that again, you show us where the error occurred. The key is to make it as easy as possible for someone to help you.

    Edit: also, one other suggestion is to reduce your indentation size a little bit (3 or four spaces work well), and make sure your indentation is true. Currently there's some ugliness in your code. Again, the easier your code is to read, the easier it is to help. Much luck.

  20. #20
    bigskers76 is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    okay I hope this makes it clearer.

    The thing about this program is the program was given to me except the last 3 methods (cleargrid, genNextgeneration, and countNeighbors) those are the ones I had to write.

    here is my whole code and the errors again...

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Life.countNeighbors(Life.java:260)
    at Life.genNextGrid(Life.java:144)
    at Life.main(Life.java:34)


    I'll write where it says the codes is wrong

    Java Code:
    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);                                                  <-- This is line 34 error
                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();
            }
        }
    public static void clearGrid ( boolean[][] board )
        {
            for( int row = 0; row < board.length; row++ )
            {
                    for( int col = 0; col < board[row].length; col++ )
                    {
                            board[row][col] = false;
                    }
            }
        }
    
        public static void genNextGrid ( boolean[][] board )
        {
            boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE];
    
            for( int row = 0; row < GRIDSIZE; row++)
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            temp[row][col] = board[row][col];
    
                            int count = countNeighbors( board, row, col );    <--This is the Line 144 error
    
                            switch(count)
                            {
                                    case 0: case 1:
                                            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:
                                            temp[row][col] = true;
                                            break;
                                    case 4: case 5: case 6: case 7: case 8:
                                            temp[row][col] = false;
                                            break;
                            }
                    }
            }
    
            for( int row = 0; row < GRIDSIZE; row ++ )
            {
                    for( int col = 0; col < GRIDSIZE; col++ )
                    {
                            board[row][col] = temp[row][col];
                    }
            }
        }
      public static int countNeighbors ( final boolean[][] board, final int row, final int col )
        {
            int n = 0;
    
            //checks upper left, cells around row=0, col=0, corners
            if( row == 0 && col ==0 )
            {
                    if(board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row+ 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //bottom left
            else if( col == 0 && row == GRIDSIZE - 1 )
            {
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //upper right
            else if( row == 0 && col == GRIDSIZE - 1 )
            {
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
            }
            //bottom right
            else if( row == GRIDSIZE - 1 && col == GRIDSIZE -1)
            {
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
            }
            //edges(not corners), top edge
            if( row == 0 )
            {
                    if( board[row][col - 1] == true )      <--This is the 260 line
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //bottom edge
            else if( row == GRIDSIZE - 1 )
            {
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                           n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if(board[row - 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //left edge
            else if( col == 0 )
            {
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col +1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                           n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
            }
            //right edge
            else if( col == GRIDSIZE - 1 )
            {
                    if(board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col] == true )
                    {
                            n++;
                    }
            }
    
            //cell with 8 surronding cells
            else
            {
                    //below
                    if( board[row - 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col] == true )
                    {
                            n++;
                    }
    
                    if( board[row - 1][col + 1] == true )
                    {
                            n++;
                    }
                    // same
                    if( board[row][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row][col + 1] == true )
                    {
                            n++;
                    }
                    //above
                    if( board[row + 1][col - 1] == true )
                    {
                            n++;
                    }
    
                    if( board[row + 1][col + 1] == true )
                    {
                            n++;
                    }
                    if( board [row + 1][col] == true )
                    {
                            n++;
                    }
            }
            return n;
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. game of life
    By bigskers76 in forum New To Java
    Replies: 10
    Last Post: 12-09-2009, 06:21 AM
  2. 2d array help- Game of Life
    By n3philim in forum New To Java
    Replies: 2
    Last Post: 07-23-2009, 01:01 AM
  3. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 04:08 AM
  4. Game of Life assignment
    By javan00b in forum New To Java
    Replies: 4
    Last Post: 04-28-2008, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •