Java Program In 1970, mathematician John Conway developed the Game of Life (no,
ID: 3806754 • Letter: J
Question
Java Program
In 1970, mathematician John Conway developed the Game of Life (no, not the board game). Imagine a grid of cells, each cell in one of two states: alive or dead. Each cell interacts with its eight neighbors so that, over time, life in the cells will either begin or end, following a simple set of rules: 1. Any live cell with fewer than two live neighbors will die (from loneliness, perhaps) 2. Any live cell with two or three live neighbors remains alive 3. Any live cell with more than three live neighbors will die (from overcrowding, perhaps) 4. Any dead cell with exactly three live neighbors will become alive When the main program begins, the user will input the number of time steps to be shown, the size of the grid (always square), and then the initial grid values. The size of the grid will be determined by the original input and will not change while the program is running. For simplicity, we will assume that any neighboring cells beyond the edges of the grid are dead. In the initial values and in the output, use "-" to indicate dead cells and "O" to indicate live cells (although you may of course represent these internally however you like (a boolean array, for example)). Running the program will look like the examples below.Explanation / Answer
This is Main as well as Driver class:
package com.GameOfLife;
import java.util.Scanner;
public class GameOfLife {
public static void main(String[] args){
System.out.println("Game of Life ");
Scanner s = new Scanner(System.in);
System.out.print("How many step in time?: ");
int gens=s.nextInt();
System.out.print("What is size of grid?: ");
int size=s.nextInt();
String dish1=s.next();
String[] dish=new String[size];
for(int j = 0; j <dish.length;j++)
{
//System.out.println(j);
//System.out.print("Enter initial grid:");
dish[j] = s.next();
}
/*String[] dish= {
"_#_",
"_#_",
"_#_",};
int gens= 3;*/
for(int i= 1;i <= gens;i++){
System.out.println("After " + i + " steps:");
print(dish);
dish= life(dish);
}
}
public static String[] life(String[] dish){
String[] newGen= new String[dish.length];
for(int row= 0;row < dish.length;row++){//each row
newGen[row]= "";
for(int i= 0;i < dish[row].length();i++){//each char in the row
String above= "";//neighbors above
String same= "";//neighbors in the same row
String below= "";//neighbors below
if(i == 0){//all the way on the left
//no one above if on the top row
//otherwise grab the neighbors from above
above= (row == 0) ? null : dish[row - 1].substring(i,
i + 2);
same= dish[row].substring(i + 1, i + 2);
//no one below if on the bottom row
//otherwise grab the neighbors from below
below= (row == dish.length - 1) ? null : dish[row + 1]
.substring(i, i + 2);
}else if(i == dish[row].length() - 1){//right
//no one above if on the top row
//otherwise grab the neighbors from above
above= (row == 0) ? null : dish[row - 1].substring(i - 1,
i + 1);
same= dish[row].substring(i - 1, i);
//no one below if on the bottom row
//otherwise grab the neighbors from below
below= (row == dish.length - 1) ? null : dish[row + 1]
.substring(i - 1, i + 1);
}else{//anywhere else
//no one above if on the top row
//otherwise grab the neighbors from above
above= (row == 0) ? null : dish[row - 1].substring(i - 1,
i + 2);
same= dish[row].substring(i - 1, i)
+ dish[row].substring(i + 1, i + 2);
//no one below if on the bottom row
//otherwise grab the neighbors from below
below= (row == dish.length - 1) ? null : dish[row + 1]
.substring(i - 1, i + 2);
}
int neighbors= getNeighbors(above, same, below);
if(neighbors < 2 || neighbors > 3){
newGen[row]+= "_";//<2 or >3 neighbors -> die
}else if(neighbors == 3){
newGen[row]+= "#";//3 neighbors -> spawn/live
}else{
newGen[row]+= dish[row].charAt(i);//2 neighbors -> stay
}
}
}
return newGen;
}
public static int getNeighbors(String above, String same, String below){
int ans= 0;
if(above != null){//no one above
for(char x: above.toCharArray()){//each neighbor from above
if(x == '#') ans++;//count it if someone is here
}
}
for(char x: same.toCharArray()){//two on either side
if(x == '#') ans++;//count it if someone is here
}
if(below != null){//no one below
for(char x: below.toCharArray()){//each neighbor below
if(x == '#') ans++;//count it if someone is here
}
}
return ans;
}
public static void print(String[] dish){
for(String s: dish){
System.out.println(s);
}
}
}
Output:
Game of Life
How many step in time?: 1
What is size of grid?: 5
-
-----
--o--
--o--
--o--
-----
After 1 steps:
-----
--o--
--o--
--o--
-----
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.