diff --git a/lib/bottom.ex b/lib/bottom.ex new file mode 100644 index 0000000..a416f8c --- /dev/null +++ b/lib/bottom.ex @@ -0,0 +1,18 @@ +defmodule Tetris.Bottom do + def merge(bottom, points) do + points + |> Enum.map( fn {x, y, c} -> {{x, y }, {x, y, c}} end) + |> Enum.into(bottom) + end + + def collides?(bottom, {x,y, _colour}) do + collides?(bottom, {x,y}) + end + def collides?(bottom, {x,y}) do + !!Map.get(bottom, {x,y}) + end + def collides?(bottom, points) when is_list(points) do + Enum.any?(points, &collides?(bottom,&1)) + end + +end diff --git a/lib/points.ex b/lib/points.ex index 4f1a258..19fefad 100644 --- a/lib/points.ex +++ b/lib/points.ex @@ -1,6 +1,6 @@ defmodule Tetris.Points do - def move_to_location(points, {x,y}=_location) do + def move_to_location(points, {x,y}) do Enum.map(points,fn {dx, dy} -> {dx + x, dy + y} end ) end diff --git a/test/bottom_test.exs b/test/bottom_test.exs new file mode 100644 index 0000000..857c95f --- /dev/null +++ b/test/bottom_test.exs @@ -0,0 +1,29 @@ +defmodule BottomTest do + use ExUnit.Case + import Tetris.Bottom + + + test "Various Collisions" do + bottom = %{{1,1} => {1,1, :blue}} + + assert collides? bottom, {1,1} + refute collides? bottom, {1,2} + assert collides? bottom, {1,1, :red} + assert collides? bottom, {1,1, :blue} + refute collides? bottom, {1,2, :red} + + assert collides? bottom, [{1,2, :red}, {1,1, :red}] + refute collides? bottom, [{3,2, :blue}, {4,4, :red}] + end + + test "test simple merge with bottom" do + bottom = %{{1,1} => {1,1, :blue}} + actual = merge bottom, [{1,2, :red}, {1,3, :red}] + expected = %{ + {1,1} => {1, 1, :blue}, + {1,2} => {1, 2, :red}, + {1,3} => {1, 3, :red} + } + assert actual == expected + end +end