Skip to content

Instantly share code, notes, and snippets.

@jamessa
Created December 17, 2013 02:48
Show Gist options
  • Select an option

  • Save jamessa/7999167 to your computer and use it in GitHub Desktop.

Select an option

Save jamessa/7999167 to your computer and use it in GitHub Desktop.
# simple Game of Life in Julia
function evolve(N::Int,grid,grid1)
for x = 1:N; for y = 1:N
n = 0
for dx = -2:0; for dy = -2:0
x1 = (x + dx + N) % N + 1 # periodic boundaries
y1 = (y + dy + N) % N + 1
if grid[x1,y1]; n += 1; end
end end
if grid[x,y]; n -= 1; end
grid1[x,y] = (n == 3 || (n == 2 && grid[x,y]))
end end
end
function iterate_grid(N::Int,iter::Int)
grid = randbool(N,N) # start with a random grid
grid1 = Array(Bool,(N,N))
for i=1:iter
evolve(N,grid,grid1)
(grid,grid1) = (grid1,grid) # swap the grids
end
grid
end
@jamessa
Copy link
Copy Markdown
Author

jamessa commented Dec 17, 2013

julia> iterate_grid(10,10)
10x10 BitArray{2}:
false false true false false false false false false false
false true false false false true false true true false
false false true false false true false true true true
false false false true false false false false false true
true true true false false false false false false false
true true true false false false false false true true
true true true false false false true true true true
false false false false false false true true false false
false false false false false false false false false false
false false true false true false false false false false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment