How to resolve the algorithm One-dimensional cellular automata step by step in the Java programming language
How to resolve the algorithm One-dimensional cellular automata step by step in the Java programming language
Table of Contents
Problem Statement
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values. Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm One-dimensional cellular automata step by step in the Java programming language
==> Notes for the first code in Java...
- This Java program simulates the Game of Life, a cellular automaton where cells can live or die based on the number of neighboring cells that are alive.
- The program starts with an initial configuration of cells and evolves it for a specified number of generations.
- It calculates the number of living neighbors for each cell and updates the cell's state based on specific rules.
- The program consists of two methods:
life
andgetNeighbors
. - The
life
method takes the previous generation of cells as input and returns the next generation based on the rules of the Game of Life. - The
getNeighbors
method calculates the number of living neighbors for a given cell.
==> Notes for the second code in Java...
- This code simulates the Game of Life using a more compact and efficient approach.
- It uses a 2D array to represent the cells and a lookup table to determine the state of each cell in the next generation.
- The lookup table is a character array called
trans
that maps the number of living neighbors to the next state of the cell. - The
evolve
method updates the state of each cell in the grid. - It calculates the number of living neighbors for each cell and uses the lookup table to determine the new state.
- The
main
method initializes the grid and repeatedly calls theevolve
method until the grid stabilizes (i.e., no more changes occur).
==> The Game of Life Rules...
In the Game of Life, cells can be either alive or dead. The rules for determining the state of a cell in the next generation are as follows:
- If a living cell has less than two living neighbors, it dies.
- If a living cell has two or three living neighbors, it survives.
- If a living cell has more than three living neighbors, it dies.
- If a dead cell has exactly three living neighbors, it becomes alive.
Source code in the java programming language
public class Life{
public static void main(String[] args) throws Exception{
String start= "_###_##_#_#_#_#__#__";
int numGens = 10;
for(int i= 0; i < numGens; i++){
System.out.println("Generation " + i + ": " + start);
start= life(start);
}
}
public static String life(String lastGen){
String newGen= "";
for(int i= 0; i < lastGen.length(); i++){
int neighbors= 0;
if (i == 0){//left edge
neighbors= lastGen.charAt(1) == '#' ? 1 : 0;
} else if (i == lastGen.length() - 1){//right edge
neighbors= lastGen.charAt(i - 1) == '#' ? 1 : 0;
} else{//middle
neighbors= getNeighbors(lastGen.substring(i - 1, i + 2));
}
if (neighbors == 0){//dies or stays dead with no neighbors
newGen+= "_";
}
if (neighbors == 1){//stays with one neighbor
newGen+= lastGen.charAt(i);
}
if (neighbors == 2){//flips with two neighbors
newGen+= lastGen.charAt(i) == '#' ? "_" : "#";
}
}
return newGen;
}
public static int getNeighbors(String group){
int ans= 0;
if (group.charAt(0) == '#') ans++;
if (group.charAt(2) == '#') ans++;
return ans;
}
}
public class Life{
private static char[] trans = "___#_##_".toCharArray();
private static int v(StringBuilder cell, int i){
return (cell.charAt(i) != '_') ? 1 : 0;
}
public static boolean evolve(StringBuilder cell){
boolean diff = false;
StringBuilder backup = new StringBuilder(cell.toString());
for(int i = 1; i < cell.length() - 3; i++){
/* use left, self, right as binary number bits for table index */
backup.setCharAt(i, trans[v(cell, i - 1) * 4 + v(cell, i) * 2
+ v(cell, i + 1)]);
diff = diff || (backup.charAt(i) != cell.charAt(i));
}
cell.delete(0, cell.length());//clear the buffer
cell.append(backup);//replace it with the new generation
return diff;
}
public static void main(String[] args){
StringBuilder c = new StringBuilder("_###_##_#_#_#_#__#__\n");
do{
System.out.printf(c.substring(1));
}while(evolve(c));
}
}
You may also check:How to resolve the algorithm Long year step by step in the Delphi programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Factor programming language
You may also check:How to resolve the algorithm Arrays step by step in the Halon programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the SequenceL programming language