Example code in Java for Conway’s Game of Life

With knowledge of their neighbors and the ability to compute the next state, cells could transmit information about only the altered cells and exclude the rest of the field from calculation. The function itself does not possess the ability to recognize that searching for living neighbors beyond the right edge of the map is redundant, hence why such functions have been designed to account for this.


Solution:

To ensure valid entries in the array, the code is using two functions,

min

and

max

. If this is not done, an ArrayOutOfBoundsException will occur when trying to use

-1

,

m

, or

n

as array indexes. These functions encode the fact that the loop doesn’t “know” that it shouldn’t search for living neighbors further to the right given a square at the right edge of the map. The loop control variables,

x

and

y

, are used to iterate over valid squares surrounding the target square.

The variable labeled as

lives

serves as a placeholder to track the number of live neighbors encountered during the loops below. This can be deduced from the fact that it is the output of the function labeled as

liveNeighbors

.

To illustrate, consider an example labeled

liveNeighbors(board,9,9,0,2)

, with

board

being the board specified in the driver. The board is a 9×9 grid, which we denote as

m

and

n

, respectively, and our focus is on the square located at

0

, also known as

2

. This square is situated at the beginning of the third row, with a neighboring square to its right, indicated by

1

. Now, we can proceed with the example.

Using the code

i=0

, we can determine

x = Math.max(i - 1, 0) = Math.max(-1, 0) = 0

. This is important to note because it explains why the

max

function is necessary. If we only relied on

int x=i-1

, we would encounter an issue where the value is beyond the array’s limits, denoted as

x = -1

. Additionally, when evaluating x, we consider the condition x <= Math.min(i + 1, m – 1) = Math.min(1, 8) = 1. In particular, if we were examining a cell in the last column, this rule would enforce the right edge of the array.

The similar reasoning regarding

y

and

j

can be left for you to handle.

The loop simplifies to:

for (int x = 0; x <= 1; x++) {
    for (int y = 1; y <= 3; y++) {
        lives += board[x][y] & 1;
    }
}

The inner loop will iterate six times and generate six pairs of

(x,y)

. It is important to note that these pairs represent the square being investigated and its neighboring squares.

Out of the six squares, five will return

0

, while only one at

(1,2)

will return 1. As a result, by the end of this loop,

lives

will have a value of 1. To conclude,

lives -= board[i][j] & 1;

is executed, which decreases

lives

by 1 if the square being examined contains a 1. However, since

board[i][j]

equals 0 in our case, we subtract 0, resulting in a return value of 1. Finally,

liveNeighbors(board,9,9,0,2) = 1

is returned.

While there may have been a few instances where I mixed up

x

and

y

, I believe that I have provided enough information for you to comprehend the situation.

Frequently Asked Questions