add winning conditions and ability to mark them in the graph
This commit is contained in:
@ -56,7 +56,7 @@ auto State::AddBlock(Block block) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
int index = 5 + (width * block.y + block.x) * 2;
|
||||
int index = GetIndex(block.x, block.y);
|
||||
state.replace(index, 2, block.ToString());
|
||||
|
||||
return true;
|
||||
@ -76,13 +76,21 @@ auto State::GetBlock(int x, int y) const -> Block {
|
||||
return Block::Invalid();
|
||||
}
|
||||
|
||||
auto State::GetBlockAt(int x, int y) const -> std::string {
|
||||
return state.substr(GetIndex(x, y), 2);
|
||||
}
|
||||
|
||||
auto State::GetIndex(int x, int y) const -> int {
|
||||
return 5 + (y * width + x) * 2;
|
||||
}
|
||||
|
||||
auto State::RemoveBlock(int x, int y) -> bool {
|
||||
Block b = GetBlock(x, y);
|
||||
if (!b.IsValid()) {
|
||||
Block block = GetBlock(x, y);
|
||||
if (!block.IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = 5 + (width * b.y + b.x) * 2;
|
||||
int index = GetIndex(block.x, block.y);
|
||||
state.replace(index, 2, "..");
|
||||
|
||||
return true;
|
||||
|
||||
80
src/main.cpp
80
src/main.cpp
@ -22,46 +22,59 @@ auto state_simple_1r() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_simple_1r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_simple_1f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, true));
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_simple_1f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_simple_2r() -> State {
|
||||
State s = State(4, 5, true);
|
||||
s.AddBlock(Block(0, 0, 1, 2, true));
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_simple_2r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_simple_2f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, true));
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
s.AddBlock(Block(1, 0, 1, 2, false));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_simple_2f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_simple_3r() -> State {
|
||||
State s = State(4, 5, true);
|
||||
s.AddBlock(Block(0, 0, 1, 2, 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;
|
||||
}
|
||||
|
||||
auto state_simple_3r_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_simple_3f() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, 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;
|
||||
}
|
||||
|
||||
auto state_simple_3f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_complex_1r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(1, 0, 1, 3, false));
|
||||
@ -74,6 +87,10 @@ auto state_complex_1r() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_complex_1r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
auto state_complex_2r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(2, 0, 1, 3, false));
|
||||
@ -86,6 +103,10 @@ auto state_complex_2r() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_complex_2r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
auto state_complex_3r() -> State {
|
||||
State s = State(6, 6, true);
|
||||
s.AddBlock(Block(0, 0, 3, 1, false));
|
||||
@ -102,12 +123,16 @@ auto state_complex_3r() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_complex_3r_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(4, 2) == "ba";
|
||||
}
|
||||
|
||||
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, true));
|
||||
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));
|
||||
@ -116,6 +141,8 @@ auto state_complex_4f() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_complex_4f_wc(const State &state) -> bool { return false; }
|
||||
|
||||
auto state_klotski() -> State {
|
||||
State s = State(4, 5, false);
|
||||
s.AddBlock(Block(0, 0, 1, 2, false));
|
||||
@ -132,17 +159,26 @@ auto state_klotski() -> State {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto state_klotski_wc(const State &state) -> bool {
|
||||
return state.GetBlockAt(1, 3) == "bb";
|
||||
}
|
||||
|
||||
std::array<StateGenerator, 8> generators{
|
||||
state_simple_1r, state_simple_2r, state_simple_3r, state_complex_1r,
|
||||
state_complex_2r, state_complex_3r, state_complex_4f, state_klotski};
|
||||
|
||||
std::array<WinCondition, 8> 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_klotski_wc};
|
||||
|
||||
auto apply_state(MassSpringSystem &mass_springs, StateGenerator generator)
|
||||
-> State {
|
||||
mass_springs.springs.clear();
|
||||
mass_springs.masses.clear();
|
||||
|
||||
State s = generator();
|
||||
mass_springs.AddMass(1.0, Vector3Zero(), false, s.state);
|
||||
mass_springs.AddMass(MASS, Vector3Zero(), false, s.state);
|
||||
|
||||
return s;
|
||||
};
|
||||
@ -157,7 +193,7 @@ auto solve_closure(MassSpringSystem &mass_springs, const State board) -> void {
|
||||
static_cast<float>(GetRandomValue(-10000, 10000)) / 1000.0,
|
||||
static_cast<float>(GetRandomValue(-10000, 10000)) / 1000.0);
|
||||
|
||||
mass_springs.AddMass(1.0, pos, false, state);
|
||||
mass_springs.AddMass(MASS, pos, false, state);
|
||||
}
|
||||
for (const auto &[from, to] : closure.second) {
|
||||
mass_springs.AddSpring(from, to, SPRING_CONSTANT, DAMPENING_CONSTANT,
|
||||
@ -196,9 +232,9 @@ auto main(int argc, char *argv[]) -> int {
|
||||
Renderer renderer;
|
||||
|
||||
// Klotski configuration
|
||||
int current_generator = 0;
|
||||
int current_preset = 0;
|
||||
MassSpringSystem masssprings;
|
||||
State board = apply_state(masssprings, generators[current_generator]);
|
||||
State board = apply_state(masssprings, generators[current_preset]);
|
||||
|
||||
// Game loop
|
||||
float frametime;
|
||||
@ -263,34 +299,40 @@ auto main(int argc, char *argv[]) -> int {
|
||||
} else if (IsKeyPressed(KEY_P)) {
|
||||
std::cout << board.state << std::endl;
|
||||
} else if (IsKeyPressed(KEY_N)) {
|
||||
current_generator =
|
||||
(generators.size() + current_generator - 1) % generators.size();
|
||||
board = apply_state(masssprings, generators[current_generator]);
|
||||
current_preset =
|
||||
(generators.size() + current_preset - 1) % generators.size();
|
||||
board = apply_state(masssprings, generators[current_preset]);
|
||||
previous_state = board.state;
|
||||
} else if (IsKeyPressed(KEY_M)) {
|
||||
current_generator = (current_generator + 1) % generators.size();
|
||||
board = apply_state(masssprings, generators[current_generator]);
|
||||
current_preset = (current_preset + 1) % generators.size();
|
||||
board = apply_state(masssprings, generators[current_preset]);
|
||||
previous_state = board.state;
|
||||
} else if (IsKeyPressed(KEY_R)) {
|
||||
board = generators[current_generator]();
|
||||
board = generators[current_preset]();
|
||||
} else if (IsKeyPressed(KEY_C)) {
|
||||
solve_closure(masssprings, board);
|
||||
renderer.UpdateWinningStates(masssprings, win_conditions[current_preset]);
|
||||
} else if (IsKeyPressed(KEY_G)) {
|
||||
masssprings.masses.clear();
|
||||
masssprings.springs.clear();
|
||||
masssprings.AddMass(1.0, Vector3Zero(), false, board.state);
|
||||
masssprings.AddMass(MASS, Vector3Zero(), false, board.state);
|
||||
previous_state = board.state;
|
||||
} else if (IsKeyPressed(KEY_I)) {
|
||||
renderer.mark_solutions = !renderer.mark_solutions;
|
||||
} else if (IsKeyPressed(KEY_O)) {
|
||||
renderer.connect_solutions = !renderer.connect_solutions;
|
||||
}
|
||||
|
||||
if (previous_state != board.state) {
|
||||
masssprings.AddMass(
|
||||
1.0,
|
||||
MASS,
|
||||
Vector3(static_cast<float>(GetRandomValue(-1000, 1000)) / 1000.0,
|
||||
static_cast<float>(GetRandomValue(-1000, 1000)) / 1000.0,
|
||||
static_cast<float>(GetRandomValue(-1000, 1000)) / 1000.0),
|
||||
false, board.state);
|
||||
masssprings.AddSpring(board.state, previous_state, SPRING_CONSTANT,
|
||||
DAMPENING_CONSTANT, REST_LENGTH);
|
||||
renderer.AddWinningState(board, win_conditions[current_preset]);
|
||||
}
|
||||
|
||||
// Physics update
|
||||
@ -314,7 +356,7 @@ auto main(int argc, char *argv[]) -> int {
|
||||
renderer.UpdateCamera(masssprings, board);
|
||||
renderer.DrawMassSprings(masssprings, board);
|
||||
renderer.DrawKlotski(board, hov_x, hov_y, sel_x, sel_y);
|
||||
renderer.DrawMenu(masssprings);
|
||||
renderer.DrawMenu(masssprings, current_preset);
|
||||
renderer.DrawTextures();
|
||||
std::chrono::high_resolution_clock::time_point re =
|
||||
std::chrono::high_resolution_clock::now();
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <raymath.h>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "klotski.hpp"
|
||||
#include "mass_springs.hpp"
|
||||
|
||||
auto OrbitCamera3D::Update(const Mass ¤t_mass) -> void {
|
||||
@ -45,7 +46,13 @@ auto OrbitCamera3D::Update(const Mass ¤t_mass) -> void {
|
||||
Vector2 dx = Vector2Subtract(mouse, last_mouse);
|
||||
last_mouse = mouse;
|
||||
|
||||
float speed = distance * PAN_SPEED / 1000.0;
|
||||
// float speed = PAN_SPEED;
|
||||
float speed;
|
||||
if (IsKeyDown(KEY_LEFT_SHIFT)) {
|
||||
speed = distance * PAN_SPEED / 1000.0 * PAN_MULTIPLIER;
|
||||
} else {
|
||||
speed = distance * PAN_SPEED / 1000.0;
|
||||
}
|
||||
Vector3 forward =
|
||||
Vector3Normalize(Vector3Subtract(camera.target, camera.position));
|
||||
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, camera.up));
|
||||
@ -80,6 +87,27 @@ auto OrbitCamera3D::Update(const Mass ¤t_mass) -> void {
|
||||
camera.target = target;
|
||||
}
|
||||
|
||||
auto Renderer::UpdateWinningStates(const MassSpringSystem &masssprings,
|
||||
const WinCondition win_condition) -> void {
|
||||
winning_states.clear();
|
||||
winning_states.reserve(masssprings.masses.size());
|
||||
for (const auto &[state, mass] : masssprings.masses) {
|
||||
if (win_condition(state)) {
|
||||
winning_states.insert(state);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Found " << winning_states.size() << " winning states."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
auto Renderer::AddWinningState(const State &state,
|
||||
const WinCondition win_condition) -> void {
|
||||
if (win_condition(state)) {
|
||||
winning_states.insert(state);
|
||||
}
|
||||
}
|
||||
|
||||
auto Renderer::UpdateCamera(const MassSpringSystem &masssprings,
|
||||
const State ¤t) -> void {
|
||||
const Mass &c = masssprings.masses.at(current.state);
|
||||
@ -102,16 +130,25 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &masssprings,
|
||||
}
|
||||
|
||||
// Draw masses (high performance impact)
|
||||
if (masssprings.masses.size() <= 5000) {
|
||||
for (const auto &[state, mass] : masssprings.masses) {
|
||||
for (const auto &[state, mass] : masssprings.masses) {
|
||||
if (state == current.state) {
|
||||
DrawCube(mass.position, 4 * VERTEX_SIZE, 4 * VERTEX_SIZE, 4 * VERTEX_SIZE,
|
||||
RED);
|
||||
} else if (winning_states.contains(state)) {
|
||||
if (mark_solutions) {
|
||||
DrawCube(mass.position, 4 * VERTEX_SIZE, 4 * VERTEX_SIZE,
|
||||
4 * VERTEX_SIZE, BLUE);
|
||||
}
|
||||
if (connect_solutions) {
|
||||
DrawLine3D(mass.position, masssprings.masses.at(current.state).position,
|
||||
PURPLE);
|
||||
}
|
||||
} else if (masssprings.masses.size() <= DRAW_VERTICES_LIMIT) {
|
||||
DrawCube(mass.position, VERTEX_SIZE, VERTEX_SIZE, VERTEX_SIZE,
|
||||
VERTEX_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
const Mass &c = masssprings.masses.at(current.state);
|
||||
DrawCube(c.position, 2 * VERTEX_SIZE, 2 * VERTEX_SIZE, 2 * VERTEX_SIZE, RED);
|
||||
|
||||
// DrawGrid(10, 1.0);
|
||||
// DrawSphere(camera.target, VERTEX_SIZE, ORANGE);
|
||||
EndMode3D();
|
||||
@ -203,7 +240,8 @@ auto Renderer::DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
|
||||
EndTextureMode();
|
||||
}
|
||||
|
||||
auto Renderer::DrawMenu(const MassSpringSystem &masssprings) -> void {
|
||||
auto Renderer::DrawMenu(const MassSpringSystem &masssprings, int current_preset)
|
||||
-> void {
|
||||
BeginTextureMode(menu_target);
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
@ -237,10 +275,13 @@ auto Renderer::DrawMenu(const MassSpringSystem &masssprings) -> void {
|
||||
DARKGREEN);
|
||||
|
||||
draw_btn(1, 0, std::format("Reset Board State (R)"), DARKBLUE);
|
||||
draw_btn(1, 1, std::format("Switch to Next Preset (M)"), DARKBLUE);
|
||||
draw_btn(1, 2, std::format("Switch to Previous Preset (N)"), DARKBLUE);
|
||||
draw_btn(1, 1, std::format("Preset (M/N): {}", current_preset), DARKBLUE);
|
||||
draw_btn(1, 2, std::format("Print Board State to Console (P)"), DARKBLUE);
|
||||
|
||||
draw_btn(2, 0, std::format("Print Board State to Console (P)"), DARKPURPLE);
|
||||
draw_btn(2, 0,
|
||||
std::format("Mark (I): {} / Connect (O): {}", mark_solutions,
|
||||
connect_solutions),
|
||||
DARKPURPLE);
|
||||
draw_btn(2, 1, std::format("Solve Board Closure (C)"), DARKPURPLE);
|
||||
draw_btn(2, 2, std::format("Clear Graph (G)"), DARKPURPLE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user