Results 1 to 11 of 11

Thread: game of life

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

    Default game of life

    my problem with this is the genNextgrid isn't working. once the user puts in the information. the user gets asked if they wanted to know the next generation and when they choose to it comes up, but the board is blank...can anyone help!

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    // Change NetID to your NetID
    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 )
        {
            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];
                    }
            }
        }
    
        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;
        }
    }
    Last edited by Fubarable; 12-09-2009 at 02:06 AM. Reason: Code tags added

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Your probebly not gona get any answer unless you shorten your code to the error area and choose better title..

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    and use Code tags!
    Or PHP tags, those keep the identing..

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,957
    Rep Power
    19

    Default

    So do code tags.

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

    Default help with methods for game of life

    here's my code where the problem is occuring...my problem with this is the genNextgrid isn't working. once the user puts in the information. the user gets asked if they wanted to know the next generation and when they choose to it comes up, but the board is blank...can anyone help!

    Java Code:
    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++)
          {
                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];
             }
         }
    }
    
    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;
        }
    }

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

    Default

    I am no expert on the Game of Life, far from it, but I wonder if you're trying to do too much inside your nested for loops when creating the next generation. If you change the board array from inside the nested for loop, you will probably mess up the calculations for the neighbor cells of the one you just changed since they will "see" the new changes when it is their turn to calculate whether they should be alive or dead. In other words, as in Quantum Mechanics, your observations are effecting the observed.

    Perhaps what you need to do is to use nested for loops and the current board to just set the values of the temp grid. Then when you're completely done with this, have another set of nested for loops that do nothing but set the board's grid to the values held in the temp grid.

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

    Default

    thank you for your help
    Last edited by bigskers76; 12-09-2009 at 03:59 AM.

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

    Default

    You will get better the more you try. So in that light, I'd much rather you do it in code. What specifically do you not understand about my question?

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

    Default

    To reiterate:

    Each cell looks at its eight neighbors and based on their states (alive or dead), it decides whether it will be alive or dead in the next generation. So say you have 9 cells held by the board 2-dimensional array:

    Java Code:
    A    B    C
    
    D    E    F
    
    G    H    I
    If you change A's state, and then test cell B, it will see A's new state, the state it's supposed to be in the next generation, not A's state in the current generation, and this will mess up B's tests to see what it should be in the next generation.

    My suggestion is to iterate through all the cells changing the states in the temp array only, not in the board array. Then after you've set all the temp array's values, loop through the board array again, and this time only set the board array's values using the temp values.

    So you'll be looping through the whole array twice, the first time to check each cell's neighbors and use this to decide what state to use to set the temp array, and then a second time now not testing but simply setting the board array with the temp's values (with the next generation's values).

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

    Default

    Quote Originally Posted by bigskers76 View Post
    thank you for your help
    OK, I see that you've changed your post above from one asking for the code to the note above. Does this mean that you understand what I'm noting and recommended? Does your code now work?

    Edit: never mind. It appears that you've left. Hm.

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

Similar Threads

  1. Coded Life Lesson :)
    By MuslimCoder in forum Forum Lobby
    Replies: 7
    Last Post: 07-29-2009, 08:16 PM
  2. 2d array help- Game of Life
    By n3philim in forum New To Java
    Replies: 2
    Last Post: 07-23-2009, 12:01 AM
  3. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 03:08 AM
  4. Game of Life assignment
    By javan00b in forum New To Java
    Replies: 4
    Last Post: 04-28-2008, 05: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
  •