From 320cb03b1f8bc324227f20e942b6459000a86c82 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 23 Mar 2018 12:01:07 -0400 Subject: [PATCH] Add tests for king in check --- lib/chess/board.ex | 12 ++++++++++ lib/chess/moves/piece.ex | 4 ++++ test/chess/board_test.exs | 40 +++++++++++++++++++++++++++++++-- test/chess/moves/piece_test.exs | 9 ++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/chess/board.ex b/lib/chess/board.ex index 1157ac2..b764522 100644 --- a/lib/chess/board.ex +++ b/lib/chess/board.ex @@ -1,6 +1,8 @@ defmodule Chess.Board do @moduledoc false + alias Chess.Moves.Piece + def transform(board) do Enum.map(0..7, fn (rank) -> Enum.map(0..7, fn (file) -> @@ -30,6 +32,16 @@ defmodule Chess.Board do Map.put(board, "#{to_file},#{to_rank}", piece) end + def check?(board, colour) do + king = + board + |> search(%{"type" => "king", "colour" => colour}) + |> List.first + + board + |> Piece.attacked?(king) + end + def default do %{ "0,7" => %{"type" => "rook", "colour" => "black"}, diff --git a/lib/chess/moves/piece.ex b/lib/chess/moves/piece.ex index fb8cc30..3c75381 100644 --- a/lib/chess/moves/piece.ex +++ b/lib/chess/moves/piece.ex @@ -49,6 +49,10 @@ defmodule Chess.Moves.Piece do Enum.any?(moves, &(match_piece(board, &1, "pawn"))) end + defp _attacked?(_board, {0, _rank}, {-1, _}, _), do: false + defp _attacked?(_board, {_file, 0}, {_, -1}, _), do: false + defp _attacked?(_board, {7, _rank}, {1, _}, _), do: false + defp _attacked?(_board, {_file, 7}, {_, 1}, _), do: false defp _attacked?(board, {file, rank}, {fv, rv}, pieces) do {file, rank} = board diff --git a/test/chess/board_test.exs b/test/chess/board_test.exs index 0215798..c19f448 100644 --- a/test/chess/board_test.exs +++ b/test/chess/board_test.exs @@ -31,13 +31,49 @@ defmodule Chess.BoardTest do test "moves a piece" do board = %{ - "3,0" => %{"type" => "queen", "colour" => "white"}, + "3,0" => %{"type" => "queen", "colour" => "white"}, } new_board = Board.move_piece(board, %{"from" => [3, 0], "to" => [5, 2]}) assert new_board == %{ - "5,2" => %{"type" => "queen", "colour" => "white"}, + "5,2" => %{"type" => "queen", "colour" => "white"}, } end + + test "recognise when the king is in check" do + board = %{ + "4,0" => %{"type" => "king", "colour" => "white"}, + "4,7" => %{"type" => "queen", "colour" => "black"}, + } + + assert Board.check?(board, "white") + end + + test "recognise when the king is not in check" do + board = %{ + "5,0" => %{"type" => "king", "colour" => "white"}, + "4,7" => %{"type" => "queen", "colour" => "black"}, + } + + refute Board.check?(board, "white") + end + + test "recognize when the king is in check by a knight" do + board = %{ + "4,0" => %{"type" => "king", "colour" => "white"}, + "3,2" => %{"type" => "knight", "colour" => "black"}, + } + + assert Board.check?(board, "white") + end + + test "recognize when the king is in check by a pawn" do + board = %{ + "4,0" => %{"type" => "king", "colour" => "white"}, + "3,1" => %{"type" => "pawn", "colour" => "black"}, + } + + assert Board.check?(board, "white") + end end diff --git a/test/chess/moves/piece_test.exs b/test/chess/moves/piece_test.exs index 8b20e4d..9fa6376 100644 --- a/test/chess/moves/piece_test.exs +++ b/test/chess/moves/piece_test.exs @@ -12,6 +12,15 @@ defmodule Chess.Moves.PieceTest do refute Piece.attacked?(board, {4, 5}) end + test "piece on the edge of the board is not being attacked" do + board = %{ + "4,0" => %{"type" => "king", "colour" => "white"}, + "2,7" => %{"type" => "rook", "colour" => "black"}, + } + + refute Piece.attacked?(board, {4, 0}) + end + test "piece is not being attacked by piece of its own colour" do board = %{ "4,5" => %{"type" => "king", "colour" => "white"},