1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Split out game status checks and add piece colour search

This commit is contained in:
Daniel Barber 2018-04-07 13:08:18 -04:00
parent d6c0ec5f13
commit e2826fdbce
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
4 changed files with 89 additions and 49 deletions

View File

@ -1,8 +1,6 @@
defmodule Chess.Board do defmodule Chess.Board do
@moduledoc false @moduledoc false
alias Chess.Moves.Piece
def transform(board) do def transform(board) do
Enum.map(0..7, fn (rank) -> Enum.map(0..7, fn (rank) ->
Enum.map(0..7, fn (file) -> Enum.map(0..7, fn (file) ->
@ -16,7 +14,15 @@ defmodule Chess.Board do
|> Enum.filter(fn({_index, piece}) -> |> Enum.filter(fn({_index, piece}) ->
match?(%{"type" => ^type, "colour" => ^colour}, piece) match?(%{"type" => ^type, "colour" => ^colour}, piece)
end) end)
|> Enum.map(fn({index, _piece}) -> index_to_tuple(index) end) |> indexes_to_tuples
end
def search(board, %{"colour" => colour}) do
board
|> Enum.filter(fn({_index, piece}) ->
match?(%{"colour" => ^colour}, piece)
end)
|> indexes_to_tuples
end end
def piece(board, {file, rank}) do def piece(board, {file, rank}) do
@ -32,16 +38,6 @@ defmodule Chess.Board do
Map.put(board, "#{to_file},#{to_rank}", piece) Map.put(board, "#{to_file},#{to_rank}", piece)
end end
def king_in_check?(board, colour) do
king =
board
|> search(%{"type" => "king", "colour" => colour})
|> List.first
board
|> Piece.attacked?(king)
end
def default do def default do
%{ %{
"0,7" => %{"type" => "rook", "colour" => "black"}, "0,7" => %{"type" => "rook", "colour" => "black"},
@ -82,6 +78,11 @@ defmodule Chess.Board do
} }
end end
defp indexes_to_tuples(list) do
list
|> Enum.map(fn({index, _piece}) -> index_to_tuple(index) end)
end
defp index_to_tuple(index) do defp index_to_tuple(index) do
index index
|> String.split(",") |> String.split(",")

21
lib/chess/game.ex Normal file
View File

@ -0,0 +1,21 @@
defmodule Chess.Game do
@moduledoc false
alias Chess.Board
alias Chess.Moves.Piece
def player_checkmated?(board, colour) do
board
|> Board.search(%{"colour" => colour})
end
def king_in_check?(board, colour) do
king =
board
|> Board.search(%{"type" => "king", "colour" => colour})
|> List.first
board
|> Piece.attacked?(king)
end
end

View File

@ -22,6 +22,17 @@ defmodule Chess.BoardTest do
assert Board.search(board, piece) == expected_result assert Board.search(board, piece) == expected_result
end end
test "finds pieces on the board with a partial search" do
board = Board.default
piece = %{"colour" => "white"}
expected_result = [
{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1},
{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
] |> Enum.sort
assert Board.search(board, piece) == expected_result
end
test "finds a single piece on the board" do test "finds a single piece on the board" do
board = Board.default board = Board.default
@ -40,40 +51,4 @@ defmodule Chess.BoardTest do
"5,2" => %{"type" => "queen", "colour" => "white"}, "5,2" => %{"type" => "queen", "colour" => "white"},
} }
end 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.king_in_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.king_in_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.king_in_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.king_in_check?(board, "white")
end
end end

43
test/chess/game_test.ex Normal file
View File

@ -0,0 +1,43 @@
defmodule Chess.GameTest do
@moduledoc false
use Chess.DataCase
alias Chess.Game
test "recognise when the king is in check" do
board = %{
"4,0" => %{"type" => "king", "colour" => "white"},
"4,7" => %{"type" => "queen", "colour" => "black"},
}
assert Game.king_in_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 Game.king_in_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 Game.king_in_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 Game.king_in_check?(board, "white")
end
end