Simple Game of Life in command line - runtime error

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 11100 Views - Last Post: 30 April 2011 - 11:30 AM Rate Topic: -----

#1 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 01:31 PM

For this assignment we were given a template and told not to alter the code or add new methods.
I split it into two pieces of code, but it is all the same class. The bottom is what I have written myself.

Quote

PROGRAM REQUIREMENT 2 : YOU'RE GOING TO ADD THREE METHODS

Here is a list of the three methods that you must write, as described.

public static void clearGrid ( boolean[][] grid );

This method will simply reset the two-dimensional array that has been passed to it to all false values.

This method shouldn't do any input or output.

public static int countNeighbors ( final boolean[][] grid, final int row,
final int col );

This method counts the number of neighbors in grid for an element at position row and column. For instance, if the method call was

int test = countNeighbors ( thing, 4, 6 );

then you would be counting the neighbors for thing[4][6]. Remember that "neighbors" consist of the 8 cells immediately adjacent to the one being examined.

Make sure to take into account if you are on an edge of the matrix (bottom or top rows, leftmost or rightmost columns), because you will need to make sure you don't try to count the neighbors that would appear off the matrix. The return value of this method should be a number between 0 and 8. There will be no input or output done in this method.

public static void genNextGrid (boolean[][] grid);

This method will actually generate the next generation of the simulation.

It should use the two-dimensional array grid that is passed to it as the "current" generation. It should create a second, temporary two-dimensional array that will hold the "next" generation. You will need to create this temporary matrix to work with as you can't make changes to the current matrix because you would risk losing all of the information you need in order to successfully create the next generation.

This method will need to call the countNeighbors() method you created to help decide if a cell will be alive or dead in the next generation. This method will need to implement the birth/survival/loneliness-overcrowding algorithm discussed earlier in the assignment.

Once you have created the next generation of the simulation into your temporary two-dimensional array, you will need to copy that array back over the current generation (called grid, the method's parameter) so that the next generation can leave the method through the parameter.

This method shouldn't do any input or output.



// The game of life

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

// Change NetID to your NetID
public class dmcelligott_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!



These are the only methods that I can edit

    public static void clearGrid ( boolean[][] grid )
    {
	for (int r=0; r < grid.length; r++)
	{
	   for (int c=0; c < grid[r].length; c++)
	   {
		grid[r][c]=false;
	   }
	}
    }

    public static void genNextGrid ( boolean[][] grid )
    {

	boolean [][] nextgen = new boolean[GRIDSIZE][GRIDSIZE];


	for (int y=0; y <= grid.length; y++)
	{
	   for (int x=0; x <= grid[y].length; x++)
	   {
		if (countNeighbors(grid, y, x) == 3) 
			nextgen[y][x]=true;

		else if (countNeighbors(grid, y, x) == 2)
		{ 
		   if (grid[y][x] == true)
			nextgen[y][x]=true;
		   else
			nextgen[y][x]=false; // lonliness
		}			
		
		else
		   nextgen[y][x]=false; // lonliness or ovecrowding
	   }
	}

	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	      grid[y][x]=nextgen[y][x];
	}
    }

    public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
    {
	int neighbors=0;
	boolean [][] bounds = new boolean[GRIDSIZE +2][GRIDSIZE +2];

	
	for (int y=0; y <= bounds.length; y++)
	{
	   for (int x=0; x <= bounds[y].length; x++)
	   {
		if ((y > 0 && y < bounds.length) && ( x > 0 && x < bounds[y].length))
			bounds[y][x] = grid[y-1][x-1];
		else
			bounds[y][x] = false;
	   }
	   
	}

	for (int y=row -1; y <= row +1; y++) 
	   {
		for (int x=col -1; x<= col + 1; x++)
		   {
			if (bounds[y+1][x+1] != bounds[row][col])
			{
			   if (bounds[y+1][x+1] == true)
				neighbors++;
			}
		   }

	   }
	return neighbors; // return number between 0 - 8
    }
}



clearGrid seems to be fine
This is the ouput and runtime error I recieve:


Quote

Start with a ®andom board, the (q)ueen bee shuttle or the (g)lider pattern? r
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: 20
at dmcelligott_Life1.countNeighbors(dmcelligott_Life1.java:195)
at dmcelligott_Life1.genNextGrid(dmcelligott_Life1.java:159)
at dmcelligott_Life1.main(dmcelligott_Life1.java:49)



This is my last assignment before finals so I am close to my wits end.
All help is appreciated!!


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Game of Life in command line - runtime error

#2 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 01:49 PM

Must be a bad idea to post on such a beautiful day, its 70 degrees and sunny in Nebraska B)
Was This Post Helpful? 0
  • +
  • -

#3 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,740
  • Joined: 13-March 10

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 01:52 PM

What exact problems are you facing?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 01:57 PM

Quote

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20


Which line in the code you have pasted is this?
Was This Post Helpful? 0
  • +
  • -

