Browse Source

Add collision/merge with bottom

master
Julian Noble 2 years ago
parent
commit
1b4a42b668
  1. 18
      lib/bottom.ex
  2. 2
      lib/points.ex
  3. 29
      test/bottom_test.exs

18
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

2
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

29
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
Loading…
Cancel
Save