mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Kings are obstructed
This commit is contained in:
parent
7e1665781a
commit
17b9108173
@ -1,12 +1,12 @@
|
||||
defmodule Chess.Moves.King do
|
||||
@moduledoc false
|
||||
|
||||
def moves(_board, {file, rank}) do
|
||||
patterns()
|
||||
|> Enum.map(fn ({fv, rv}) -> {file + fv, rank + rv} end)
|
||||
|> Enum.reject(fn ({file, rank}) ->
|
||||
file < 0 || rank < 0 || file > 7 || rank > 7
|
||||
end)
|
||||
alias Chess.Moves.Generator
|
||||
|
||||
def moves(board, {file, rank}) do
|
||||
board["#{file},#{rank}"]
|
||||
|> Map.get("colour")
|
||||
|> Generator.moves(board, {file, rank}, patterns())
|
||||
end
|
||||
|
||||
defp patterns do
|
||||
|
||||
@ -13,12 +13,25 @@ defmodule Chess.Moves.KingTest do
|
||||
assert Enum.sort(moves) == expected_moves
|
||||
end
|
||||
|
||||
test "knights cannot move beyond the edges of the board" do
|
||||
board = %{"0,0" => %{"type" => "knight", "colour" => "white"}}
|
||||
test "kings cannot move beyond the edges of the board" do
|
||||
board = %{"0,0" => %{"type" => "king", "colour" => "white"}}
|
||||
moves = Moves.available(board, {0, 0})
|
||||
|
||||
expected_moves = Enum.sort([
|
||||
{1, 2}, {2, 1}
|
||||
{0, 1}, {1, 1}, {1, 0},
|
||||
])
|
||||
assert Enum.sort(moves) == expected_moves
|
||||
end
|
||||
|
||||
test "kings are blocked by pieces of the same colour" do
|
||||
board = %{
|
||||
"0,0" => %{"type" => "king", "colour" => "white"},
|
||||
"1,1" => %{"type" => "rook", "colour" => "white"},
|
||||
}
|
||||
moves = Moves.available(board, {0, 0})
|
||||
|
||||
expected_moves = Enum.sort([
|
||||
{0, 1}, {1, 0},
|
||||
])
|
||||
assert Enum.sort(moves) == expected_moves
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user