diff --git a/lib/bottom.ex b/lib/bottom.ex index a416f8c..e2386de 100644 --- a/lib/bottom.ex +++ b/lib/bottom.ex @@ -9,7 +9,7 @@ defmodule Tetris.Bottom do collides?(bottom, {x,y}) end def collides?(bottom, {x,y}) do - !!Map.get(bottom, {x,y}) + !!Map.get(bottom, {x,y}) || x < 1 || x > 10 || y > 20 end def collides?(bottom, points) when is_list(points) do Enum.any?(points, &collides?(bottom,&1)) diff --git a/lib/tetris.ex b/lib/tetris.ex index ba5aa00..4039349 100644 --- a/lib/tetris.ex +++ b/lib/tetris.ex @@ -1,6 +1,25 @@ defmodule Tetris do + alias Tetris.{Bottom, Brick, Points} - def hello do - :world + def prepare(brick) do + brick + |> Brick.prepare + |> Points.move_to_location(brick.location) + + end + + def try_move(brick, bottom, f) do + new_brick = f.(brick) + + if Bottom.collides?(bottom, prepare(new_brick)) do + brick + else + new_brick + end end + + def try_left(brick, bottom), do: try_move(brick,bottom, &Brick.left/1) + def try_right(brick, bottom), do: try_move(brick,bottom, &Brick.right/1) + def try_spin(brick, bottom), do: try_move(brick,bottom, &Brick.spin_90/1) + end diff --git a/test/tetris_test.exs b/test/tetris_test.exs index 731ca15..bedade2 100644 --- a/test/tetris_test.exs +++ b/test/tetris_test.exs @@ -1,7 +1,26 @@ defmodule TetrisTest do use ExUnit.Case + import Tetris + alias Tetris.{Brick} - test "greets the world" do - assert Tetris.hello() == :world + test "try to move right, success" do + brick = Brick.new(%{location: {5,1}}) + bottom = %{} + + expected = brick |> Brick.right + actual = try_right(brick, bottom) + + assert actual == expected + end + + test "try to move right, failure returns original brick " do + brick = Brick.new(%{location: {8,1}}) + bottom = %{} + + actual = try_right(brick, bottom) + + assert actual == brick end + + end