Browse Source

Add tetris move api and fix collisions

master 0.1.2
Julian Noble 2 years ago
parent
commit
d18409abc1
  1. 2
      lib/bottom.ex
  2. 23
      lib/tetris.ex
  3. 23
      test/tetris_test.exs

2
lib/bottom.ex

@ -9,7 +9,7 @@ defmodule Tetris.Bottom do
collides?(bottom, {x,y}) collides?(bottom, {x,y})
end end
def collides?(bottom, {x,y}) do def collides?(bottom, {x,y}) do
!!Map.get(bottom, {x,y}) !!Map.get(bottom, {x,y}) || x < 1 || x > 10 || y > 20
end end
def collides?(bottom, points) when is_list(points) do def collides?(bottom, points) when is_list(points) do
Enum.any?(points, &collides?(bottom,&1)) Enum.any?(points, &collides?(bottom,&1))

23
lib/tetris.ex

@ -1,6 +1,25 @@
defmodule Tetris do defmodule Tetris do
alias Tetris.{Bottom, Brick, Points}
def hello do def prepare(brick) do
:world 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 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 end

23
test/tetris_test.exs

@ -1,7 +1,26 @@
defmodule TetrisTest do defmodule TetrisTest do
use ExUnit.Case use ExUnit.Case
import Tetris
alias Tetris.{Brick}
test "greets the world" do test "try to move right, success" do
assert Tetris.hello() == :world 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
end end

Loading…
Cancel
Save