Need help with Game of Life generations
I'm making a simple game of life 10x10 board using dashes, and filling them in with x's when the user inputs the corresponding row and column. I have everything I need for this except option 3, which is to 'show the next generation' by these rules:
- A new cell is born on an empty square if it is surrounded by exactly three neighbors
- A cell dies of overcrowding if it is surrounded by four or more neighbors
- It dies of loneliness if it is surrounded by zero or one neighbor
- If a cell has two or three neighbors, it survives to the next generation
Code :
import java.util.Scanner;
public class GameOfLife
{
public static void main (String[] args) {
final int ROWS = 10;
final int COLUMNS = 10;
Scanner scan = new Scanner(System.in);
String[][] board = new String[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = "-";
boolean done = false;
while (!done) {
System.out.println("1 - Add a being");
System.out.println("2 - Show current board");
System.out.println("3 - Show next generation");
System.out.println("4 - Clear board");
System.out.println("5 - Exit");
int option = scan.nextInt();
if (option == 1) {
System.out.print("Enter row for being (1 through 9): ");
int i = scan.nextInt();
System.out.print("Enter column for being: ");
int j = scan.nextInt();
board[i][j] = "x";
}
else if (option == 2) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++)
System.out.print(board[i][j]);
System.out.println();
}
}
else if (option == 3) {
int neighbors = 0;
int x = ROWS, y = COLUMNS;
if (x >= 1 && y >=1 && x < COLUMNS && y < ROWS) {
if (board[x][y++].equals(1)) {neighbors++;}
if (board[x][y--].equals(1)) {neighbors++;}
if (board[x++][y].equals(1)) {neighbors++;}
if (board[x++][y++].equals(1)) {neighbors++;}
if (board[x++][y--].equals(1)) {neighbors++;}
if (board[x--][y--].equals(1)) {neighbors++;}
if (board[x--][y++].equals(1)) {neighbors++;}
}
}
else if (option == 4) {
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = "-";
}
else if (option == 5) {
done = true;
}
else System.out.println("Invalid input.");
}
System.exit(0);
}
}
I have it so it finds the neighbors but I'm not sure how to go about deleting or keeping certain x's.
For example, how to go from this:
Code :
----------
----------
----------
---xxx----
----------
----------
----------
----------
----------
----------
To this:
Code :
----------
----------
----x-----
----x-----
----x-----
----------
----------
----------
----------
----------
Re: Need help with Game of Life generations
Quote:
how to go about deleting or keeping certain x's.
Use the rules of the game to change the contents of a slot depending on what neighbors it has.
Perhaps you need two boards: current and next. Use the current board to set the xs in the next board. Then set current to next.
Re: Need help with Game of Life generations
Quote:
Originally Posted by
Norm
Use the rules of the game to change the contents of a slot depending on what neighbors it has.
Perhaps you need two boards: current and next. Use the current board to set the xs in the next board. Then set current to next.
I have a current board (which is actually just option 2), and I tried just copying that again and using if statements according to the rules, but that's where I'm not sure of how to start it.
Re: Need help with Game of Life generations
Use the items on the current board to set the items on the next board.
Re: Need help with Game of Life generations
Changed my else if for option 3 and now I'm getting this error on the if statements inside of option 3:
java.lang.ArrayIndexOutOfBoundsException: 10
Code :
/**
* Program the mathematical game called The Game of Life.
*
* @author Marcus Stone
* @version 11-27-2012
*/
import java.util.Scanner;
public class GameOfLife
{
public static void main (String[] args) {
final int ROWS = 10;
final int COLUMNS = 10;
Scanner scan = new Scanner(System.in);
String[][] board = new String[ROWS][COLUMNS];
String[][] nextGen = new String[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = "-";
boolean done = false;
while (!done) {
System.out.println("1 - Add a being");
System.out.println("2 - Show current board");
System.out.println("3 - Show next generation");
System.out.println("4 - Clear board");
System.out.println("5 - Exit");
int option = scan.nextInt();
if (option == 1) {
System.out.print("Enter row for being (1 through 8): ");
int i = scan.nextInt();
System.out.print("Enter column for being (1 through 8): ");
int j = scan.nextInt();
board[i][j] = "x";
}
else if (option == 2) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++)
System.out.print(board[i][j]);
System.out.println();
}
}
else if (option == 3) {
for (int i = 1; i <= ROWS; i++) {
for (int j = 1; j <= COLUMNS; j++) {
if (board[i][j] == "x") {
int count = 0; {
if (board[i][j - 1] == "x")
count++;
if (board[i][j + 1] == "x")
count++;
if (board[i - 1][j] == "x")
count++;
if (board[i - 1][j - 1] == "x")
count++;
if (board[i - 1][j + 1] == "x")
count++;
if (board[i + 1][j - 1] == "x")
count++;
if (board[i + 1][j] == "x")
count++;
if (board[i + 1][j + 1] == "x")
count++;
}
if (count == 2 || count == 3)
board[i][j] = "x";
else
board[i][j] = "-";
}
if (board[i][j] == "-") {
int nextCount = 0; {
if (board[i][j - 1] == "x")
nextCount++;
if (board[i][j + 1] == "x")
nextCount++;
if (board[i - 1][j] == "x")
nextCount++;
if (board[i - 1][j - 1] == "x")
nextCount++;
if (board[i - 1][j + 1] == "x")
nextCount++;
if (board[i + 1][j - 1] == "x")
nextCount++;
if (board[i + 1][j] == "x")
nextCount++;
if (board[i + 1][j + 1] == "x")
nextCount++;
}
if (nextCount == 3)
board[i][j] = "x";
}
System.out.print(board[i][j]);
}
}
}
else if (option == 4) {
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = "-";
}
else if (option == 5) {
done = true;
}
else System.out.println("Invalid input.");
}
System.exit(0);
}
}
Re: Need help with Game of Life generations
Quote:
java.lang.ArrayIndexOutOfBoundsException: 10
That means that program is using an index that is past the end of the array.
Remember that array indexes go from 0 to the array length-1
Make sure the index's value never goes past the array's length-1