Simplify the Tablebase struct to the fields the grader reads (#26)

Drop dtm/dtc from Tablebase and Tablebase.Move; the local Syzygy backend cannot produce them and nothing reads them. The Lichess parser now maps only category, checkmate and moves (with per-move category, dtz, san and uci), and the struct-shape tests follow.
This commit is contained in:
2026-11-03 12:41:28 +01:00
parent ff7adcf308
commit ec657c3424
4 changed files with 12 additions and 24 deletions
+3 -7
View File
@@ -3,9 +3,9 @@ defmodule Chesstrainer.Endgames.Tablebase do
All endgame tablebase related functions. All endgame tablebase related functions.
The cached value carries only what the trainer reads: `category`, The cached value carries only what the trainer reads: `category`,
`checkmate`, and `moves` drive play, while `dtm`/`dtc` are small distance `checkmate`, and `moves`. Cursed-win/blessed-loss are normalised to `:draw` at
hedges. Cursed-win/blessed-loss are normalised to `:draw` at parse time, so parse time, and distance fields (`dtm`/`dtc`/`dtw`) are not kept because the
the raw `dtw`/`dtz`/`insufficient_material` fields are not kept here. local Syzygy backend cannot produce them.
""" """
alias Chesstrainer.Endgames.Tablebase alias Chesstrainer.Endgames.Tablebase
@@ -15,15 +15,11 @@ defmodule Chesstrainer.Endgames.Tablebase do
@type t :: %__MODULE__{ @type t :: %__MODULE__{
category: result, category: result,
checkmate: boolean(), checkmate: boolean(),
dtc: integer() | nil,
dtm: integer() | nil,
moves: [Tablebase.Move.t()] moves: [Tablebase.Move.t()]
} }
defstruct category: nil, defstruct category: nil,
checkmate: false, checkmate: false,
dtc: nil,
dtm: nil,
moves: nil moves: nil
@doc """ @doc """
+3 -7
View File
@@ -3,25 +3,21 @@ defmodule Chesstrainer.Endgames.Tablebase.Move do
Moves struct for the tablebase Moves struct for the tablebase
Carries only the fields the trainer reads: `category` and `dtz` for grading, Carries only the fields the trainer reads: `category` and `dtz` for grading,
`san`/`uci` for display and matching, and `dtm`/`dtc` as small distance and `san`/`uci` for display and matching. Terminal detection is structural
hedges. Terminal detection is structural (the next position's `checkmate` or (the next position's `checkmate` or its empty `moves`), so no per-move
its empty `moves`), so no per-move terminal flags are kept. terminal flags are kept.
""" """
@type result :: :win | :draw | :loss | :unknown @type result :: :win | :draw | :loss | :unknown
@type t :: %__MODULE__{ @type t :: %__MODULE__{
category: result, category: result,
dtc: integer() | nil,
dtm: integer() | nil,
dtz: integer() | nil, dtz: integer() | nil,
san: String.t() | nil, san: String.t() | nil,
uci: String.t() | nil uci: String.t() | nil
} }
defstruct category: nil, defstruct category: nil,
dtc: nil,
dtm: nil,
dtz: nil, dtz: nil,
san: nil, san: nil,
uci: nil uci: nil
@@ -43,8 +43,6 @@ defmodule Chesstrainer.Endgames.Tablebase.Parser do
%Tablebase{ %Tablebase{
category: Tablebase.normalize_category(map["category"]), category: Tablebase.normalize_category(map["category"]),
checkmate: map["checkmate"], checkmate: map["checkmate"],
dtc: map["dtc"],
dtm: map["dtm"],
moves: Enum.map(map["moves"], &moves_from_map/1) moves: Enum.map(map["moves"], &moves_from_map/1)
}} }}
end end
@@ -52,8 +50,6 @@ defmodule Chesstrainer.Endgames.Tablebase.Parser do
defp moves_from_map(map) do defp moves_from_map(map) do
%Tablebase.Move{ %Tablebase.Move{
category: Tablebase.normalize_category(map["category"]), category: Tablebase.normalize_category(map["category"]),
dtc: map["dtc"],
dtm: map["dtm"],
dtz: map["dtz"], dtz: map["dtz"],
san: map["san"], san: map["san"],
uci: map["uci"] uci: map["uci"]
@@ -35,12 +35,12 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
describe "struct shape" do describe "struct shape" do
test "Tablebase carries only the fields the trainer reads" do test "Tablebase carries only the fields the trainer reads" do
assert %Tablebase{} |> Map.from_struct() |> Map.keys() |> Enum.sort() == assert %Tablebase{} |> Map.from_struct() |> Map.keys() |> Enum.sort() ==
[:category, :checkmate, :dtc, :dtm, :moves] [:category, :checkmate, :moves]
end end
test "Tablebase.Move carries only the fields the trainer reads" do test "Tablebase.Move carries only the fields the trainer reads" do
assert %Tablebase.Move{} |> Map.from_struct() |> Map.keys() |> Enum.sort() == assert %Tablebase.Move{} |> Map.from_struct() |> Map.keys() |> Enum.sort() ==
[:category, :dtc, :dtm, :dtz, :san, :uci] [:category, :dtz, :san, :uci]
end end
end end
@@ -65,9 +65,9 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
] ]
}) })
assert %Tablebase{category: :draw, checkmate: false, dtc: nil, dtm: 20} = tablebase assert %Tablebase{category: :draw, checkmate: false} = tablebase
assert [%Tablebase.Move{category: :draw, dtc: nil, dtm: -19, dtz: -2}] = tablebase.moves assert [%Tablebase.Move{category: :draw, dtz: -2}] = tablebase.moves
assert [%Tablebase.Move{san: "Kd7", uci: "e8d7"}] = tablebase.moves assert [%Tablebase.Move{san: "Kd7", uci: "e8d7"}] = tablebase.moves
end end
@@ -103,12 +103,12 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
}) })
assert %Tablebase{} |> Map.from_struct() |> Map.keys() |> Enum.sort() == assert %Tablebase{} |> Map.from_struct() |> Map.keys() |> Enum.sort() ==
[:category, :checkmate, :dtc, :dtm, :moves] [:category, :checkmate, :moves]
assert [move] = tablebase.moves assert [move] = tablebase.moves
assert move |> Map.from_struct() |> Map.keys() |> Enum.sort() == assert move |> Map.from_struct() |> Map.keys() |> Enum.sort() ==
[:category, :dtc, :dtm, :dtz, :san, :uci] [:category, :dtz, :san, :uci]
end end
test "keeps checkmate and empty moves, which drive terminal detection" do test "keeps checkmate and empty moves, which drive terminal detection" do