Skip to content

Commit

Permalink
Merge pull request #63 from Dkendal/fix/rand-space-on-diagonal
Browse files Browse the repository at this point in the history
Fix random space func
  • Loading branch information
Dylan C. Kendal authored Feb 28, 2017
2 parents 09cda19 + 67a8359 commit f7431f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/battle_snake/world.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ defmodule BattleSnake.World do
width = world.width - 1
height = world.height - 1

domain = Stream.map(0..width, (& &1))
range = Stream.map(0..height, (& &1))
stream = Stream.zip(domain, range)
stream = Stream.map(stream, fn {x, y} -> p(x, y) end)
stream = Stream.flat_map(0..width, fn x ->
Stream.flat_map(0..height, fn y ->
[p(x, y)]
end)
end)

stream = Stream.filter(stream, fn p ->
!MapSet.member?(spaces, p)
end)
Expand Down
10 changes: 6 additions & 4 deletions test/battle_snake/world_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ defmodule BattleSnake.WorldTest do
end

test "does the thing" do
food = (for x <- 0..90, y <- 0..90, do: p(x, y))

world = build(:world,
snakes: [],
width: 100,
height: 100,
food: (for x <- 0..90, y <- 0..90, do: p(x, y)))
food: food)

assert {:ok, p(x, y)} = World.rand_unoccupied_space(world)
assert_in_delta 90, 99, x
assert_in_delta 90, 99, y
assert {:ok, v} = World.rand_unoccupied_space(world)
assert MapSet.member?(MapSet.new(food), v) == false
end

test "returns an error if there is no space" do
Expand Down

0 comments on commit f7431f4

Please sign in to comment.