From 3f8274f1d9fe33d15c948b7268c80d960bf019b5 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Wed, 25 Feb 2026 03:20:40 +0100 Subject: [PATCH] add button to move to a state farthest from any win condition --- include/state.hpp | 4 ++++ src/distance.cpp | 1 - src/input.cpp | 3 ++- src/main.cpp | 4 +--- src/renderer.cpp | 3 ++- src/state.cpp | 26 ++++++++++++++++++++++++-- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/include/state.hpp b/include/state.hpp index b9d0792..ba2bbfd 100644 --- a/include/state.hpp +++ b/include/state.hpp @@ -82,6 +82,10 @@ public: auto FindTargetPath() -> void; + auto FindWorstState() -> State; + + auto GoToWorst() -> void; + auto CurrentMassIndex() const -> std::size_t; }; diff --git a/src/distance.cpp b/src/distance.cpp index a727ca8..da7cdc3 100644 --- a/src/distance.cpp +++ b/src/distance.cpp @@ -2,7 +2,6 @@ #include "config.hpp" #include -#include #include #ifdef TRACY diff --git a/src/input.cpp b/src/input.cpp index 5421d16..d933c45 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,6 +1,5 @@ #include "input.hpp" #include "config.hpp" -#include "distance.hpp" #include #include @@ -154,6 +153,8 @@ auto InputHandler::HandleKeys() -> void { mark_path = !mark_path; } else if (IsKeyPressed(KEY_SPACE)) { state.NextPath(); + } else if (IsKeyPressed(KEY_V)) { + state.GoToWorst(); } else if (IsKeyPressed(KEY_F)) { state.current_state.ToggleRestricted(); state.ClearGraph(); diff --git a/src/main.cpp b/src/main.cpp index 53baec3..f71b9ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,6 @@ #include #include "config.hpp" -#include "distance.hpp" #include "input.hpp" #include "physics.hpp" #include "renderer.hpp" @@ -16,8 +15,7 @@ // TODO: Graph interaction // - Click states to display them in the board -// - Find shortest path to any winning state and mark it in the graph -// - Also mark the next move along the path on the board +// TODO: Add a move history and backspace to go back auto main(int argc, char *argv[]) -> int { std::string preset_file; diff --git a/src/renderer.cpp b/src/renderer.cpp index e947862..93535a9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -351,7 +351,8 @@ auto Renderer::DrawMenu(const std::vector &masses) -> void { input.mark_path, input.mark_solutions, input.connect_solutions), DARKPURPLE); - draw_btn(2, 3, std::format("Move along Path (Space)"), DARKPURPLE); + draw_btn(2, 3, std::format("Path forward (Space) / To worst (V)"), + DARKPURPLE); DrawLine(0, MENU_HEIGHT - 1, GetScreenWidth(), MENU_HEIGHT - 1, BLACK); EndTextureMode(); diff --git a/src/state.cpp b/src/state.cpp index 1d217d6..26e7c30 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -193,8 +193,30 @@ auto StateManager::FindTargetPath() -> void { } winning_path = GetPath(target_distances, CurrentMassIndex()); - // std::cout << "Nearest target is " << winning_path.size() << " moves away." - // << std::endl; + std::cout << "Nearest target is " << winning_path.size() << " moves away." + << std::endl; +} + +auto StateManager::FindWorstState() -> State { + if (target_distances.Empty()) { + return current_state; + } + + int max = 0; + int index = 0; + for (std::size_t i = 0; i < target_distances.distances.size(); ++i) { + if (target_distances.distances[i] > max) { + max = target_distances.distances[i]; + index = i; + } + } + + return masses.at(index); +} + +auto StateManager::GoToWorst() -> void { + current_state = FindWorstState(); + FindTargetPath(); } auto StateManager::CurrentMassIndex() const -> std::size_t {