1
0
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:
Daniel Barber 2018-03-18 19:48:26 -04:00
parent 92b0b08298
commit aa7f685521
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
3 changed files with 64 additions and 5 deletions

View File

@ -9,6 +9,14 @@ defmodule Chess.Board do
end) end)
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 def piece(board, {file, rank}) do
board["#{file},#{rank}"] board["#{file},#{rank}"]
end end
@ -61,4 +69,11 @@ defmodule Chess.Board do
"7,0" => %{"type" => "rook", "colour" => "white"} "7,0" => %{"type" => "rook", "colour" => "white"}
} }
end end
defp index_to_tuple(index) do
index
|> String.split(",")
|> Enum.map(&(String.to_integer(&1)))
|> List.to_tuple
end
end end

View File

@ -4,14 +4,13 @@ defmodule Chess.Moves.Piece do
alias Chess.Board alias Chess.Board
alias Chess.Moves.Generator alias Chess.Moves.Generator
alias Chess.Moves.Pieces.Knight alias Chess.Moves.Pieces.Knight
alias Chess.Moves.Pieces.Pawn
def find(board, piece) do
end
def attacked?(board, {file, rank}) do def attacked?(board, {file, rank}) do
attacked_by_rook_or_queen?(board, {file, rank}) || attacked_by_rook_or_queen?(board, {file, rank}) ||
attacked_by_bishop_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 end
defp attacked_by_rook_or_queen?(board, {file, rank}) do 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") _attacked?(board, {file, rank}, Knight.pattern, "knight")
end 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 defp _attacked?(board, {file, rank}, {fv, rv}, pieces) do
{file, rank} = {file, rank} =
board board
@ -40,7 +57,7 @@ defmodule Chess.Moves.Piece do
piece = board["#{file},#{rank}"] piece = board["#{file},#{rank}"]
Enum.any?(pieces, &(match?(%{"type" => &1}, piece))) Enum.any?(pieces, &(match?(%{"type" => ^&1}, piece)))
end end
defp _attacked?(board, {file, rank}, pattern, piece_type) do defp _attacked?(board, {file, rank}, pattern, piece_type) do

View File

@ -12,6 +12,15 @@ defmodule Chess.Moves.PieceTest do
refute Piece.attacked?(board, {4, 5}) refute Piece.attacked?(board, {4, 5})
end 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 test "piece can be attacked by a rook" do
board = %{ board = %{
"4,5" => %{"type" => "king", "colour" => "white"}, "4,5" => %{"type" => "king", "colour" => "white"},
@ -56,4 +65,22 @@ defmodule Chess.Moves.PieceTest do
assert Piece.attacked?(board, {4, 5}) assert Piece.attacked?(board, {4, 5})
end 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 end