store winning condition in state + remove presets
This commit is contained in:
12
src/main.cpp
12
src/main.cpp
@ -25,10 +25,12 @@
|
||||
// - Also mark the next move along the path on the board
|
||||
|
||||
auto main(int argc, char *argv[]) -> int {
|
||||
// if (argc < 2) {
|
||||
// std::cout << "Missing .klotski file." << std::endl;
|
||||
// return 1;
|
||||
// }
|
||||
std::string preset_file;
|
||||
if (argc != 2) {
|
||||
preset_file = "default.puzzle";
|
||||
} else {
|
||||
preset_file = argv[1];
|
||||
}
|
||||
|
||||
// RayLib window setup
|
||||
SetTraceLogLevel(LOG_ERROR);
|
||||
@ -40,7 +42,7 @@ auto main(int argc, char *argv[]) -> int {
|
||||
|
||||
// Game setup
|
||||
ThreadedPhysics physics;
|
||||
StateManager state(physics);
|
||||
StateManager state(physics, preset_file);
|
||||
InputHandler input(state);
|
||||
OrbitCamera3D camera;
|
||||
Renderer renderer(camera, state, input);
|
||||
|
||||
@ -53,6 +53,24 @@ auto Block::Collides(const Block &other) const -> bool {
|
||||
|
||||
auto State::Hash() const -> int { return std::hash<std::string>{}(state); }
|
||||
|
||||
auto State::HasWinCondition() const -> bool {
|
||||
return target_x == 9 || target_y == 9;
|
||||
}
|
||||
|
||||
auto State::IsWon() const -> bool {
|
||||
if (!HasWinCondition()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto &block : *this) {
|
||||
if (block.target) {
|
||||
return block.x == target_x && block.y == target_y;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto State::AddColumn() const -> State {
|
||||
State newstate = State(width + 1, height, restricted);
|
||||
|
||||
@ -129,7 +147,7 @@ auto State::GetBlockAt(int x, int y) const -> std::string {
|
||||
}
|
||||
|
||||
auto State::GetIndex(int x, int y) const -> int {
|
||||
return 5 + (y * width + x) * 2;
|
||||
return prefix + (y * width + x) * 2;
|
||||
}
|
||||
|
||||
auto State::RemoveBlock(int x, int y) -> bool {
|
||||
@ -185,36 +203,36 @@ auto State::MoveBlockAt(int x, int y, Direction dir) -> bool {
|
||||
Direction::WES;
|
||||
|
||||
// Get target block
|
||||
int target_x = block.x;
|
||||
int target_y = block.y;
|
||||
int _target_x = block.x;
|
||||
int _target_y = block.y;
|
||||
switch (dir) {
|
||||
case Direction::NOR:
|
||||
if (!(dirs & Direction::NOR) || target_y < 1) {
|
||||
if (!(dirs & Direction::NOR) || _target_y < 1) {
|
||||
return false;
|
||||
}
|
||||
target_y--;
|
||||
_target_y--;
|
||||
break;
|
||||
case Direction::EAS:
|
||||
if (!(dirs & Direction::EAS) || target_x + block.width >= width) {
|
||||
if (!(dirs & Direction::EAS) || _target_x + block.width >= width) {
|
||||
return false;
|
||||
}
|
||||
target_x++;
|
||||
_target_x++;
|
||||
break;
|
||||
case Direction::SOU:
|
||||
if (!(dirs & Direction::SOU) || target_y + block.height >= height) {
|
||||
if (!(dirs & Direction::SOU) || _target_y + block.height >= height) {
|
||||
return false;
|
||||
}
|
||||
target_y++;
|
||||
_target_y++;
|
||||
break;
|
||||
case Direction::WES:
|
||||
if (!(dirs & Direction::WES) || target_x < 1) {
|
||||
if (!(dirs & Direction::WES) || _target_x < 1) {
|
||||
return false;
|
||||
}
|
||||
target_x--;
|
||||
_target_x--;
|
||||
break;
|
||||
}
|
||||
Block target =
|
||||
Block(target_x, target_y, block.width, block.height, block.target);
|
||||
Block(_target_x, _target_y, block.width, block.height, block.target);
|
||||
|
||||
// Check collisions
|
||||
for (Block b : *this) {
|
||||
|
||||
@ -207,7 +207,7 @@ auto Renderer::DrawKlotski() -> void {
|
||||
DrawRectangle(x_offset, y_offset,
|
||||
board_width - 2 * x_offset + 2 * BOARD_PADDING,
|
||||
board_height - 2 * y_offset + 2 * BOARD_PADDING,
|
||||
state.CurrentWinCondition()(state.current_state)
|
||||
state.current_state.IsWon()
|
||||
? GREEN
|
||||
: (state.current_state.restricted ? DARKGRAY : LIGHTGRAY));
|
||||
for (int x = 0; x < state.current_state.width; ++x) {
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#include "state.hpp"
|
||||
#include "config.hpp"
|
||||
#include "presets.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <raymath.h>
|
||||
|
||||
#ifdef TRACY
|
||||
@ -9,30 +10,61 @@
|
||||
#include <tracy/Tracy.hpp>
|
||||
#endif
|
||||
|
||||
auto StateManager::ParsePresetFile(const std::string &preset_file) -> void {
|
||||
std::ifstream file(preset_file);
|
||||
if (!file) {
|
||||
std::cout << "Preset file \"" << preset_file << "\" couldn't be loaded."
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
std::vector<std::string> lines;
|
||||
while (std::getline(file, line)) {
|
||||
if (line.starts_with("F") || line.starts_with("R")) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
if (lines.size() == 0) {
|
||||
std::cout << "Preset file \"" << preset_file << "\" couldn't be loaded."
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
presets.clear();
|
||||
for (const auto &preset : lines) {
|
||||
presets.emplace_back(preset);
|
||||
}
|
||||
|
||||
std::cout << "Loaded " << lines.size() << " presets." << std::endl;
|
||||
}
|
||||
|
||||
auto StateManager::LoadPreset(int preset) -> void {
|
||||
current_preset = preset;
|
||||
current_state = CurrentGenerator()();
|
||||
current_state = presets.at(current_preset);
|
||||
ClearGraph();
|
||||
edited = false;
|
||||
}
|
||||
|
||||
auto StateManager::ResetState() -> void {
|
||||
current_state = CurrentGenerator()();
|
||||
current_state = presets.at(current_preset);
|
||||
previous_state = current_state;
|
||||
if (edited) {
|
||||
// We also need to clear the graph in case the state has been edited
|
||||
// because the graph could contain states that are impossible to reach now.
|
||||
// because the graph could contain states that are impossible to reach
|
||||
// now.
|
||||
ClearGraph();
|
||||
edited = false;
|
||||
}
|
||||
}
|
||||
|
||||
auto StateManager::PreviousPreset() -> void {
|
||||
LoadPreset((generators.size() + current_preset - 1) % generators.size());
|
||||
LoadPreset((presets.size() + current_preset - 1) % presets.size());
|
||||
}
|
||||
|
||||
auto StateManager::NextPreset() -> void {
|
||||
LoadPreset((current_preset + 1) % generators.size());
|
||||
LoadPreset((current_preset + 1) % presets.size());
|
||||
}
|
||||
|
||||
auto StateManager::FillGraph() -> void {
|
||||
@ -62,7 +94,7 @@ auto StateManager::UpdateGraph() -> void {
|
||||
}
|
||||
|
||||
visited_states.insert(current_state);
|
||||
if (win_conditions[current_preset](current_state)) {
|
||||
if (current_state.IsWon()) {
|
||||
winning_states.insert(current_state);
|
||||
}
|
||||
}
|
||||
@ -85,20 +117,12 @@ auto StateManager::ClearGraph() -> void {
|
||||
auto StateManager::FindWinningStates() -> void {
|
||||
winning_states.clear();
|
||||
for (const auto &[state, mass] : states) {
|
||||
if (CurrentWinCondition()(state)) {
|
||||
if (state.IsWon()) {
|
||||
winning_states.insert(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto StateManager::CurrentGenerator() const -> StateGenerator {
|
||||
return generators[current_preset];
|
||||
}
|
||||
|
||||
auto StateManager::CurrentWinCondition() const -> WinCondition {
|
||||
return win_conditions[current_preset];
|
||||
}
|
||||
|
||||
auto StateManager::CurrentMassIndex() const -> std::size_t {
|
||||
return states.at(current_state);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user