mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Reject out-of-range endgame FENs at entry (#25)
Add FEN.piece_count/1 and reject positions with more than 7 pieces in the Endgame changeset, so an 8+ piece FEN fails at entry instead of saving and then blocking at play. Accumulate all FEN issues rather than letting a later check overwrite an earlier one, and guard the seed FENs with a test (dropping the out-of-range seed that bypassed the changeset).
This commit is contained in:
@@ -66,27 +66,35 @@ defmodule Chesstrainer.Endgames.Endgame do
|
||||
defp fen_issues(fen) do
|
||||
case String.split(fen, " ") do
|
||||
[board, active, _castling, _en_passant, halfmove, fullmove] ->
|
||||
board_valid? = valid_board?(board)
|
||||
pieces = Chesstrainer.FEN.piece_count(fen)
|
||||
|
||||
issues = []
|
||||
|
||||
issues =
|
||||
if valid_board?(board),
|
||||
if board_valid?,
|
||||
do: issues,
|
||||
else: ["board must be 8 ranks of 8 squares each"]
|
||||
else: issues ++ ["board must be 8 ranks of 8 squares each"]
|
||||
|
||||
issues =
|
||||
if active in ["w", "b"],
|
||||
do: issues,
|
||||
else: ["active color must be 'w' or 'b'"]
|
||||
else: issues ++ ["active color must be 'w' or 'b'"]
|
||||
|
||||
issues =
|
||||
if non_negative_integer?(halfmove),
|
||||
do: issues,
|
||||
else: ["halfmove clock must be a non-negative integer"]
|
||||
else: issues ++ ["halfmove clock must be a non-negative integer"]
|
||||
|
||||
issues =
|
||||
if positive_integer?(fullmove),
|
||||
do: issues,
|
||||
else: ["fullmove number must be a positive integer"]
|
||||
else: issues ++ ["fullmove number must be a positive integer"]
|
||||
|
||||
issues =
|
||||
if board_valid? and is_integer(pieces) and pieces > 7,
|
||||
do: issues ++ ["positions with more than 7 pieces are not covered by tablebases"],
|
||||
else: issues
|
||||
|
||||
issues
|
||||
|
||||
|
||||
@@ -40,6 +40,50 @@ defmodule Chesstrainer.FEN do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Counts the pieces on the board, or returns `nil` if the board field is not a
|
||||
well-formed FEN board.
|
||||
|
||||
Kings count as pieces; empty-square digits are ignored. This is the number
|
||||
tablebases are sized by, so a position with more than seven pieces is beyond
|
||||
every tablebase.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Chesstrainer.FEN.piece_count("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
|
||||
3
|
||||
|
||||
iex> Chesstrainer.FEN.piece_count("not a fen")
|
||||
nil
|
||||
"""
|
||||
@spec piece_count(String.t() | nil) :: non_neg_integer() | nil
|
||||
def piece_count(fen) when is_binary(fen) do
|
||||
case String.split(fen, " ") do
|
||||
[board | _] -> board_piece_count(board)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
def piece_count(_fen), do: nil
|
||||
|
||||
defp board_piece_count(board) do
|
||||
board
|
||||
|> String.graphemes()
|
||||
|> Enum.reduce_while(0, fn
|
||||
"/", acc ->
|
||||
{:cont, acc}
|
||||
|
||||
<<c>>, acc when c in ?1..?8 ->
|
||||
{:cont, acc}
|
||||
|
||||
piece, acc when piece in ~w(p n b r q k P N B R Q K) ->
|
||||
{:cont, acc + 1}
|
||||
|
||||
_other, _acc ->
|
||||
{:halt, nil}
|
||||
end)
|
||||
end
|
||||
|
||||
defp color_initial_to_color_atom("b"), do: :black
|
||||
defp color_initial_to_color_atom("w"), do: :white
|
||||
defp color_initial_to_color_atom(_), do: nil
|
||||
|
||||
Reference in New Issue
Block a user