mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
End the attempt on solve or fail and count attempts
#9: solve the puzzle on checkmate or a winning promotion, fail immediately on a losing move (revealing the missed best move), and increment times_attempted when an attempt starts and times_solved when it is solved. Terminal states lock play; reset starts a new attempt.
This commit is contained in:
@@ -105,6 +105,22 @@ defmodule Chesstrainer.Endgames do
|
|||||||
Endgame.changeset(endgame, attrs)
|
Endgame.changeset(endgame, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Records the start of an attempt, incrementing `times_attempted`.
|
||||||
|
"""
|
||||||
|
def record_attempt(%Endgame{id: id}), do: increment(id, times_attempted: 1)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Records a solved attempt, incrementing `times_solved`.
|
||||||
|
"""
|
||||||
|
def record_solve(%Endgame{id: id}), do: increment(id, times_solved: 1)
|
||||||
|
|
||||||
|
defp increment(id, increments) do
|
||||||
|
Repo.update_all(from(e in Endgame, where: e.id == ^id), inc: increments)
|
||||||
|
|
||||||
|
{:ok, Repo.get!(Endgame, id)}
|
||||||
|
end
|
||||||
|
|
||||||
def add_tag(endgame, tag) do
|
def add_tag(endgame, tag) do
|
||||||
endgame
|
endgame
|
||||||
|> Repo.preload(:tags)
|
|> Repo.preload(:tags)
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
<.header>
|
<.header>
|
||||||
Play endgame {@endgame.key}
|
Play endgame {@endgame.key}
|
||||||
<:subtitle>
|
<:subtitle>
|
||||||
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating}
|
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating} · Attempts: {@endgame.times_attempted ||
|
||||||
|
0} · Solved: {@endgame.times_solved || 0}
|
||||||
</:subtitle>
|
</:subtitle>
|
||||||
<:actions>
|
<:actions>
|
||||||
<.button navigate={~p"/endgames"}>
|
<.button navigate={~p"/endgames"}>
|
||||||
@@ -38,6 +39,14 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
<% @status == :blocked -> %>
|
<% @status == :blocked -> %>
|
||||||
<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 -> %>
|
||||||
|
<p class="font-semibold text-emerald-600">Solved!</p>
|
||||||
|
<% @status == :failed -> %>
|
||||||
|
<p class="font-semibold text-red-600">Failed</p>
|
||||||
|
<p class="text-red-500">That move throws away the win.</p>
|
||||||
|
<%= if @best_move do %>
|
||||||
|
<p class="text-gray-600">Best was: {@best_move}</p>
|
||||||
|
<% end %>
|
||||||
<% @awaiting_reply -> %>
|
<% @awaiting_reply -> %>
|
||||||
<p class="text-gray-500">Opponent is replying…</p>
|
<p class="text-gray-500">Opponent is replying…</p>
|
||||||
<% @last_grade == :optimal -> %>
|
<% @last_grade == :optimal -> %>
|
||||||
@@ -74,7 +83,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<.button phx-click="reset" variant="primary">
|
<.button phx-click="reset" variant="primary">
|
||||||
<.icon name="hero-arrow-path" /> Reset
|
<.icon name="hero-arrow-path" />
|
||||||
|
{if @status in [:solved, :failed], do: "Try again", else: "Reset"}
|
||||||
</.button>
|
</.button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -95,6 +105,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
|> assign(:tablebase, nil)
|
|> assign(:tablebase, nil)
|
||||||
|> assign(:status, :loading)
|
|> assign(:status, :loading)
|
||||||
|> assign(:blocked_reason, nil)
|
|> assign(:blocked_reason, nil)
|
||||||
|
|> assign(:best_move, nil)
|
||||||
|> assign(:move_list, [])
|
|> assign(:move_list, [])
|
||||||
|> assign(:last_san, "None yet")
|
|> assign(:last_san, "None yet")
|
||||||
|> assign(:last_from_to, "None yet")
|
|> assign(:last_from_to, "None yet")
|
||||||
@@ -112,10 +123,13 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
fen_after = params["fen"] || socket.assigns.fen
|
fen_after = params["fen"] || socket.assigns.fen
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
socket.assigns.status != :playing ->
|
socket.assigns.status == :blocked ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
put_flash(socket, :error, "The tablebase is unavailable, so this move cannot be graded.")}
|
put_flash(socket, :error, "The tablebase is unavailable, so this move cannot be graded.")}
|
||||||
|
|
||||||
|
socket.assigns.status != :playing ->
|
||||||
|
{:noreply, socket}
|
||||||
|
|
||||||
not same_position?(prev_fen, socket.assigns.fen) ->
|
not same_position?(prev_fen, socket.assigns.fen) ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
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.")}
|
||||||
@@ -180,12 +194,20 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
|
|
||||||
defp load_attempt(socket) do
|
defp load_attempt(socket) do
|
||||||
fen = socket.assigns.endgame.fen
|
fen = socket.assigns.endgame.fen
|
||||||
socket = socket |> assign(:fen, fen) |> assign(:awaiting_reply, false)
|
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:fen, fen)
|
||||||
|
|> assign(:awaiting_reply, false)
|
||||||
|
|> assign(:best_move, nil)
|
||||||
|
|
||||||
if connected?(socket) do
|
if connected?(socket) do
|
||||||
case Tablebase.new(fen) do
|
case Tablebase.new(fen) do
|
||||||
{:ok, tablebase} ->
|
{:ok, tablebase} ->
|
||||||
|
{:ok, endgame} = Endgames.record_attempt(socket.assigns.endgame)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|
|> assign(:endgame, endgame)
|
||||||
|> assign(:tablebase, tablebase)
|
|> assign(:tablebase, tablebase)
|
||||||
|> assign(:status, :playing)
|
|> assign(:status, :playing)
|
||||||
|> assign(:blocked_reason, nil)
|
|> assign(:blocked_reason, nil)
|
||||||
@@ -244,10 +266,19 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
|> assign(:last_grade, grade)
|
|> assign(:last_grade, grade)
|
||||||
|> assign(:move_list, socket.assigns.move_list ++ [san])
|
|> assign(:move_list, socket.assigns.move_list ++ [san])
|
||||||
|
|
||||||
if grade in [:optimal, :winning] and
|
cond do
|
||||||
not same_position?(fen_after, socket.assigns.fen) do
|
grade == :losing ->
|
||||||
|
fail_attempt(socket)
|
||||||
|
|
||||||
|
grade in [:optimal, :winning] and not is_nil(promotion) ->
|
||||||
|
# Conversion cutoff: a promotion that keeps the win ends the puzzle.
|
||||||
|
solve_attempt(socket)
|
||||||
|
|
||||||
|
grade in [:optimal, :winning] and
|
||||||
|
not same_position?(fen_after, socket.assigns.fen) ->
|
||||||
start_opponent_reply(socket, fen_after)
|
start_opponent_reply(socket, fen_after)
|
||||||
else
|
|
||||||
|
true ->
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -255,14 +286,17 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
|
|
||||||
defp start_opponent_reply(socket, fen_after) do
|
defp start_opponent_reply(socket, fen_after) do
|
||||||
case Tablebase.new(fen_after) do
|
case Tablebase.new(fen_after) do
|
||||||
|
{:ok, %Tablebase{checkmate: true}} ->
|
||||||
|
solve_attempt(socket)
|
||||||
|
|
||||||
{:ok, %Tablebase{moves: [reply | _]}} ->
|
{:ok, %Tablebase{moves: [reply | _]}} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> assign(:awaiting_reply, true)
|
|> assign(:awaiting_reply, true)
|
||||||
|> push_event("opponent_move", reply_payload(reply))}
|
|> push_event("opponent_move", reply_payload(reply))}
|
||||||
|
|
||||||
{:ok, %Tablebase{moves: []}} ->
|
{:ok, %Tablebase{}} ->
|
||||||
# Terminal position (checkmate/stalemate): there is no reply to play.
|
# Terminal non-mate position (stalemate/insufficient): no reply to play.
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
|
|
||||||
other ->
|
other ->
|
||||||
@@ -273,6 +307,47 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp solve_attempt(socket) do
|
||||||
|
{:ok, endgame} = Endgames.record_solve(socket.assigns.endgame)
|
||||||
|
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> assign(:status, :solved)
|
||||||
|
|> assign(:endgame, endgame)
|
||||||
|
|> assign(:awaiting_reply, false)
|
||||||
|
|> assign(:best_move, nil)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fail_attempt(socket) do
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> assign(:status, :failed)
|
||||||
|
|> assign(:awaiting_reply, false)
|
||||||
|
|> assign(:best_move, best_move_text(socket.assigns.tablebase))}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp best_move_text(%Tablebase{category: category, moves: [best | _] = moves}) do
|
||||||
|
best_rank = outcome_rank(category)
|
||||||
|
|
||||||
|
if is_nil(best_rank) do
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
san =
|
||||||
|
moves
|
||||||
|
|> Enum.filter(fn move ->
|
||||||
|
outcome_rank(mover_outcome(move.category)) == best_rank and move.dtz == best.dtz
|
||||||
|
end)
|
||||||
|
|> Enum.map(& &1.san)
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
|> Enum.take(3)
|
||||||
|
|> Enum.join(", ")
|
||||||
|
|
||||||
|
if san == "", do: nil, else: san
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp best_move_text(_tablebase), do: nil
|
||||||
|
|
||||||
defp reply_payload(%Tablebase.Move{uci: uci, san: san}) do
|
defp reply_payload(%Tablebase.Move{uci: uci, san: san}) do
|
||||||
%{
|
%{
|
||||||
from: String.slice(uci, 0, 2),
|
from: String.slice(uci, 0, 2),
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
import Phoenix.LiveViewTest
|
import Phoenix.LiveViewTest
|
||||||
import Chesstrainer.EndgamesFixtures
|
import Chesstrainer.EndgamesFixtures
|
||||||
|
|
||||||
|
alias Chesstrainer.Endgames
|
||||||
alias Chesstrainer.Endgames.Tablebase
|
alias Chesstrainer.Endgames.Tablebase
|
||||||
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
|
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
|
||||||
alias Chesstrainer.Ratelimiters
|
alias Chesstrainer.Ratelimiters
|
||||||
@@ -22,6 +23,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
defp seed_tablebase(fen, opts \\ []) do
|
defp seed_tablebase(fen, opts \\ []) do
|
||||||
tablebase = %Tablebase{
|
tablebase = %Tablebase{
|
||||||
category: Keyword.get(opts, :category, :win),
|
category: Keyword.get(opts, :category, :win),
|
||||||
|
checkmate: Keyword.get(opts, :checkmate, false),
|
||||||
moves: Keyword.get(opts, :moves, [])
|
moves: Keyword.get(opts, :moves, [])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +148,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
assert render(view) =~ "Winning — but not the sharpest"
|
assert render(view) =~ "Winning — but not the sharpest"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "grades a move that throws the win away as losing", %{conn: conn} do
|
test "fails a move that throws the win away", %{conn: conn} do
|
||||||
endgame = endgame_fixture()
|
endgame = endgame_fixture()
|
||||||
seed_tablebase(endgame.fen, moves: grading_moves())
|
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||||
|
|
||||||
@@ -154,7 +156,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
|
|
||||||
play_move(view, endgame, "d1", "d4", "Qd4")
|
play_move(view, endgame, "d1", "d4", "Qd4")
|
||||||
|
|
||||||
assert render(view) =~ "Losing move"
|
assert render(view) =~ "Failed"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "rejects a move that is out of sequence", %{conn: conn} do
|
test "rejects a move that is out of sequence", %{conn: conn} do
|
||||||
@@ -220,7 +222,75 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
%{proxy: {ref, _topic, _}} = view
|
%{proxy: {ref, _topic, _}} = view
|
||||||
|
|
||||||
refute_receive {^ref, {:push_event, "opponent_move", _payload}}, 100
|
refute_receive {^ref, {:push_event, "opponent_move", _payload}}, 100
|
||||||
assert render(view) =~ "Losing move"
|
assert render(view) =~ "Failed"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "attempt outcome" do
|
||||||
|
test "records an attempt on start", %{conn: conn} do
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
seed_tablebase(endgame.fen)
|
||||||
|
|
||||||
|
{:ok, _view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||||
|
|
||||||
|
assert Endgames.get_endgame!(endgame.id).times_attempted == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "solves on checkmate", %{conn: conn} do
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
fen_after = "8/8/8/8/8/8/8/8 b - - 0 1"
|
||||||
|
|
||||||
|
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||||
|
seed_tablebase(fen_after, checkmate: true, moves: [])
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||||
|
|
||||||
|
render_hook(view, "endgame_move_played", %{
|
||||||
|
"from" => "d1",
|
||||||
|
"to" => "d7",
|
||||||
|
"san" => "Qd7+",
|
||||||
|
"prev_fen" => endgame.fen,
|
||||||
|
"fen" => fen_after,
|
||||||
|
"endgame_id" => endgame.id
|
||||||
|
})
|
||||||
|
|
||||||
|
assert render(view) =~ "Solved!"
|
||||||
|
assert Endgames.get_endgame!(endgame.id).times_solved == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "solves on a promotion that keeps the win", %{conn: conn} do
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}]
|
||||||
|
seed_tablebase(endgame.fen, moves: moves)
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||||
|
|
||||||
|
render_hook(view, "endgame_move_played", %{
|
||||||
|
"from" => "a7",
|
||||||
|
"to" => "a8",
|
||||||
|
"san" => "a8=Q",
|
||||||
|
"promotion" => "q",
|
||||||
|
"prev_fen" => endgame.fen,
|
||||||
|
"fen" => endgame.fen,
|
||||||
|
"endgame_id" => endgame.id
|
||||||
|
})
|
||||||
|
|
||||||
|
assert render(view) =~ "Solved!"
|
||||||
|
assert Endgames.get_endgame!(endgame.id).times_solved == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fails on a losing move and reveals the best move", %{conn: conn} do
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||||
|
|
||||||
|
play_move(view, endgame, "d1", "d4", "Qd4")
|
||||||
|
|
||||||
|
html = render(view)
|
||||||
|
assert html =~ "Failed"
|
||||||
|
assert html =~ "Qd7+"
|
||||||
|
assert Endgames.get_endgame!(endgame.id).times_solved == 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -240,6 +310,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
assert html =~ "No Moves"
|
assert html =~ "No Moves"
|
||||||
refute html =~ "Optimal move"
|
refute html =~ "Optimal move"
|
||||||
assert_init_board(view, endgame.fen, "white", "white")
|
assert_init_board(view, endgame.fen, "white", "white")
|
||||||
|
assert Endgames.get_endgame!(endgame.id).times_attempted == 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user