From: CPSCmajor on
Hello,
My assignment is to write a program that simulates the game of life
with a multidimensional array over 25 generations. I got the initial
configurations entered in, but I can't think of how to make the method
for the future generations work. The rules are that if a living counter
(X in my program) is surrounded by 2 or 3 neighbors, it survives. Each
counter surrounded by 4 or more dies, and so does each counter with 1
or no neighbors. A birth occurs in each empty cell that is adjacent
exactly to 3 cells. I am not asking for someone to write the code for
me...so please don't misunderstand me. I just need help on how to write
a method or way to check for these conditions for each of the different
configurations without having to go through and actually code each
generation out. Any ideas are greatly appreciated, this is due
Monday...Thanks!

Here is my code so far, and there are 12 configurations.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;

public class GameOfLife
{
private String[][] board;
private static final int ROWS = 30;
private static final int COLUMNS = 30;
// Constructs an empty board.
public GameOfLife()
{
board = new String[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = " ";
}
/**
Sets a field in the board. The field must be unoccupied.
@param i the row index
@param j the column index
*/
public void set(int i, int j)
{
if (board[i][j].equals(" "))
board[i][j] = "X"; // Sets up the configuration
}

/**
Creates a string representation of the board, such as
| |
| |
| |
@return the string representation
*/
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}
/**
*This was where I attempted to start the method to check for the
generations which I *realized would involve coding for every single
one.
*/
public void nextGen(int i, int j)
{
int x;
for (x = 0; x <= 25; x++)
{
if (board[i][j].equals("X"))
if (board[i][j-1].equals("X") &&
board[i][j+1].equals("X") && board[i-1][j].equals("X") &&
board[i-1][j-1].equals("X") && board[i-1][j+1].equals("X") &&
board[i+1][j].equals("X") && board[i+1][j-1].equals("X") &&
board[i+1][j+1].equals("X"))

}

}
public static void main(String[] args)
{
GameOfLife g = new GameOfLife(); // Creates a new Game of Life
Scanner s = new Scanner(System.in);
System.out.print("Enter a configuration: "); // Allows for the
user to choose the setup position
int config = s.nextInt();
int i = 12;
int j = 12;
int board[][] = new int[ROWS][COLUMNS];
System.out.print(g.toString());

if (config == 1) // Sets up configuration a
{
for (board[i][j] = 1; j <= 15; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 2) // Sets up configuration b
{
for (board[i][j] = 1; j <= 16; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 3) // Sets up configuration c
{
for (board[i][j] = 1; j <= 17; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 4) // Sets up configuration d
{
g.set(i-1, j+1);
System.out.print(g.toString());

g.set(i, j+2);
System.out.print(g.toString());

g.set(i, j);
System.out.print(g.toString());

g.set(i+1,j+1);
System.out.print(g.toString());
}
else if (config == 5) // Sets up configuration e
{
g.set(i-1, j+1);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 6) // Sets up configuration f
{
g.set(i, j+4);
g.set(i, j+5);
g.set(i, j+6);
System.out.print(g.toString());
for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 7) // Sets up configuration g
{
g.set(i, j+5);
g.set(i, j+6);
g.set(i, j+7);
g.set(i, j+8);
System.out.print(g.toString());
for (board[i][j] = 1; j <= 15; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 8) // Sets up configuration h
{
g.set(i-2, j+1);
g.set(i-1, j+2);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 9) // Sets up configuration i
{
g.set(i-1, j+1);
g.set(i-1, j+2);
g.set(i-1, j+3);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 10) // Sets up configuration j
{
g.set(i-1, j);
g.set(i-1, j+2);
g.set(i+1, j);
g.set(i+1, j+2);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 11) // Sets up configuration k
{
g.set(i-2, j);
g.set(i-1, j);
g.set(i-2, j+2);
g.set(i-1, j+2);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 14; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 12) // Sets up configuration l
{
g.set(i-1, j);
g.set(i-1, j+1);
g.set(i+1, j+2);
g.set(i+1, j+3);
g.set(i+2, j+2);
g.set(i+2, j+3);
System.out.print(g.toString());

for (board[i][j] = 1; j <= 13; j++)
g.set(i, j);
System.out.print(g.toString());
}



}
}

From: Niels Dybdahl on
> My assignment is to write a program that simulates the game of life
> with a multidimensional array over 25 generations. I got the initial
> configurations entered in, but I can't think of how to make the method
> for the future generations work. The rules are that if a living counter
> (X in my program) is surrounded by 2 or 3 neighbors, it survives. Each
> counter surrounded by 4 or more dies, and so does each counter with 1
> or no neighbors. A birth occurs in each empty cell that is adjacent
> exactly to 3 cells. I am not asking for someone to write the code for
> me...so please don't misunderstand me. I just need help on how to write
> a method or way to check for these conditions for each of the different
> configurations without having to go through and actually code each
> generation out. Any ideas are greatly appreciated, this is due
> Monday...Thanks!

Loop from -1 to +1 in both x and y-directions for each cell and count the
number of neighbors.
Use two arrays. One for the current generation and one for the next and swap
them when you have calculated the next.
Take care at the borders.

Niels Dybdahl


From: CPSCmajor on
Hey,
I don't know how to write a loop for that, so I guess I will just have
to code in for what it is supposed to display...is there a method to
pause or slow down executions, so that one can see what is going on. I
found the TimerTask class but I didn't work with what I was trying to
do. Any other ideas?

Thanks.

Niels Dybdahl wrote:
> > My assignment is to write a program that simulates the game of life
> > with a multidimensional array over 25 generations. I got the initial
> > configurations entered in, but I can't think of how to make the method
> > for the future generations work. The rules are that if a living counter
> > (X in my program) is surrounded by 2 or 3 neighbors, it survives. Each
> > counter surrounded by 4 or more dies, and so does each counter with 1
> > or no neighbors. A birth occurs in each empty cell that is adjacent
> > exactly to 3 cells. I am not asking for someone to write the code for
> > me...so please don't misunderstand me. I just need help on how to write
> > a method or way to check for these conditions for each of the different
> > configurations without having to go through and actually code each
> > generation out. Any ideas are greatly appreciated, this is due
> > Monday...Thanks!
>
> Loop from -1 to +1 in both x and y-directions for each cell and count the
> number of neighbors.
> Use two arrays. One for the current generation and one for the next and swap
> them when you have calculated the next.
> Take care at the borders.
>
> Niels Dybdahl

From: CPSCmajor on
Hey I figured out how to write the loop, but I am getting an out of
bounds exception that I have tried to fix w/ no luck...

public int countNeighbors(String[][] currentBoard, int x, int y)
{
int neighbors = 0;
if("X".equals(currentBoard[x-1][y-1]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x-1][y]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x-1][y+1]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x][y-1]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x][y+1]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x+1][y-1]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x+1][y]) && x < 30 && y < 30)
neighbors++;
if("X".equals(currentBoard[x+1][y+1]) && x < 30 && y < 30)
neighbors++;
return neighbors;
}

