Last active
April 15, 2021 19:33
-
-
Save genkuroki/9e2ba18be6d9d95ba1c11be43c2c7e8d to your computer and use it in GitHub Desktop.
GameOfLife.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Pkg | |
Pkg.add("Plots") | |
Pkg.add("ProgressMeter") | |
previous_GKSwstype = get(ENV, "GKSwstype", "") | |
ENV["GKSwstype"] = "100" | |
string2life(S; living='o') = ((S |> split .|> collect .|> permutedims | |
|> A -> vcat(A...)) .|> c -> ifelse(c == 'o', Int8(1), Int8(0))) | |
life2string(L; living='o', empty='.') = ((L .|> s -> ifelse(!iszero(s), living, empty)) | |
|> S -> (String(S[i, :])*"\n" for i in axes(S, 1)) |> prod) | |
S = """ | |
ooo.o | |
o.... | |
...oo | |
.oo.o | |
o.o.o | |
"""; | |
using Plots, ProgressMeter, Random | |
m = n = 200 | |
L = 2000 | |
prog = Progress(L, 0); | |
v = zeros(Int8, m, n); | |
u = zero(v); | |
A = string2life(S) | |
x0, y0 = 148, 148; | |
x1, y1 = x0 + size(A, 1) - 1, y0 + size(A, 2) - 1; | |
u[x0:x1, y0:y1] .= A; | |
function lifegame!(v, u, m, n) | |
for j in 2:n-1, i in 2:m-1 | |
nn = u[i-1,j-1] + u[i,j-1] + u[i+1,j-1] + | |
u[i-1,j ] + u[i+1,j ] + | |
u[i-1,j+1] + u[i,j+1] + u[i+1,j+1] | |
v[i,j] = nn == 3 ? 1 : nn == 2 && !iszero(u[i,j]) ? 1 : 0 | |
end | |
end | |
anim = @animate for t in 1:L | |
heatmap(u; size=(440, 440), colorbar=false, frame=false, ticks=false) | |
lifegame!(v, u, m, n) | |
u .= v | |
next!(prog) | |
end | |
gif(anim, "GameOfLife.gif") |
Author
genkuroki
commented
Apr 3, 2021
For the setting ENV["GKSwstype"] = 100 or ENV["GKSwstype"] = "null", see https://discourse.julialang.org/t/deactivate-plot-display-to-avoid-need-for-x-server/19359/2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment