Created
January 30, 2015 03:06
-
-
Save kblake/a99bf6b3c549780e7523 to your computer and use it in GitHub Desktop.
Cell server - Conway's Game of Life
This file contains hidden or 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
IO.puts "Conway's Game of Life" | |
defmodule Cell do | |
def next_cycle do | |
receive do | |
{sender, number_live_neighbors} -> | |
cond do | |
number_live_neighbors < 2 -> | |
send sender, {:died, die_off} | |
number_live_neighbors > 3 -> | |
send sender, {:died, die_off} | |
number_live_neighbors == 2 -> | |
send sender, {:lived, live_on} | |
number_live_neighbors == 3 -> | |
send sender, {:lived, live_on} | |
end | |
end | |
end | |
def die_off do | |
"Bury me" | |
end | |
def live_on do | |
"party time" | |
end | |
end | |
defmodule App do | |
def run(cell, args) do | |
send cell, args | |
receive do | |
{:died, message} -> | |
IO.puts message | |
{:lived, message} -> | |
IO.puts message | |
after 3000 -> | |
IO.puts "bye bye cell" | |
end | |
end | |
end | |
Enum.each([5, 1, 2, 3], fn(number_live_neighbors) -> | |
cell = spawn(Cell, :next_cycle, []) | |
App.run(cell, {self, number_live_neighbors}) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment