mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Check for attacks from pawns
This commit is contained in:
parent
92b0b08298
commit
aa7f685521
@ -9,6 +9,14 @@ defmodule Chess.Board do
|
||||
end)
|
||||
end
|
||||
|
||||
def search(board, %{"type" => type, "colour" => colour}) do
|
||||
board
|
||||
|> Enum.filter(fn({_index, piece}) ->
|
||||
match?(%{"type" => ^type, "colour" => ^colour}, piece)
|
||||
end)
|
||||
|> Enum.map(fn({index, _piece}) -> index_to_tuple(index) end)
|
||||
end
|
||||
|
||||
def piece(board, {file, rank}) do
|
||||
board["#{file},#{rank}"]
|
||||
end
|
||||
@ -61,4 +69,11 @@ defmodule Chess.Board do
|
||||
"7,0" => %{"type" => "rook", "colour" => "white"}
|
||||
}
|
||||
end
|
||||
|
||||
defp index_to_tuple(index) do
|
||||
index
|
||||
|> String.split(",")
|
||||
|> Enum.map(&(String.to_integer(&1)))
|
||||
|> List.to_tuple
|
||||
end
|
||||
end
|
||||
|
||||
@ -4,14 +4,13 @@ defmodule Chess.Moves.Piece do
|
||||
alias Chess.Board
|
||||
alias Chess.Moves.Generator
|
||||
alias Chess.Moves.Pieces.Knight
|
||||
|
||||
def find(board, piece) do
|
||||
end
|
||||
alias Chess.Moves.Pieces.Pawn
|
||||
|
||||
def attacked?(board, {file, rank}) do
|
||||
attacked_by_rook_or_queen?(board, {file, rank}) ||
|
||||
attacked_by_bishop_or_queen?(board, {file, rank}) ||
|
||||
attacked_by_knight?(board, {file, rank})
|
||||
attacked_by_knight?(board, {file, rank}) ||
|
||||
attacked_by_pawn?(board, {file, rank})
|
||||
end
|
||||
|
||||
defp attacked_by_rook_or_queen?(board, {file, rank}) do
|
||||
@ -32,6 +31,24 @@ defmodule Chess.Moves.Piece do
|
||||
_attacked?(board, {file, rank}, Knight.pattern, "knight")
|
||||
end
|
||||
|
||||
defp attacked_by_pawn?(board, {file, rank}) do
|
||||
colour =
|
||||
board
|
||||
|> Board.piece({file, rank})
|
||||
|> Map.get("colour")
|
||||
|
||||
board
|
||||
|> _attacked_by_pawn?({file, rank}, Pawn.pattern(colour))
|
||||
end
|
||||
|
||||
defp _attacked_by_pawn?(board, {file, rank}, pattern) do
|
||||
moves =
|
||||
board
|
||||
|> Generator.moves({file, rank}, pattern)
|
||||
|
||||
Enum.any?(moves, &(match_piece(board, &1, "pawn")))
|
||||
end
|
||||
|
||||
defp _attacked?(board, {file, rank}, {fv, rv}, pieces) do
|
||||
{file, rank} =
|
||||
board
|
||||
@ -40,7 +57,7 @@ defmodule Chess.Moves.Piece do
|
||||
|
||||
piece = board["#{file},#{rank}"]
|
||||
|
||||
Enum.any?(pieces, &(match?(%{"type" => &1}, piece)))
|
||||
Enum.any?(pieces, &(match?(%{"type" => ^&1}, piece)))
|
||||
end
|
||||
|
||||
defp _attacked?(board, {file, rank}, pattern, piece_type) do
|
||||
|
||||
@ -12,6 +12,15 @@ defmodule Chess.Moves.PieceTest do
|
||||
refute Piece.attacked?(board, {4, 5})
|
||||
end
|
||||
|
||||
test "piece is not being attacked by piece of its own colour" do
|
||||
board = %{
|
||||
"4,5" => %{"type" => "king", "colour" => "white"},
|
||||
"2,5" => %{"type" => "rook", "colour" => "white"},
|
||||
}
|
||||
|
||||
refute Piece.attacked?(board, {4, 5})
|
||||
end
|
||||
|
||||
test "piece can be attacked by a rook" do
|
||||
board = %{
|
||||
"4,5" => %{"type" => "king", "colour" => "white"},
|
||||
@ -56,4 +65,22 @@ defmodule Chess.Moves.PieceTest do
|
||||
|
||||
assert Piece.attacked?(board, {4, 5})
|
||||
end
|
||||
|
||||
test "piece can be attacked by a pawn" do
|
||||
board = %{
|
||||
"4,5" => %{"type" => "king", "colour" => "white"},
|
||||
"5,6" => %{"type" => "pawn", "colour" => "black"},
|
||||
}
|
||||
|
||||
assert Piece.attacked?(board, {4, 5})
|
||||
end
|
||||
|
||||
test "piece is not attacked by a pawn directly in front" do
|
||||
board = %{
|
||||
"4,5" => %{"type" => "king", "colour" => "white"},
|
||||
"4,6" => %{"type" => "pawn", "colour" => "black"},
|
||||
}
|
||||
|
||||
refute Piece.attacked?(board, {4, 5})
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user