import java.util.Arrays;
public class GameOfLife {
private static final int ROWS = 10;
private static final int COLS = 10;
private static int[][] GRID = new int[ROWS][COLS];
public static void main(String[] args) {
GRID[4][4] = 1;
GRID[4][5] = 1;
GRID[4][6] = 1;
GRID[5][6] = 1;
GRID[6][5] = 1;
printGrid();
for (int i = 0; i < 10; i++) {
nextGeneration();
printGrid();
}
}
private static void nextGeneration() {
int[][] nextGrid = new int[ROWS][COLS];
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
int aliveNeighbors = countAliveNeighbors(row, col);
if (GRID[row][col] == 1) {
if (aliveNeighbors == 2 || aliveNeighbors == 3) {
nextGrid[row][col] = 1;
}
} else {
if (aliveNeighbors == 3) {
nextGrid[row][col] = 1;
}
}
}
}
GRID = nextGrid;
}
private static int countAliveNeighbors(int row, int col) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int r = row + i;
int c = col + j;
if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {
if (GRID[r][c] == 1 && !(i == 0 && j == 0)) {
count++;
}
}
}
}
return count;
}
private static void printGrid() {
for (int[] row : GRID) {
for (int cell : row) {
System.out.print(cell == 1 ? "* " : " ");
}
System.out.println();
}
System.out.println();
}
}