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 [ ]:
# Размеры мира
x = 350;
y = 350;
# Создание мира
World = falses(x, y);
World[3:end-2, 3:end-2] .= true;
# Инициализация вспомогательных параметров
runGame = true; # Условие запуска/остановки цикла обработки мира
count_steps = 0; # Счётчик кол-во шагов в цикле
num_step = 1000; # Кол-во шагов цикла
The cycle of processing and visualizing the world.
In [ ]:
@gif while runGame
global World, runGame, x, y, count_steps, num_step; # Объявление переменных как глобальные.
# Отрисовка мира.
heatmap(World, size=(x,y), aspect_ratio=:equal, cbar=:none, axis=nothing, border=:none, c = :blues)
# Инициализация нового состояния мира.
WorldNew = falses(x, y);
# Обработка каждой клетки в мире.
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)] # Выделение окружения клетки.
Near[2, 2] = false # Выкалывание обрабатываемой клетки.
NearSum = sum(Near) # Расчёт кол-ва клеток в окружении.
# Обработка дальнейшего развития клетки, опираясь на правила описанные выше.
if p
if (NearSum == 2) || (NearSum == 3)
p = true
else
p = false
end
else
if NearSum == 3
p = true
end
end
# Сохранение обработанной клетки в новое состояние Мира.
WorldNew[i, j] = p
end
end
# Проверка условия выхода из цикла обработки.
if count_steps > num_step
runGame = false
end
# Счётчик шагов
count_steps += 1
# Сохранение нового состояния мира для следующей итерации цикла.
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.
