mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
* commented out lesson and learning strip until lessons are established * reword the hero section * change layout and working on puzzle section - remove top div as it was redundant * amend flash message on replay * switch cancel session for return home * remove system theme toggle since this is the default, leaving only light and dark toggles * add keyboard and screen reader support for play (#72) First draft covering the accessibility work in #72:\n\n- labelled SAN/UCI move input, validated against the tablebase and applied\n by the board hook, plus an accessible promotion picker\n- live-region announcements for moves, checks, checkmates and solve/fail,\n and a hidden text summary of the position\n- accessible labels, focus ring, skip link, keyboard-friendly review table\n and non-colour grade cues\n- WCAG AA text contrast roles and a responsive play surface\n- regression tests for move entry, announcements and link-style CTAs
94 lines
2.2 KiB
Elixir
94 lines
2.2 KiB
Elixir
defmodule ChesstrainerWeb.Board.PositionText do
|
|
@moduledoc """
|
|
Plain-language descriptions of a chess position for assistive technology.
|
|
|
|
A chessground board is a picture to a screen reader, so the play surface and
|
|
the static boards use this text as their accessible name or description.
|
|
"""
|
|
|
|
alias Chesstrainer.FEN
|
|
|
|
@files ~w(a b c d e f g h)
|
|
|
|
@names %{
|
|
"k" => "king",
|
|
"q" => "queen",
|
|
"r" => "rook",
|
|
"b" => "bishop",
|
|
"n" => "knight",
|
|
"p" => "pawn"
|
|
}
|
|
|
|
@doc """
|
|
Describes whose turn it is and where every piece stands.
|
|
|
|
Returns a generic description when the FEN cannot be read.
|
|
|
|
## Examples
|
|
|
|
iex> ChesstrainerWeb.Board.PositionText.summary("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
|
|
"White to move. White: king on d3, rook on f3. Black: king on d6."
|
|
"""
|
|
@spec summary(String.t() | nil) :: String.t()
|
|
def summary(fen) when is_binary(fen) do
|
|
case FEN.board(fen) do
|
|
nil ->
|
|
"Chess position."
|
|
|
|
rows ->
|
|
["#{turn(fen)}." | piece_groups(rows)]
|
|
|> Enum.join(" ")
|
|
end
|
|
end
|
|
|
|
def summary(_fen), do: "Chess position."
|
|
|
|
defp turn(fen) do
|
|
case FEN.color_from_fen(fen) do
|
|
:white -> "White to move"
|
|
:black -> "Black to move"
|
|
_ -> "Side to move unknown"
|
|
end
|
|
end
|
|
|
|
defp piece_groups(rows) do
|
|
["White", "Black"]
|
|
|> Enum.map(fn label -> group(label, rows) end)
|
|
|> Enum.reject(&is_nil/1)
|
|
end
|
|
|
|
defp group(label, rows) do
|
|
colour = if label == "White", do: :white, else: :black
|
|
|
|
case pieces(rows, colour) do
|
|
[] -> nil
|
|
pieces -> "#{label}: #{Enum.join(pieces, ", ")}."
|
|
end
|
|
end
|
|
|
|
defp pieces(rows, colour) do
|
|
rows
|
|
|> Enum.with_index()
|
|
|> Enum.flat_map(fn {row, rank_index} ->
|
|
row
|
|
|> Enum.with_index()
|
|
|> Enum.flat_map(fn
|
|
{nil, _file_index} ->
|
|
[]
|
|
|
|
{piece, file_index} ->
|
|
if piece_colour(piece) == colour do
|
|
square = "#{Enum.at(@files, file_index)}#{8 - rank_index}"
|
|
["#{Map.fetch!(@names, String.downcase(piece))} on #{square}"]
|
|
else
|
|
[]
|
|
end
|
|
end)
|
|
end)
|
|
end
|
|
|
|
defp piece_colour(piece) do
|
|
if piece == String.upcase(piece), do: :white, else: :black
|
|
end
|
|
end
|