Skip to content

Commit

Permalink
Merge cfc98f6 into 98f3b96
Browse files Browse the repository at this point in the history
  • Loading branch information
simonewebdesign committed Jun 20, 2017
2 parents 98f3b96 + cfc98f6 commit 57fd384
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
29 changes: 15 additions & 14 deletions lib/life/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@ defmodule Life.CLI do

@grid_size 10
@sleep_interval_ms 300

@glider [{1, 0}, {2, 1}, {0, 2}, {1, 2}, {2, 2}]

def main([]) do
"no life"
loop(MapSet.new([]), @grid_size)
end
def main(["glider", "--size=" <> grid_size]) do
loop(MapSet.new(@glider), String.to_integer(grid_size))
end
def main(["glider"]) do
[{3, 0}, {4, 1}, {2, 2}, {3, 2}, {4, 2}]
|> MapSet.new()
|> loop()
loop(MapSet.new(@glider), @grid_size)
end
def main(["--size=" <> grid_size]) do
loop(MapSet.new([]), String.to_integer(grid_size))
end


def loop(alive_cells, interval \\ @sleep_interval_ms) do
defp loop(alive_cells, grid_size, interval \\ @sleep_interval_ms) do
alive_cells
|> calc_grid()
|> calc_grid(grid_size)
|> Table.print()

:timer.sleep(interval)

alive_cells
|> Life.tick()
|> loop()
|> loop(grid_size)
end


def calc_grid(alive_cells, size \\ @grid_size) do
# returns a 2d list. true for alive cells, false for dead
defp calc_grid(alive_cells, size) do
for i <- 0..(size - 1) do
for j <- 0..(size - 1) do
a = for {^j, ^i} <- alive_cells do
true
end
a = for {^j, ^i} <- alive_cells, do: true
case a do
[] -> false
[_] -> true
Expand Down
108 changes: 96 additions & 12 deletions test/life/cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@ defmodule CLITest do
use ExUnit.Case
import ExUnit.CaptureIO

# credits to this website for the ASCII table: http://www.tablesgenerator.com/text_tables
@empty_grid """
# Credits for the ASCII tables: http://www.tablesgenerator.com/text_tables

@empty_grid_10x10 """
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
"""

@empty_grid_3x3 """
╔═══╦═══╦═══╗
║ ║ ║ ║
╠═══╬═══╬═══╣
Expand All @@ -13,7 +38,31 @@ defmodule CLITest do
╚═══╩═══╩═══╝
"""

@grid_with_glider """
@grid_10x10_with_glider """
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ ║ █ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ █ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ █ ║ █ ║ █ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
"""

@grid_3x3_with_glider """
╔═══╦═══╦═══╗
║ ║ █ ║ ║
╠═══╬═══╬═══╣
Expand All @@ -23,21 +72,56 @@ defmodule CLITest do
╚═══╩═══╩═══╝
"""

@tag :skip
test "prints an empty 3x3 grid" do
assert @empty_grid ==
test "with no args, prints an empty 10x10 grid" do
assert @empty_grid_10x10 ==
capture_io(fn ->
assert :ok == Life.CLI.main([])
{:ok, pid} = Task.Supervisor.start_link()
task = Task.Supervisor.async(pid, fn ->
args = []
Task.start_link(Life.CLI.main(args))
end)
:timer.sleep(1)
Task.shutdown(task)
end)
end

@tag :skip
test "prints a 3x3 grid with a glider" do
grid = [{1, 0}, {2, 1}, {0, 2}, {1, 2}, {2, 2}]
test "with --size=3 arg, prints an empty 3x3 grid" do
assert @empty_grid_3x3 ==
capture_io(fn ->
{:ok, pid} = Task.Supervisor.start_link()
task = Task.Supervisor.async(pid, fn ->
args = ["--size=3"]
Task.start_link(Life.CLI.main(args))
end)
:timer.sleep(1)
Task.shutdown(task)
end)
end

test "with glider arg, prints a 10x10 with a glider in the top-left corner" do
assert @grid_10x10_with_glider ==
capture_io(fn ->
{:ok, pid} = Task.Supervisor.start_link()
task = Task.Supervisor.async(pid, fn ->
args = ["glider"]
Task.start_link(Life.CLI.main(args))
end)
:timer.sleep(1)
Task.shutdown(task)
end)
end

assert @grid_with_glider ==
test "with glider and --size=3 args, prints a 3x3 grid with a glider" do
assert @grid_3x3_with_glider ==
capture_io(fn ->
assert :ok == Life.CLI.print(grid)
{:ok, pid} = Task.Supervisor.start_link()
task = Task.Supervisor.async(pid, fn ->
args = ["glider", "--size=3"]
Task.start_link(Life.CLI.main(args))
end)
:timer.sleep(1)
Task.shutdown(task)
end)
end

end

0 comments on commit 57fd384

Please sign in to comment.