Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help please.... We are going to implement Conway\'s game of life. Refer t

ID: 3740489 • Letter: I

Question

I need help please.... We are going to implement Conway's game of life. Refer to the Wikipedia article for background information. We will be implementing the traditional 4 rules: Any live cell with fewer than two live neighbors dies Any live cell with two or three live neighbors continues to live Any live cell with more than three live neighbors dies Any dead cell with exactly three live neighbors becomes a live cell Your code should have a routine to set the initial state. Your code should use a variable to determine width and length of the "world". Your world will always have a rectangular/square shape, you don't need to worry about any other weird shapes and if you do allow those types of spaces (circular) you need to write the code such that it can accommodate a simple length,width definition.

Explanation / Answer

public class GameOfLife

{

    public static void main(String[] args)

    {

        int M = 10, N = 10;

        // Intiliazing the grid.

        int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

        };

        // Displaying the grid

        System.out.println("Original Generation");

        for (int i = 0; i < M; i++)

        {

            for (int j = 0; j < N; j++)

            {

                if (grid[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

        System.out.println();

        nextGeneration(grid, M, N);

    }

    // Function to print next generation

    static void nextGeneration(int grid[][], int M, int N)

    {

        int[][] future = new int[M][N];

        // Loop through every cell

        for (int l = 1; l < M - 1; l++)

        {

            for (int m = 1; m < N - 1; m++)

            {

                // finding no Of Neighbours that are alive

                int aliveNeighbours = 0;

                for (int i = -1; i <= 1; i++)

                    for (int j = -1; j <= 1; j++)

                        aliveNeighbours += grid[l + i][m + j];

                // The cell needs to be subtracted from

                // its neighbours as it was counted before

                aliveNeighbours -= grid[l][m];

                // Implementing the Rules of Life

                // Cell is lonely and dies

                if ((grid[l][m] == 1) && (aliveNeighbours < 2))

                    future[l][m] = 0;

                // Cell dies due to over population

                else if ((grid[l][m] == 1) && (aliveNeighbours > 3))

                    future[l][m] = 0;

                // A new cell is born

                else if ((grid[l][m] == 0) && (aliveNeighbours == 3))

                    future[l][m] = 1;

                // Remains the same

                else

                    future[l][m] = grid[l][m];

            }

        }

        System.out.println("Next Generation");

        for (int i = 0; i < M; i++)

        {

            for (int j = 0; j < N; j++)

            {

                if (future[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

    }

}

public class GameOfLife

{

    public static void main(String[] args)

    {

        int M = 10, N = 10;

        // Intiliazing the grid.

        int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

        };

        // Displaying the grid

        System.out.println("Original Generation");

        for (int i = 0; i < M; i++)

        {

            for (int j = 0; j < N; j++)

            {

                if (grid[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

        System.out.println();

        nextGeneration(grid, M, N);

    }

    // Function to print next generation

    static void nextGeneration(int grid[][], int M, int N)

    {

        int[][] future = new int[M][N];

        // Loop through every cell

        for (int l = 1; l < M - 1; l++)

        {

            for (int m = 1; m < N - 1; m++)

            {

                // finding no Of Neighbours that are alive

                int aliveNeighbours = 0;

                for (int i = -1; i <= 1; i++)

                    for (int j = -1; j <= 1; j++)

                        aliveNeighbours += grid[l + i][m + j];

                // The cell needs to be subtracted from

                // its neighbours as it was counted before

                aliveNeighbours -= grid[l][m];

                // Implementing the Rules of Life

                // Cell is lonely and dies

                if ((grid[l][m] == 1) && (aliveNeighbours < 2))

                    future[l][m] = 0;

                // Cell dies due to over population

                else if ((grid[l][m] == 1) && (aliveNeighbours > 3))

                    future[l][m] = 0;

                // A new cell is born

                else if ((grid[l][m] == 0) && (aliveNeighbours == 3))

                    future[l][m] = 1;

                // Remains the same

                else

                    future[l][m] = grid[l][m];

            }

        }

        System.out.println("Next Generation");

        for (int i = 0; i < M; i++)

        {

            for (int j = 0; j < N; j++)

            {

                if (future[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote