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

Bishops can be obstructed

This commit is contained in:
Daniel Barber 2018-03-14 18:24:04 -04:00
parent edda70b4f2
commit 8a5adc44f5
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 41 additions and 13 deletions

View File

@ -1,6 +1,8 @@
defmodule Chess.Moves.Bishop do
@moduledoc false
alias Chess.Moves.Generator
def moves(board, {file, rank}) do
moves_northeast(board, {file, rank}) ++
moves_southeast(board, {file, rank}) ++
@ -8,27 +10,27 @@ defmodule Chess.Moves.Bishop do
moves_southwest(board, {file, rank})
end
defp moves_northeast(_board, {7, _rank}), do: []
defp moves_northeast(_board, {_file, 7}), do: []
defp moves_northeast(board, {file, rank}) do
[{file + 1, rank + 1} | moves_northeast(board, {file + 1, rank + 1})]
board["#{file},#{rank}"]
|> Map.get("colour")
|> Generator.moves(board, {file, rank}, {1, 1})
end
defp moves_southeast(_board, {7, _rank}), do: []
defp moves_southeast(_board, {_file, 0}), do: []
defp moves_southeast(board, {file, rank}) do
[{file + 1, rank - 1} | moves_southeast(board, {file + 1, rank - 1})]
board["#{file},#{rank}"]
|> Map.get("colour")
|> Generator.moves(board, {file, rank}, {1, -1})
end
defp moves_northwest(_board, {0, _rank}), do: []
defp moves_northwest(_board, {_file, 7}), do: []
defp moves_northwest(board, {file, rank}) do
[{file - 1, rank + 1} | moves_northwest(board, {file - 1, rank + 1})]
board["#{file},#{rank}"]
|> Map.get("colour")
|> Generator.moves(board, {file, rank}, {-1, 1})
end
defp moves_southwest(_board, {0, _rank}), do: []
defp moves_southwest(_board, {_file, 0}), do: []
defp moves_southwest(board, {file, rank}) do
[{file - 1, rank - 1} | moves_southwest(board, {file - 1, rank - 1})]
board["#{file},#{rank}"]
|> Map.get("colour")
|> Generator.moves(board, {file, rank}, {-1, -1})
end
end

View File

@ -19,7 +19,33 @@ defmodule Chess.Moves.BishopTest do
moves = Moves.available(board, {0, 0})
expected_moves = Enum.sort([
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7},
])
assert Enum.sort(moves) == expected_moves
end
test "bishops are blocked by pieces of their own colour" do
board = %{
"0,0" => %{"type" => "bishop", "colour" => "white"},
"5,5" => %{"type" => "king", "colour" => "white"},
}
moves = Moves.available(board, {0, 0})
expected_moves = Enum.sort([
{1, 1}, {2, 2}, {3, 3}, {4, 4},
])
assert Enum.sort(moves) == expected_moves
end
test "bishops can take an opponents piece" do
board = %{
"0,0" => %{"type" => "bishop", "colour" => "white"},
"5,5" => %{"type" => "knight", "colour" => "black"},
}
moves = Moves.available(board, {0, 0})
expected_moves = Enum.sort([
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5},
])
assert Enum.sort(moves) == expected_moves
end