I don't understand how putting the x < 30 and y < 30 doesn't fix
it...Any ideas??

CPSCmajor wrote:
> Hey,
> I don't know how to write a loop for that, so I guess I will just have
> to code in for what it is supposed to display...is there a method to
> pause or slow down executions, so that one can see what is going on. I
> found the TimerTask class but I didn't work with what I was trying to
> do. Any other ideas?
>
> Thanks.
>
> Niels Dybdahl wrote:
> > > My assignment is to write a program that simulates the game of life
> > > with a multidimensional array over 25 generations. I got the initial
> > > configurations entered in, but I can't think of how to make the method
> > > for the future generations work. The rules are that if a living counter
> > > (X in my program) is surrounded by 2 or 3 neighbors, it survives. Each
> > > counter surrounded by 4 or more dies, and so does each counter with 1
> > > or no neighbors. A birth occurs in each empty cell that is adjacent
> > > exactly to 3 cells. I am not asking for someone to write the code for
> > > me...so please don't misunderstand me. I just need help on how to write
> > > a method or way to check for these conditions for each of the different
> > > configurations without having to go through and actually code each
> > > generation out. Any ideas are greatly appreciated, this is due
> > > Monday...Thanks!
> >
> > Loop from -1 to +1 in both x and y-directions for each cell and count the
> > number of neighbors.
> > Use two arrays. One for the current generation and one for the next and swap
> > them when you have calculated the next.
> > Take care at the borders.
> >
> > Niels Dybdahl

From: Jim Korman on
On 20 Nov 2005 14:13:52 -0800, "CPSCmajor" <bmerritt1987(a)gmail.com>
wrote:

>Hey I figured out how to write the loop, but I am getting an out of
>bounds exception that I have tried to fix w/ no luck...
>
> public int countNeighbors(String[][] currentBoard, int x, int y)
> {
> int neighbors = 0;
> if("X".equals(currentBoard[x-1][y-1]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x-1][y]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x-1][y+1]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x][y-1]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x][y+1]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x+1][y-1]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x+1][y]) && x < 30 && y < 30)
> neighbors++;
> if("X".equals(currentBoard[x+1][y+1]) && x < 30 && y < 30)
> neighbors++;
> return neighbors;
> }
>
>I don't understand how putting the x < 30 and y < 30 doesn't fix
>it...Any ideas??
Look close at the Exception. I'll bet that you're throwing the
exception on x == 0 and y == 0.

Jim