The game "Life"
This is a cellular automaton, a game without players, in which the user creates an initial state, and then only watches its development. In the game, you can create processes with Turing completeness, which allows you to implement any Turing machine.
Rules
- In an empty cell, with three living cells adjacent, life begins.
- If a living cell has two or three living neighbors, then this cell continues to live.
- If there are fewer than two or more than three living neighbors, the cell dies.
Implemented algorithm
Installing and connecting libraries.
If you need to install libraries, assign the install parameter a value of one.
In [ ]:
Pkg.add(["Animations"])
In [ ]:
install = 1;
if install == 1
using Pkg
Pkg.add("Animations")
end
Connecting libraries.
In [ ]:
using Plots
using FileIO
using Images
using Animations
Initialization of algorithm parameters.
In [ ]:
# The size of the world
x = 350;
y = 350;
# Creating the world
World = falses(x, y);
World[3:end-2, 3:end-2] .= true;
# Initialization of auxiliary parameters
runGame = true; # The condition for starting/stopping the world processing cycle
count_steps = 0; # Counter for the number of steps in a cycle
num_step = 1000; # Number of cycle steps
The cycle of processing and visualizing the world.
In [ ]:
@gif while runGame
global World, runGame, x, y, count_steps, num_step; # Declaring variables as global.
# Rendering the world.
heatmap(World, size=(x,y), aspect_ratio=:equal, cbar=:none, axis=nothing, border=:none, c = :blues)
# Initialization of a new state of the world.
WorldNew = falses(x, y);
# Processing every cell in the world.
for i in 2:x-1
for j in 2:y-1
p = World[i, j]
Near = World[(i-1):(i+1), (j-1):(j+1)] # Highlighting the cell environment.
Near[2, 2] = false # Puncturing of the treated cell.
NearSum = sum(Near) # Calculation of the number of cells in the environment.
# Processing of further cell development based on the rules described above.
if p
if (NearSum == 2) || (NearSum == 3)
p = true
else
p = false
end
else
if NearSum == 3
p = true
end
end
# Saving the treated cell to a new state of the World.
WorldNew[i, j] = p
end
end
# Checking the exit condition from the processing cycle.
if count_steps > num_step
runGame = false
end
# Step Counter
count_steps += 1
# Saving the new state of the world for the next iteration of the loop.
World = WorldNew
end
Out[0]:
Conclusion
Based on the results of the implementation of this example, we demonstrated to you the capabilities of Engee in displaying the operation of algorithms in dynamics, also showed the capabilities of monitoring states within cycles and showed how to visualize these results.
