mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Pawn can be blocked
This commit is contained in:
parent
4513bf324c
commit
2c8f2c7bcd
@ -2,19 +2,34 @@ defmodule Chess.Moves.Pawn do
|
|||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
def moves(board, {file, rank}) do
|
def moves(board, {file, rank}) do
|
||||||
piece = board["#{file},#{rank}"]
|
board["#{file},#{rank}"]
|
||||||
|
|> _moves(board, {file, rank})
|
||||||
|
end
|
||||||
|
|
||||||
case piece do
|
defp _moves(%{"colour" => "white"}, board, {file, rank}) do
|
||||||
%{"colour" => "white"} ->
|
cond do
|
||||||
case rank do
|
obstruction?(board, {file, rank + 1}) -> []
|
||||||
1 -> [{file, rank + 1}, {file, rank + 2}]
|
rank == 1 -> [
|
||||||
_ -> [{file, rank + 1}]
|
{file, rank + 1} |
|
||||||
end
|
_moves(%{"colour" => "white"}, board, {file, rank + 1})
|
||||||
%{"colour" => "black"} ->
|
]
|
||||||
case rank do
|
true -> [{file, rank + 1}]
|
||||||
6 -> [{file, rank - 1}, {file, rank - 2}]
|
|
||||||
_ -> [{file, rank - 1}]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp _moves(%{"colour" => "black"}, board, {file, rank}) do
|
||||||
|
cond do
|
||||||
|
obstruction?(board, {file, rank - 1}) -> []
|
||||||
|
rank == 6 -> [
|
||||||
|
{file, rank - 1} |
|
||||||
|
_moves(%{"colour" => "black"}, board, {file, rank - 1})
|
||||||
|
]
|
||||||
|
true -> [{file, rank - 1}]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp obstruction?(board, {file, rank}) do
|
||||||
|
board
|
||||||
|
|> Map.has_key?("#{file},#{rank}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,14 +4,14 @@ defmodule Chess.Moves.PawnTest do
|
|||||||
alias Chess.Moves.Pawn
|
alias Chess.Moves.Pawn
|
||||||
|
|
||||||
test "white pawn can move forward one or two spaces" do
|
test "white pawn can move forward one or two spaces" do
|
||||||
moves = Pawn.moves(board(), {4, 1})
|
moves = Pawn.moves(default_board(), {4, 1})
|
||||||
|
|
||||||
expected_moves = [{4, 2}, {4, 3}]
|
expected_moves = [{4, 2}, {4, 3}]
|
||||||
assert moves == expected_moves
|
assert moves == expected_moves
|
||||||
end
|
end
|
||||||
|
|
||||||
test "black pawn can move forward one or two spaces" do
|
test "black pawn can move forward one or two spaces" do
|
||||||
moves = Pawn.moves(board(), {4, 6})
|
moves = Pawn.moves(default_board(), {4, 6})
|
||||||
|
|
||||||
expected_moves = [{4, 5}, {4, 4}]
|
expected_moves = [{4, 5}, {4, 4}]
|
||||||
assert moves == expected_moves
|
assert moves == expected_moves
|
||||||
@ -33,7 +33,40 @@ defmodule Chess.Moves.PawnTest do
|
|||||||
assert moves == expected_moves
|
assert moves == expected_moves
|
||||||
end
|
end
|
||||||
|
|
||||||
def board do
|
test "pawn is blocked from moving two squares by another piece" do
|
||||||
|
board = %{
|
||||||
|
"4,1" => %{"type" => "pawn", "colour" => "white"},
|
||||||
|
"4,3" => %{"type" => "pawn", "colour" => "black"},
|
||||||
|
}
|
||||||
|
moves = Pawn.moves(board, {4, 1})
|
||||||
|
|
||||||
|
expected_moves = [{4, 2}]
|
||||||
|
assert moves == expected_moves
|
||||||
|
end
|
||||||
|
|
||||||
|
test "pawn is blocked from moving one or two squares by another piece" do
|
||||||
|
board = %{
|
||||||
|
"4,1" => %{"type" => "pawn", "colour" => "white"},
|
||||||
|
"4,2" => %{"type" => "pawn", "colour" => "black"},
|
||||||
|
}
|
||||||
|
moves = Pawn.moves(board, {4, 1})
|
||||||
|
|
||||||
|
expected_moves = []
|
||||||
|
assert moves == expected_moves
|
||||||
|
end
|
||||||
|
|
||||||
|
test "pawn is blocked from moving one square by another piece" do
|
||||||
|
board = %{
|
||||||
|
"4,2" => %{"type" => "pawn", "colour" => "white"},
|
||||||
|
"4,3" => %{"type" => "pawn", "colour" => "black"},
|
||||||
|
}
|
||||||
|
moves = Pawn.moves(board, {4, 2})
|
||||||
|
|
||||||
|
expected_moves = []
|
||||||
|
assert moves == expected_moves
|
||||||
|
end
|
||||||
|
|
||||||
|
def default_board do
|
||||||
Chess.Board.default
|
Chess.Board.default
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user