store winning condition in state + remove presets
This commit is contained in:
@ -4,9 +4,11 @@
|
||||
#include <raylib.h>
|
||||
|
||||
#define THREADPOOL // Enable physics threadpool
|
||||
#define TRACY // Enable tracy profiling support
|
||||
#define BACKWARD // Enable pretty stack traces
|
||||
|
||||
// Gets set by CMake
|
||||
// #define TRACY // Enable tracy profiling support
|
||||
|
||||
// Window
|
||||
constexpr int INITIAL_WIDTH = 800;
|
||||
constexpr int INITIAL_HEIGHT = 800;
|
||||
|
||||
@ -1,215 +0,0 @@
|
||||
#ifndef __PRESETS_HPP_
|
||||
#define __PRESETS_HPP_
|
||||
|
||||
#include "config.hpp"
|
||||
#include "puzzle.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
using StateGenerator = std::function<State(void)>;
|
||||
|
||||
inline auto state_simple_1r() -> State {
|
||||
State s = State(4, 5, true);
|
||||
s.AddBlock(Block(0, 0, 1, 2, true));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_1r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_simple_1f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_1f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_simple_2r() -> State {
|
||||
State s = State(4, 5, true);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_2r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_simple_2f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_2f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_simple_3r() -> State {
|
||||
State s = State(4, 5, true);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
s.AddBlock(Block(2, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_3r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_simple_3f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
s.AddBlock(Block(2, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_simple_3f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_complex_1r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(1, 0, 1, 3, false));
|
||||
s.AddBlock(Block(3, 0, 2, 1, false));
|
||||
s.AddBlock(Block(5, 0, 1, 3, false));
|
||||
s.AddBlock(Block(3, 2, 2, 1, true));
|
||||
s.AddBlock(Block(3, 3, 1, 2, false));
|
||||
s.AddBlock(Block(4, 4, 2, 1, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_complex_1r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
inline auto state_complex_2r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(2, 0, 1, 3, false));
|
||||
s.AddBlock(Block(0, 2, 2, 1, true));
|
||||
s.AddBlock(Block(1, 3, 2, 1, false));
|
||||
s.AddBlock(Block(1, 4, 2, 1, false));
|
||||
s.AddBlock(Block(5, 4, 1, 2, false));
|
||||
s.AddBlock(Block(0, 5, 3, 1, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_complex_2r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
inline auto state_complex_3r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(0, 0, 3, 1, false));
|
||||
s.AddBlock(Block(5, 0, 1, 3, false));
|
||||
s.AddBlock(Block(2, 2, 1, 2, false));
|
||||
s.AddBlock(Block(3, 2, 2, 1, true));
|
||||
s.AddBlock(Block(3, 3, 1, 2, false));
|
||||
s.AddBlock(Block(4, 3, 2, 1, false));
|
||||
s.AddBlock(Block(0, 4, 1, 2, false));
|
||||
s.AddBlock(Block(2, 4, 1, 2, false));
|
||||
s.AddBlock(Block(4, 4, 2, 1, false));
|
||||
s.AddBlock(Block(3, 5, 3, 1, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_complex_3r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
inline auto state_complex_4f() -> State {
|
||||
State s = State(4, 4, false);
|
||||
s.AddBlock(Block(0, 0, 2, 1, false));
|
||||
s.AddBlock(Block(3, 0, 1, 1, false));
|
||||
s.AddBlock(Block(0, 1, 1, 2, false));
|
||||
s.AddBlock(Block(1, 1, 2, 2, false));
|
||||
s.AddBlock(Block(3, 1, 1, 1, false));
|
||||
s.AddBlock(Block(3, 2, 1, 1, false));
|
||||
s.AddBlock(Block(0, 3, 1, 1, false));
|
||||
s.AddBlock(Block(1, 3, 1, 1, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_complex_4f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
inline auto state_complex_5r() -> State {
|
||||
return State("R6x6:31....12....1221......12..ba..1212..21..12....12......21.."
|
||||
"....21..21....");
|
||||
}
|
||||
|
||||
inline auto state_complex_5r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
inline auto state_complex_6r() -> State {
|
||||
return State(
|
||||
"R6x6:1231....1213..31........ba..121212..21................1221....."
|
||||
".....21..");
|
||||
}
|
||||
|
||||
inline auto state_complex_6r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
inline auto state_klotski() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 2, 2, true));
|
||||
s.AddBlock(Block(3, 0, 1, 2, false));
|
||||
s.AddBlock(Block(0, 2, 1, 2, false));
|
||||
s.AddBlock(Block(1, 2, 2, 1, false));
|
||||
s.AddBlock(Block(3, 2, 1, 2, false));
|
||||
s.AddBlock(Block(1, 3, 1, 1, false));
|
||||
s.AddBlock(Block(2, 3, 1, 1, false));
|
||||
s.AddBlock(Block(0, 4, 1, 1, false));
|
||||
s.AddBlock(Block(3, 4, 1, 1, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
inline auto state_klotski_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(1, 3) == "bb";
|
||||
}
|
||||
|
||||
inline auto state_century() -> State {
|
||||
return State("F4x5:11bb..1112....12..12....11....1121..21..");
|
||||
}
|
||||
|
||||
inline auto state_century_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(1, 3) == "bb";
|
||||
}
|
||||
|
||||
inline auto state_super_century() -> State {
|
||||
return State("F4x5:12111111..12bb..12........21..11....21..");
|
||||
}
|
||||
|
||||
inline auto state_super_century_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(1, 3) == "bb";
|
||||
}
|
||||
|
||||
inline auto state_new_century() -> State {
|
||||
return State("F4x5:12111111..12bb..12........21..11....21..");
|
||||
}
|
||||
|
||||
inline auto state_new_century_wc(const State &state) -> bool {
|
||||
// What kind of brain do you need for this???
|
||||
return state.state == "F4x5:21......1121..12bb..12........12111111..";
|
||||
}
|
||||
|
||||
static std::vector<StateGenerator> generators{
|
||||
state_simple_1r, state_simple_2r, state_simple_3r, state_complex_1r,
|
||||
state_complex_2r, state_complex_3r, state_complex_4f, state_complex_5r,
|
||||
state_complex_6r, state_klotski, state_century, state_super_century};
|
||||
|
||||
static std::vector<WinCondition> win_conditions{
|
||||
state_simple_1r_wc, state_simple_2r_wc, state_simple_3r_wc,
|
||||
state_complex_1r_wc, state_complex_2r_wc, state_complex_3r_wc,
|
||||
state_complex_4f_wc, state_complex_5r_wc, state_complex_6r_wc,
|
||||
state_klotski_wc, state_century_wc, state_super_century_wc};
|
||||
|
||||
#endif
|
||||
@ -102,16 +102,21 @@ public:
|
||||
auto Collides(const Block &other) const -> bool;
|
||||
};
|
||||
|
||||
// A state is represented by a string "WxH:blocks", where W is the board width,
|
||||
// H is the board height and blocks is an enumeration of each of the board's
|
||||
// cells, with each cell being a 2-letter or 2-digit block representation (a 3x3
|
||||
// board would have a string representation with length 4 + 3*3 * 2).
|
||||
// The board's cells are enumerated from top-left to bottom-right with each
|
||||
// block's pivot being its top-left corner.
|
||||
// A state is represented by a string "MWHXYblocks", where M is "R"
|
||||
// (restricted) or "F" (free), W is the board width, H is the board height, X is
|
||||
// the target block x goal, Y is the target block y goal and blocks is an
|
||||
// enumeration of each of the board's cells, with each cell being a 2-letter or
|
||||
// 2-digit block representation (a 3x3 board would have a string representation
|
||||
// with length 5 + 3*3 * 2). The board's cells are enumerated from top-left to
|
||||
// bottom-right with each block's pivot being its top-left corner.
|
||||
class State {
|
||||
public:
|
||||
static constexpr int prefix = 5;
|
||||
|
||||
int width;
|
||||
int height;
|
||||
int target_x;
|
||||
int target_y;
|
||||
bool restricted; // Only allow blocks to move in their principal direction
|
||||
std::string state;
|
||||
|
||||
@ -133,13 +138,13 @@ public:
|
||||
|
||||
Block operator*() const {
|
||||
return Block(current_pos % state.width, current_pos / state.width,
|
||||
state.state.substr(current_pos * 2 + 5, 2));
|
||||
state.state.substr(current_pos * 2 + prefix, 2));
|
||||
}
|
||||
|
||||
BlockIterator &operator++() {
|
||||
do {
|
||||
current_pos++;
|
||||
} while (state.state.substr(current_pos * 2 + 5, 2) == "..");
|
||||
} while (state.state.substr(current_pos * 2 + prefix, 2) == "..");
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -151,28 +156,51 @@ public:
|
||||
};
|
||||
|
||||
public:
|
||||
State(int _width, int _height, bool _restricted)
|
||||
: width(_width), height(_height), restricted(_restricted),
|
||||
state(std::format("{}{}x{}:{}", _restricted ? "R" : "F", _width,
|
||||
_height, std::string(_width * _height * 2, '.'))) {
|
||||
if (_width <= 0 || _width >= 10 || _height <= 0 || _height >= 10) {
|
||||
State(int _width, int _height, int _target_x, int _target_y, bool _restricted)
|
||||
: width(_width), height(_height), target_x(_target_x),
|
||||
target_y(_target_y), restricted(_restricted),
|
||||
state(std::format("{}{}{}{}{}{}", _restricted ? "R" : "F", _width,
|
||||
_height, _target_x, _target_y,
|
||||
std::string(_width * _height * 2, '.'))) {
|
||||
if (_width < 1 || _width > 9 || _height < 1 || _height > 9) {
|
||||
std::cerr << "State width/height must be in [1, 9]!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
if (_target_x < 0 || _target_x >= 9 || _target_y < 0 || _target_y >= 9) {
|
||||
if (_target_x != 9 && _target_y != 9) {
|
||||
std::cerr << "State target must be within the board bounds!"
|
||||
<< std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
State(int _width, int _height, bool _restricted)
|
||||
: State(_width, _height, 9, 9, _restricted) {}
|
||||
|
||||
State() : State(4, 5, 9, 9, false) {}
|
||||
|
||||
explicit State(std::string _state)
|
||||
: width(std::stoi(_state.substr(1, 1))),
|
||||
height(std::stoi(_state.substr(3, 1))),
|
||||
height(std::stoi(_state.substr(2, 1))),
|
||||
target_x(std::stoi(_state.substr(3, 1))),
|
||||
target_y(std::stoi(_state.substr(4, 1))),
|
||||
restricted(_state.substr(0, 1) == "R"), state(_state) {
|
||||
if (width <= 0 || width >= 10 || height <= 0 || height >= 10) {
|
||||
if (width < 1 || width > 9 || height < 1 || height > 9) {
|
||||
std::cerr << "State width/height must be in [1, 9]!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
if (static_cast<int>(_state.length()) != width * height * 2 + 5) {
|
||||
if (target_x < 0 || target_x >= 9 || target_y < 0 || target_y >= 9) {
|
||||
if (target_x != 9 && target_y != 9) {
|
||||
std::cerr << "State target must be within the board bounds!"
|
||||
<< std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (static_cast<int>(_state.length()) != width * height * 2 + prefix) {
|
||||
std::cerr
|
||||
<< "State representation must have length [width * height * 2 + 5]!"
|
||||
<< std::endl;
|
||||
<< "State representation must have length [width * height * 2 + "
|
||||
<< prefix << "]!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@ -194,6 +222,10 @@ public:
|
||||
public:
|
||||
auto Hash() const -> int;
|
||||
|
||||
auto HasWinCondition() const -> bool;
|
||||
|
||||
auto IsWon() const -> bool;
|
||||
|
||||
auto AddColumn() const -> State;
|
||||
|
||||
auto RemoveColumn() const -> State;
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
#include "config.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "presets.hpp"
|
||||
#include "puzzle.hpp"
|
||||
|
||||
#include <raymath.h>
|
||||
@ -14,6 +13,8 @@ class StateManager {
|
||||
public:
|
||||
ThreadedPhysics &physics;
|
||||
|
||||
std::vector<State> presets;
|
||||
|
||||
std::unordered_map<State, std::size_t> states;
|
||||
std::unordered_set<State> winning_states;
|
||||
std::unordered_set<State> visited_states;
|
||||
@ -26,11 +27,11 @@ public:
|
||||
bool edited = false;
|
||||
|
||||
public:
|
||||
StateManager(ThreadedPhysics &_physics)
|
||||
: physics(_physics), current_preset(0),
|
||||
starting_state(generators[current_preset]()),
|
||||
current_state(starting_state), previous_state(starting_state),
|
||||
StateManager(ThreadedPhysics &_physics, const std::string &preset_file)
|
||||
: physics(_physics), presets({State()}), current_preset(0),
|
||||
edited(false) {
|
||||
ParsePresetFile(preset_file);
|
||||
current_state = presets.at(current_preset);
|
||||
ClearGraph();
|
||||
}
|
||||
|
||||
@ -41,6 +42,9 @@ public:
|
||||
|
||||
~StateManager() {}
|
||||
|
||||
private:
|
||||
auto ParsePresetFile(const std::string &preset_file) -> void;
|
||||
|
||||
public:
|
||||
auto LoadPreset(int preset) -> void;
|
||||
|
||||
@ -58,10 +62,6 @@ public:
|
||||
|
||||
auto FindWinningStates() -> void;
|
||||
|
||||
auto CurrentGenerator() const -> StateGenerator;
|
||||
|
||||
auto CurrentWinCondition() const -> WinCondition;
|
||||
|
||||
auto CurrentMassIndex() const -> std::size_t;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user