#5 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 02:09 PM

[quote name='CasiOo' date='28 April 2011 - 01:57 PM' timestamp='1304024233' post='1325150']

Quote

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20

Quote

Which line in the code you have pasted is this?

Actually there is nothing there line 20 is between
import java.util.Random;

public class Life



Also important to note: That is part of the template or "Skeleton program" our teacher calls it, and can't be changed. :pinch:

[quote name='Decibal' date='28 April 2011 - 02:02 PM' timestamp='1304024536' post='1325156']

View PostCasiOo, on 28 April 2011 - 01:57 PM, said:

Quote

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20

Quote

Which line in the code you have pasted is this?

Actually there is nothing there line 20 is between
import java.util.Random;

public class Life



Also important to note: That is part of the template or "Skeleton program" our teacher calls it, and can't be changed. :pinch:


18. import java.util.scanner;
19. import java.util.Random;
20. //blank
21. public class Life

I apologize for the mess of quotations I am new to this.

This post has been edited by Decibal: 28 April 2011 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 02:10 PM

Arrays are indexed from 0 (inclusive) to the length (exclusive) of the array. So your condition should go from i = 0 to i < arrayName.length.
Was This Post Helpful? 2
  • +
  • -

#7 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 02:16 PM

Quote

View Postmacosxnerd101, on 28 April 2011 - 02:10 PM, said:

Arrays are indexed from 0 (inclusive) to the length (exclusive) of the array. So your condition should go from i = 0 to i < arrayName.length.


Do you see that as an issue on any certain line?
I changed all of the i <= arrayName.length to i < arrayName.length
now I am just getting same errors on slightly different lines

This post has been edited by Decibal: 28 April 2011 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 02:21 PM

Post your revised code and error messages.
Was This Post Helpful? 0
  • +
  • -

#9 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 02:25 PM

Revise Code
// The game of life

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[][] grid )
    {
	for (int r=0; r < grid.length; r++)
	{
	   for (int c=0; c < grid[r].length; c++)
	   {
		grid[r][c]=false;
	   }
	}
    }

    public static void genNextGrid ( boolean[][] grid )
    {

	boolean [][] nextgen = new boolean[GRIDSIZE][GRIDSIZE];


	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	   {
		if (countNeighbors(grid, y, x) == 3) 
			nextgen[y][x]=true;

		else if (countNeighbors(grid, y, x) == 2)
		{ 
		   if (grid[y][x] == true)
			nextgen[y][x]=true;
		   else
			nextgen[y][x]=false; // lonliness
		}			
		
		else
		   nextgen[y][x]=false; // lonliness or ovecrowding
	   }
	}

	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	      grid[y][x]=nextgen[y][x];
	}
    }

    public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
    {
	int neighbors=0;
	boolean [][] bounds = new boolean[GRIDSIZE +2][GRIDSIZE +2];

	
	for (int y=0; y < bounds.length; y++)
	{
	   for (int x=0; x < bounds[y].length; x++)
	   {
		if ((y > 0 && y < bounds.length) && ( x > 0 && x < bounds[y].length))
			bounds[y][x] = grid[y-1][x-1];
		else
			bounds[y][x] = false;
	   }
	   
	}

	for (int y=row -1; y <= row +1; y++) 
	   {
		for (int x=col -1; x<= col + 1; x++)
		   {
			if (bounds[y+1][x+1] != bounds[row][col])
			{
			   if (bounds[y+1][x+1] == true)
				neighbors++;
			}
		   }

	   }
	return neighbors; // return number between 0 - 8
    }
}



Quote

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
at dmcelligott_Life.countNeighbors(dmcelligott_Life.java:193)
at dmcelligott_Life.genNextGrid(dmcelligott_Life.java:159)
at dmcelligott_Life.main(dmcelligott_Life.java:49)

Was This Post Helpful? 0
  • +
  • -

#10 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 03:10 PM

I am not sure what I changed, but it is running without runtime error now.
It isn't updating correctly, but I will take a look at it myself for a bit.

Thank you macosxnerd101 and everyone else who posted with good intentions.

Quick forum etiquette question.
If i can't get it to update the next generation correctly should I update this thread or start a new one?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 03:11 PM

Glad I could help. Feel free to update this thread. :)
Was This Post Helpful? 0
  • +
  • -

#12 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 04:16 PM

As I understand the image is supposed to morph and move across the grid, It seems to be staying in the same place.

Here is a screen shot
Attached Image

Quote

An article described what it should be doing:
The Game of Life
"The Queen Bee Shuttle
A very important pattern makes a brief appearance after running the R-pentomino for 774 steps:

It doesn't last long in these stormy surroundings, but you can enter it into the applet by itself and watch what it does. First it moves to the right, but after a few steps, it produces the still life called a beehive and turns back to the left. Then it produces another beehive to its left and turns right again. Conway called this pattern the queen bee shuttle for this reason. Unfortunately, the next time around, it crashes into the first beehive it created."


