Julian Noble
2 years ago
3 changed files with 48 additions and 1 deletions
@ -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 |
@ -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…
Reference in new issue