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

The Game of Life is a 2-dimensional cellular automaton. However there are also 1

ID: 663344 • Letter: T

Question

The Game of Life is a 2-dimensional cellular automaton. However there are also 1-dimensional cellular automata, where the world is simply a 1-dimensional strip of cells, each of which is either occupied or vacant. A 15 cell world is shown below with marks signifying occupied cells. Each cell has just two neighbours which are the cells one step to the right or left of it. We assume that the strip wraps around so that the left-hand edge is joined to the right-hand edge (forming a circular strip). At each time-step the world evolves according to the following rules: any cell with 0 or 2 occupied neighbours remains in its current state, while any cell with a single occupied neighbours changes state (that is, from occupied to unoccupied or vice versa). For example, for the configuration shown above, each cell has the following number of occupied neighbours. and so applying the rule to all cells simultaneously the world evolves to its next state: Write a method that takes a boolean array representing the current state of the cellular automaton and computes and returns a boolean array representing the next generation.

Explanation / Answer

class GameOfLife { static int countSurrounding(int[][] board, int a, int b) { int count = 0; int[][] surrounding = {{a - 1, b - 1}, {a - 1, b }, {a - 1, b + 1}, {a , b - 1}, {a , b + 1}, {a + 1, b - 1}, {a + 1, b }, {a + 1, b + 1}}; for (int i[]: surrounding) { try { if (board[i[0]][i[1]] == 1) { count++; } } catch (ArrayIndexOutOfBoundsException e) {} } return count; } static void printBoard(int[][] board) { for (int[] i: board) { for (int j: i) { if (j == 1) { System.out.print("#"); } else { System.out.print("."); } } System.out.println(); } System.out.println(); } public static void main(String[] args) { int[][] nextBoard = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 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, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; int[][] board = new int[nextBoard.length][nextBoard[0].length]; for (int gen = 0; gen < 25; gen++) { for (int i = 0; i
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