How to prevent using global arrays in iteration in the module

No.

The function LifeGame.gif!(board, niters; ...) in my example contains the for loop updating niters times the global variables initialized by boardrandom = LifeGame.randboard() and board5x5 = LifeGame.fivexfive() outside the module LifeGame and plotting each step to gif animations.

In the module LifeGame:

Outside the module LifeGame:

LifeGame.update!(board, niters) is not used in the example, but can update a LifeGame.Board type variable without plotting.

In the module LifeGame:

The array tmp, which is used as a temporary workspace, is created by the constructor of LifeGame.Board objects.

Example of LifeGame.update!: Glider

Updating a global variable with a for loop can be done by passing it as an argument to a function containing a for loop.

state = Int8[
    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 1 1 0 0 0 0
    1 0 1 0 0 0 0
    0 0 1 0 0 0 0
]
board = LifeGame.Board(state)
board.state
7×7 Matrix{Int8}:
 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  1  1  0  0  0  0
 1  0  1  0  0  0  0
 0  0  1  0  0  0  0


↓ update the global variable board 12 times
↓ by the for loop in the function LifeGame.update!

LifeGame.update!(board, 12)
board.state
7×7 Matrix{Int8}:
 0  0  0  0  0  0  0
 0  0  0  0  1  1  0
 0  0  0  1  0  1  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


↓ update and animate the global variable board

LifeGame.gif!(board, 27; gifname="lifeglider.gif", fps=5)

image

Have fun!

Jupyter notebook: https://github.com/genkuroki/public/blob/main/0014/Game%20of%20Life.ipynb