## Game Of Life Notes

>>>> Game of Life Due Next Wednesday <<<<

### Project Euler 17

• lots of special cases.

### Project Euler 24

• One option: 9 nested loops
• counter for something
• only 1 million things, ints will work.
• Better option: recursive calls.

### Game of Life

• Based off of Conway's game of life.
• See the rules on the assingment page.

You'll need two grids: the current generation and the next generation. The next gen will be computed off of the current gen.

#### GUI Ideas

• Arrays of buttons, etc.

#### Toroidal Arrays

• necessary part of the program (the array wraps around)
``````// for A[0][0], to find the neighboors to the left.
neighboorLeft = A[r][c-1] if c > 0
neighboorLeft =   A[r][c+A[0].length - 1] otherwise

// or better way:

neighboorLeft =  A[r][(c + A[0].length - 1) % A[0].length];
A[r][(c+1) % A[0].length]
``````

#### Example method to find all live neighboors:

``````// for an array of booleans
// called by the
public int liveNeighboors(boolean[][] A, int r, int c)
{
for(int row = r-1; row <= r+1; row++) {
for (int col = c-1; col <= c+1; col++) {
int nr = (row + A.length) % A.length;
int nc = (col + A[0].length) % A[0].length;
if (A[nr][nc] == true) count ++;
}
}
if (A[r][c]) count--;
return count;
}
``````

#### ActionPerformed

``````actionPerformed(   )
{
if (source == timer) {
computeGenerations(); // key routine, probably should return void.
repaint();
}
}

public void computeGeneration(Boolean[][] A, Boolean[][] B)
{
for (int r=0; r<A.length; r++) {
for (int c=0; c<A[r].length; r++) {
int ln = liveNeighboors(A,r,c);
boolean isLive = A[r][c];
if (isLive) {
if (ln <= 2 || ln > 3) B[r][c] = false;
else B[r][c] = true;
else {
if (ln == 3) B[r][c]

}
``````

#### Timer

in the constructor:

``````animationTimer = new javax.swing.Timer(30, this);
animationTimer.netInitialDelay(50);
animationTimer.start();
``````

paintComponent method:

``````public void paintComponent
``````