The Game of Life¶
This is a cellular automaton, a playerless game in which the user creates an initial state and then only watches it evolve. In the game it is possible to create processes with Turing completeness, which allows to realise any Turing machine.
Rules
- In an empty cell with three living cells neighbouring it, life originates.
- If a living cell has two or three living neighbours, that cell continues to live
- If there are less than two or more than three living neighbours, the cell dies.
Implemented algorithm¶
Installation and connection of libraries.
If it is necessary 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
Initialisation 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; # Кол-во шагов цикла
Cycle of world processing and visualisation.
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 this example we have shown you the possibilities of Engee in displaying the work of algorithms in dynamics, we have also shown you the possibilities of monitoring the states inside the loops and we have shown how to visualise these results.