From 8a5adc44f574d042ea612ebcc46beb42452f1a8f Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Wed, 14 Mar 2018 18:24:04 -0400 Subject: [PATCH] Bishops can be obstructed --- lib/chess/moves/bishop.ex | 26 ++++++++++++++------------ test/chess/moves/bishop_test.exs | 28 +++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/chess/moves/bishop.ex b/lib/chess/moves/bishop.ex index a46c9c7..59dee89 100644 --- a/lib/chess/moves/bishop.ex +++ b/lib/chess/moves/bishop.ex @@ -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 diff --git a/test/chess/moves/bishop_test.exs b/test/chess/moves/bishop_test.exs index 4bd3941..9ffa149 100644 --- a/test/chess/moves/bishop_test.exs +++ b/test/chess/moves/bishop_test.exs @@ -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