import java.util.Arrays;

//Show me how to write Java code which demonstrates a flyer in Conway's game of life.

//Here is an example of how you could write a Java program that demonstrates a flyer in Conway's Game of Life:

/*
This program sets up a grid with a flyer pattern, which is a simple pattern that oscillates 
between two different states. The nextGeneration method updates the grid according to the 
rules of Conway's Game of Life: cells that are alive and have 2 or 3 alive neighbors stay 
alive, cells that are dead and have 3 alive neighbors come to life, and all other cells die. 
The countAliveNeighbors method counts the number of alive cells in the 8 surrounding cells 
of a given cell. The printGrid method is used to print the grid after each generation, so 
you can see the evolution of the flyer pattern.

 */

public class GameOfLife {
    private static final int ROWS = 10;
    private static final int COLS = 10;
    //private static final int[][] GRID = new int[ROWS][COLS];
    private static  int[][] GRID = new int[ROWS][COLS];

    public static void main(String[] args) {
        // Initialize the grid with a flyer pattern
        GRID[4][4] = 1;
        GRID[4][5] = 1;
        GRID[4][6] = 1;
        GRID[5][6] = 1;
        GRID[6][5] = 1;

        // Print the initial grid
        printGrid();

        // Run the simulation for 10 generations
        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) {
//            System.out.println(Arrays.toString(row));
//        }
//        System.out.println();
//    }
    
    //can you make the printGrid method produce more readable output?
    private static void printGrid() {
        for (int[] row : GRID) {
            for (int cell : row) {
                System.out.print(cell == 1 ? "* " : "  ");
            }
            System.out.println();
        }
        System.out.println();
    }
}