I must be not declaring all the correct cells to true, but I can't figure it out

This is my Revised Code
// The game of life

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

// Change NetID to your NetID
public class dmcelligott_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!

    // Loops through every cell in 2d array grid setting each value to false
    public static void clearGrid ( boolean[][] grid )
    {
	for (int r=0; r < grid.length; r++)
	{
	   for (int c=0; c < grid[r].length; c++)
	   {
		grid[r][c]=false;
	   }
	}
    }

    // updates choosen grid
    public static void genNextGrid ( boolean[][] grid )
    {

	boolean [][] nextgen = new boolean[GRIDSIZE][GRIDSIZE];

	// Loops throught each cell and sets true depending on neigbors
	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	   {
		// Birth
		if (countNeighbors(grid, y, x) == 3) 
			nextgen[y][x]=true;
		// Survival
		else if ( (countNeighbors(grid, y, x) == 2) && (grid[y][x] == true) )
			nextgen[y][x]=true;
		// Loliness of overcrowding	
		else
		   nextgen[y][x]=false;
	   }
	}

	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	      {grid[y][x]=nextgen[y][x];}
	}
    }

    public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
    {
	int neighbors=0;
	boolean [][] bounds = new boolean[GRIDSIZE +2][GRIDSIZE +2];

	
	for (int y=0; y < bounds.length; y++)
	{

	   for (int x=0; x < bounds[y].length; x++)
	   {
		if ((y > 0 && y < bounds.length -1) && ( x > 0 && x < bounds[y].length -1)) // -1 changed -2
			bounds[y][x] = grid[y-1][x-1];
		else
			bounds[y][x] = false;

	   }
	   
	}

	for (int y=row -1; y <= row +1; y++) 
	   {
		for (int x=col -1; x<= col + 1; x++)
		{
		   if (bounds[y+1][x+1] == true) 
			neighbors++;
		}

	   }

	return neighbors; // return number between 0 - 8
    }
}




I am going to make a sandwich. brb :hungry:
Was This Post Helpful? 0
  • +
  • -

#13 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 04:45 PM

any ideas what is going on here?
Was This Post Helpful? 0
  • +
  • -

#14 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 28 April 2011 - 07:02 PM

Most recent
public class dmcelligott_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!

    // Loops through every cell in 2d array grid setting each value to false
    public static void clearGrid ( boolean[][] grid )
    {
	for (int r=0; r < grid.length; r++)
	{
	   for (int c=0; c < grid[r].length; c++)
	   {
		grid[r][c]=false;
	   }
	}
    }

    // updates choosen grid
    public static void genNextGrid ( boolean[][] grid )
    {

	boolean [][] nextgen = new boolean[GRIDSIZE][GRIDSIZE];

	// Loops throught each cell and sets true depending on neigbors
	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
	   {
		if (grid[y][x] == true)
		   {
			if (countNeighbors(grid, y, x) < 2)
			   nextgen[y][x] = false;

			if (countNeighbors(grid, y, x) > 3)
			   nextgen[y][x] = false;
		   }
		else
		   {
			if (countNeighbors(grid, y, x) == 3)
			   nextgen[y][x] = true;

		   }
	   }
	}

	for (int y=0; y < grid.length; y++)
	{
	   for (int x=0; x < grid[y].length; x++)
		{
		   if (nextgen[y][x] != grid[y][x])
	      		grid[y][x]=nextgen[y][x];
		}
	}
    }

    public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
    {
	int neighbors=0;
	boolean [][] bounds = new boolean[GRIDSIZE +2][GRIDSIZE +2];

	
	for (int y=0; y < bounds.length; y++)
	{

	   for (int x=0; x < bounds[y].length; x++)
	   {
		bounds[y][x]=false;

	   }
	   
	}



	for (int y=1; y < grid.length; y++)
	{

	   for (int x=1; x < grid[y].length; x++)
	   {
		bounds[y][x]=grid[y-1][x-1];
	   }
	   
	}

 	for (int y=row; y <= row +2; y++) 
	   {
		for (int x=col; x<= col + 2; x++)
		{
		   if ( (bounds[y][x] == true) && (bounds[y][x] != bounds[row +1][col +1]) )
			neighbors++;
		}

	   }

	return neighbors; // return number between 0 - 8
    }
}




My glider dies right away, heres my output:

Attached Image

queen bee goes pretty well
Attached Image

but then...

Attached Image

Please look it over and tell me what you think.
Remember I can't add methods and can only edit the bottom 3 methods
Thanks

PS: I am proud to be one of the few people who actually wrote their code before asking for help on this assignment. So help me out would ya? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#15 Decibal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-April 11

Re: Simple Game of Life in command line - runtime error

Posted 29 April 2011 - 11:47 AM

Sorry guys, I just thought that since my problem in the title changed so I was worried people would disregard it.
No one is posting on this thread so what am I to do?
I'm in a bind :sad2:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3