refactor usage of std::string to refer to states + improve initial mass positioning

This commit is contained in:
2026-02-22 00:12:47 +01:00
parent 0d3913e27e
commit cc4f8ce865
7 changed files with 66 additions and 57 deletions

View File

@ -190,7 +190,7 @@ public:
#endif
}
State(std::string state)
explicit State(std::string state)
: width(std::stoi(state.substr(1, 1))),
height(std::stoi(state.substr(3, 1))),
restricted(state.substr(0, 1) == "R"), state(state) {
@ -278,15 +278,30 @@ public:
auto GetNextStates() const -> std::vector<State>;
auto Closure() const
-> std::pair<std::unordered_set<std::string>,
std::vector<std::pair<std::string, std::string>>>;
auto Closure() const -> std::pair<std::unordered_set<State>,
std::vector<std::pair<State, State>>>;
};
template <> struct std::hash<State> {
std::size_t operator()(const State &s) const noexcept { return s.Hash(); }
};
template <> struct std::hash<std::pair<State, State>> {
std::size_t operator()(const std::pair<State, State> &s) const noexcept {
auto h1 = std::hash<State>{}(s.first);
auto h2 = std::hash<State>{}(s.second);
return h1 + h2 + (h1 * h2);
}
};
template <> struct std::equal_to<std::pair<State, State>> {
bool operator()(const std::pair<State, State> &a,
const std::pair<State, State> &b) const noexcept {
return (a.first == b.first && a.second == b.second) ||
(a.first == b.second && a.second == b.first);
}
};
using WinCondition = std::function<bool(const State &)>;
#endif

View File

@ -5,7 +5,6 @@
#include "klotski.hpp"
#include <raylib.h>
#include <raymath.h>
#include <string>
#include <unordered_map>
#include <vector>
@ -87,6 +86,7 @@ public:
class MassSpringSystem {
private:
// TODO: Use references
std::vector<Mass *> mass_vec;
std::vector<int> indices;
std::vector<int64_t> cell_ids;
@ -95,8 +95,10 @@ private:
int last_springs_count;
public:
std::unordered_map<std::string, Mass> masses;
std::unordered_map<std::string, Spring> springs;
// This is the main ownership of all the states/masses/springs.
// Everything is stored multiple times but idc.
std::unordered_map<State, Mass> masses;
std::unordered_map<std::pair<State, State>, Spring> springs;
public:
MassSpringSystem() : last_build(REPULSION_GRID_REFRESH) {};
@ -112,8 +114,7 @@ private:
auto BuildGrid() -> void;
public:
auto AddMass(float mass, Vector3 position, bool fixed, const State &state)
-> void;
auto AddMass(float mass, bool fixed, const State &state) -> void;
auto GetMass(const State &state) -> Mass &;

View File

@ -25,7 +25,7 @@ public:
: mass_springs(mass_springs), current_preset(0),
current_state(generators[current_preset]()),
previous_state(current_state), edited(false) {
mass_springs.AddMass(MASS, Vector3Zero(), false, current_state);
mass_springs.AddMass(MASS, false, current_state);
}
StateManager(const StateManager &copy) = delete;