C
CPSCmajor
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[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[j].equals(" "))
board[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[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[j].equals("X"))
if (board[j-1].equals("X") &&
board[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[j] = 1; j <= 15; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 2) // Sets up configuration b
{
for (board[j] = 1; j <= 16; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 3) // Sets up configuration c
{
for (board[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[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[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[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[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[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[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[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[j] = 1; j <= 13; j++)
g.set(i, j);
System.out.print(g.toString());
}
}
}
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[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[j].equals(" "))
board[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[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[j].equals("X"))
if (board[j-1].equals("X") &&
board[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[j] = 1; j <= 15; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 2) // Sets up configuration b
{
for (board[j] = 1; j <= 16; j++)
g.set(i, j);
System.out.print(g.toString());
}
else if (config == 3) // Sets up configuration c
{
for (board[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[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[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[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[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[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[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[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[j] = 1; j <= 13; j++)
g.set(i, j);
System.out.print(g.toString());
}
}
}