1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Kings are obstructed

This commit is contained in:
Daniel Barber 2018-03-14 20:36:29 -04:00
parent 7e1665781a
commit 17b9108173
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 22 additions and 9 deletions

View File

@ -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

View File

@ -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