Play draw endgames and solve on a rule draw

#10: drawn positions are graded against the draw as the target — a move
that keeps it passes, a losing move fails, and a rule draw reported by
chess.js (insufficient material, repetition, or 50-move) solves the
puzzle. The promotion cutoff now applies only to win puzzles.
This commit is contained in:
2026-11-01 22:04:21 +01:00
parent c5989628bc
commit be23f1d7f8
3 changed files with 167 additions and 29 deletions
+4
View File
@@ -161,6 +161,8 @@ export const ChessBoard = {
fen: this.chess.fen(), fen: this.chess.fen(),
san: move.san, san: move.san,
promotion: promotion || null, promotion: promotion || null,
draw: this.chess.isDraw(),
checkmate: this.chess.isCheckmate(),
}; };
if (isTrainer) { if (isTrainer) {
@@ -204,6 +206,8 @@ export const ChessBoard = {
this.pushEvent("opponent_reply_applied", { this.pushEvent("opponent_reply_applied", {
fen: this.chess.fen(), fen: this.chess.fen(),
san: move.san, san: move.san,
draw: this.chess.isDraw(),
checkmate: this.chess.isCheckmate(),
}); });
}, },
+32 -11
View File
@@ -40,10 +40,16 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
<p class="font-semibold text-red-600">Tablebase unavailable</p> <p class="font-semibold text-red-600">Tablebase unavailable</p>
<p class="text-red-500">{@blocked_reason}</p> <p class="text-red-500">{@blocked_reason}</p>
<% @status == :solved -> %> <% @status == :solved -> %>
<p class="font-semibold text-emerald-600">Solved!</p> <p class="font-semibold text-emerald-600">
{if @endgame.result == :draw, do: "Draw held!", else: "Solved!"}
</p>
<% @status == :failed -> %> <% @status == :failed -> %>
<p class="font-semibold text-red-600">Failed</p> <p class="font-semibold text-red-600">Failed</p>
<p class="text-red-500">That move throws away the win.</p> <p class="text-red-500">
{if @endgame.result == :draw,
do: "That move loses the draw.",
else: "That move throws away the win."}
</p>
<%= if @best_move do %> <%= if @best_move do %>
<p class="text-gray-600">Best was: {@best_move}</p> <p class="text-gray-600">Best was: {@best_move}</p>
<% end %> <% end %>
@@ -119,6 +125,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
def handle_event("endgame_move_played", params, socket) do def handle_event("endgame_move_played", params, socket) do
%{"from" => from, "to" => to, "san" => san} = params %{"from" => from, "to" => to, "san" => san} = params
promotion = params["promotion"] promotion = params["promotion"]
draw = params["draw"] || false
prev_fen = params["prev_fen"] || socket.assigns.fen prev_fen = params["prev_fen"] || socket.assigns.fen
fen_after = params["fen"] || socket.assigns.fen fen_after = params["fen"] || socket.assigns.fen
@@ -135,12 +142,22 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
put_flash(socket, :error, "That move is out of sequence. Press Reset to try again.")} put_flash(socket, :error, "That move is out of sequence. Press Reset to try again.")}
true -> true ->
grade_and_record(socket, from, to, promotion, san, fen_after) grade_and_record(socket, from, to, promotion, san, fen_after, draw)
end end
end end
def handle_event("opponent_reply_applied", %{"fen" => fen}, socket) do def handle_event("opponent_reply_applied", params, socket) do
if socket.assigns.awaiting_reply do %{"fen" => fen} = params
draw = params["draw"] || false
cond do
not socket.assigns.awaiting_reply ->
{:noreply, socket}
draw and socket.assigns.endgame.result == :draw ->
solve_attempt(socket)
true ->
case Tablebase.new(fen) do case Tablebase.new(fen) do
{:ok, tablebase} -> {:ok, tablebase} ->
{:noreply, {:noreply,
@@ -159,8 +176,6 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|> assign(:status, :blocked) |> assign(:status, :blocked)
|> assign(:blocked_reason, blocked_reason(other))} |> assign(:blocked_reason, blocked_reason(other))}
end end
else
{:noreply, socket}
end end
end end
@@ -251,7 +266,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
"The tablebase is unavailable (#{inspect(reason)})." "The tablebase is unavailable (#{inspect(reason)})."
end end
defp grade_and_record(socket, from, to, promotion, san, fen_after) do defp grade_and_record(socket, from, to, promotion, san, fen_after, draw) do
case find_move(socket.assigns.tablebase, from, to, promotion) do case find_move(socket.assigns.tablebase, from, to, promotion) do
nil -> nil ->
{:noreply, put_flash(socket, :error, "Could not identify that move in the tablebase.")} {:noreply, put_flash(socket, :error, "Could not identify that move in the tablebase.")}
@@ -270,11 +285,17 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
grade == :losing -> grade == :losing ->
fail_attempt(socket) fail_attempt(socket)
grade in [:optimal, :winning] and not is_nil(promotion) -> grade not in [:optimal, :winning] ->
# Conversion cutoff: a promotion that keeps the win ends the puzzle. {:noreply, socket}
draw and socket.assigns.endgame.result == :draw ->
# A drawn-by-rule position is the goal of a draw puzzle.
solve_attempt(socket)
socket.assigns.endgame.result == :win and not is_nil(promotion) ->
# Conversion cutoff: a promotion that keeps the win ends a win puzzle.
solve_attempt(socket) solve_attempt(socket)
grade in [:optimal, :winning] and
not same_position?(fen_after, socket.assigns.fen) -> not same_position?(fen_after, socket.assigns.fen) ->
start_opponent_reply(socket, fen_after) start_opponent_reply(socket, fen_after)
@@ -39,6 +39,13 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
] ]
end end
defp draw_moves do
[
%Tablebase.Move{uci: "d1d7", san: "Qd7", category: :draw, dtz: nil},
%Tablebase.Move{uci: "d1d4", san: "Qd4", category: :win, dtz: nil}
]
end
defp play_move(view, endgame, from, to, san) do defp play_move(view, endgame, from, to, san) do
render_hook(view, "endgame_move_played", %{ render_hook(view, "endgame_move_played", %{
"from" => from, "from" => from,
@@ -294,6 +301,112 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
end end
end end
describe "draw endgames" do
test "loads and is playable", %{conn: conn} do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
assert has_element?(view, "#endgame-board")
assert render(view) =~ "Make a move"
end
test "passes a move that keeps the draw and replies", %{conn: conn} do
endgame = endgame_fixture(result: :draw)
fen_after = "8/8/8/8/8/8/8/8 b - - 0 1"
reply = %Tablebase.Move{uci: "e8e7", san: "Ke7", category: :draw, dtz: nil}
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
"to" => "d7",
"san" => "Qd7",
"draw" => false,
"prev_fen" => endgame.fen,
"fen" => fen_after,
"endgame_id" => endgame.id
})
%{proxy: {ref, _topic, _}} = view
assert_receive {^ref, {:push_event, "opponent_move", payload}}, 100
assert payload == %{from: "e8", to: "e7", promotion: nil, san: "Ke7"}
end
test "fails a move that loses the draw", %{conn: conn} do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
"to" => "d4",
"san" => "Qd4",
"draw" => false,
"prev_fen" => endgame.fen,
"fen" => endgame.fen,
"endgame_id" => endgame.id
})
html = render(view)
assert html =~ "Failed"
assert html =~ "loses the draw"
assert html =~ "Qd7"
end
test "solves when the user's move draws by rule", %{conn: conn} do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
"to" => "d7",
"san" => "Qd7",
"draw" => true,
"prev_fen" => endgame.fen,
"fen" => endgame.fen,
"endgame_id" => endgame.id
})
assert render(view) =~ "Draw held!"
assert Endgames.get_endgame!(endgame.id).times_solved == 1
end
test "solves when the opponent's reply draws by rule", %{conn: conn} do
endgame = endgame_fixture(result: :draw)
fen_after = "8/8/8/8/8/8/8/8 b - - 0 1"
reply = %Tablebase.Move{uci: "e8e7", san: "Ke7", category: :draw, dtz: nil}
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
"to" => "d7",
"san" => "Qd7",
"draw" => false,
"prev_fen" => endgame.fen,
"fen" => fen_after,
"endgame_id" => endgame.id
})
render_hook(view, "opponent_reply_applied", %{"fen" => fen_after, "draw" => true})
assert render(view) =~ "Draw held!"
assert Endgames.get_endgame!(endgame.id).times_solved == 1
end
end
describe "reset" do describe "reset" do
test "clears the move list and grade, and re-pushes init_board", %{conn: conn} do test "clears the move list and grade, and re-pushes init_board", %{conn: conn} do
endgame = endgame_fixture() endgame = endgame_fixture()