Compare commits

...

2 Commits

Author SHA1 Message Date
9d0afffb57 render klotski board 2026-02-17 13:27:12 +01:00
017d11245c begin implementing klotski logic 2026-02-17 13:27:05 +01:00
7 changed files with 469 additions and 11 deletions

View File

@ -12,6 +12,7 @@ add_executable(masssprings
src/main.cpp
src/renderer.cpp
src/mass_springs.cpp
src/klotski.cpp
)
target_include_directories(masssprings PUBLIC ${RAYLIB_CPP_INCLUDE_DIR})

View File

@ -7,8 +7,8 @@ constexpr int WIDTH = 800;
constexpr int HEIGHT = 800;
constexpr float VERTEX_SIZE = 50.0;
constexpr Color VERTEX_COLOR = {27, 188, 104, 255};
constexpr Color EDGE_COLOR = {20, 133, 38, 255};
constexpr Color VERTEX_COLOR = GREEN;
constexpr Color EDGE_COLOR = DARKGREEN;
constexpr float SIM_SPEED = 4.0;
constexpr float ROTATION_SPEED = 1.0;
@ -18,4 +18,7 @@ constexpr float DEFAULT_SPRING_CONSTANT = 1.5;
constexpr float DEFAULT_DAMPENING_CONSTANT = 0.1;
constexpr float DEFAULT_REST_LENGTH = 0.5;
constexpr int BOARD_PADDING = 5;
constexpr int BLOCK_PADDING = 5;
#endif

237
include/klotski.hpp Normal file
View File

@ -0,0 +1,237 @@
#ifndef __KLOTSKI_HPP_
#define __KLOTSKI_HPP_
#include <array>
#include <cstddef>
#include <format>
#include <iostream>
#include <string>
#include <vector>
// #define DBG_PRINT
enum Direction {
NOR = 1 << 0,
EAS = 1 << 1,
SOU = 1 << 2,
WES = 1 << 3,
};
// A block is represented as a 2-digit string "wh", where w is the block width
// and h the block height.
// The target block (to remove from the board) is represented as a 2-letter
// string "xy", where x is the block width and y the block height and
// width/height are represented by [abcdefghi] (~= [123456789]).
class Block {
public:
int x;
int y;
int width;
int height;
bool target;
public:
Block(int x, int y, int width, int height, bool target)
: x(x), y(y), width(width), height(height), target(target) {
if (x < 0 || x + width >= 10 || y < 0 || y + height >= 10) {
std::cerr << "Block must fit on a 9x9 board!" << std::endl;
exit(1);
}
#ifdef DBG_PRINT
std::cout << ToString() << std::endl;
#endif
}
Block(int x, int y, std::string block) : x(x), y(y) {
if (block == "..") {
this->x = 0;
this->y = 0;
width = 0;
height = 0;
target = false;
return;
}
const std::array<char, 9> chars{'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i'};
target = false;
for (const char c : chars) {
if (block.contains(c)) {
target = true;
break;
}
}
if (target) {
width = static_cast<int>(block.at(0)) - static_cast<int>('a') + 1;
height = static_cast<int>(block.at(1)) - static_cast<int>('a') + 1;
} else {
width = std::stoi(block.substr(0, 1));
height = std::stoi(block.substr(1, 1));
}
if (x < 0 || x + width >= 10 || y < 0 || y + height >= 10) {
std::cerr << "Block must fit on a 9x9 board!" << std::endl;
exit(1);
}
if (block.length() != 2) {
std::cerr << "Block representation must have length [2]!" << std::endl;
exit(1);
}
#ifdef DBG_PRINT
std::cout << "At: (" << x << ", " << y << "), Size: (" << width << ", "
<< height << "), Target: " << target << std::endl;
#endif
}
Block(const Block &copy)
: x(copy.x), y(copy.y), width(copy.width), height(copy.height),
target(copy.target) {}
Block &operator=(const Block &copy) = delete;
Block(Block &&move)
: x(move.x), y(move.y), width(move.width), height(move.height),
target(move.target) {}
Block &operator=(Block &&move) = delete;
bool operator==(const Block &other) {
return x == other.x && y == other.y && width && other.width &&
target == other.target;
}
bool operator!=(const Block &other) { return !(*this == other); }
~Block() {}
public:
auto Hash() -> int;
static auto Invalid() -> Block const;
auto IsValid() -> bool;
auto ToString() -> std::string;
auto Covers(int xx, int yy) -> bool;
auto Collides(const Block &other) -> 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.
class State {
public:
int width;
int height;
std::string state;
// https://en.cppreference.com/w/cpp/iterator/input_iterator.html
class BlockIterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = Block;
private:
const State &state;
int current_pos;
public:
BlockIterator(const State &state) : state(state), current_pos(0) {}
BlockIterator(const State &state, int current_pos)
: state(state), current_pos(current_pos) {}
Block operator*() const {
return Block(current_pos % state.width, current_pos / state.width,
state.state.substr(current_pos * 2 + 4, 2));
}
BlockIterator &operator++() {
do {
current_pos++;
} while (state.state.substr(current_pos * 2 + 4, 2) == "..");
return *this;
}
bool operator==(const BlockIterator &other) {
return state == other.state && current_pos == other.current_pos;
}
bool operator!=(const BlockIterator &other) { return !(*this == other); }
};
public:
State(int width, int height)
: width(width), height(height),
state(std::format("{}x{}:{}", width, height,
std::string(width * height * 2, '.'))) {
if (width <= 0 || width >= 10 || height <= 0 || height >= 10) {
std::cerr << "State width/height must be in [1, 9]!" << std::endl;
exit(1);
}
#ifdef DBG_PRINT
std::cout << "State(" << width << ", " << height << "): \"" << state << "\""
<< std::endl;
#endif
}
State(std::string state)
: width(std::stoi(state.substr(0, 1))),
height(std::stoi(state.substr(2, 1))), state(state) {
if (width <= 0 || width >= 10 || height <= 0 || height >= 10) {
std::cerr << "State width/height must be in [1, 9]!" << std::endl;
exit(1);
}
if (state.length() != width * height * 2 + 4) {
std::cerr
<< "State representation must have length [width * height * 2 + 4]!"
<< std::endl;
exit(1);
}
}
State(const State &copy)
: width(copy.width), height(copy.height), state(copy.state) {}
State &operator=(const State &copy) = delete;
State(State &&move)
: width(move.width), height(move.height), state(std::move(move.state)) {}
State &operator=(State &&move) = delete;
bool operator==(const State &other) const { return state == other.state; }
bool operator!=(const State &other) const { return !(*this == other); }
BlockIterator begin() { return BlockIterator(*this); }
BlockIterator end() { return BlockIterator(*this, width * height); }
~State() {}
public:
auto Hash() -> int;
auto AddBlock(Block block) -> bool;
auto GetBlock(int x, int y) -> Block;
auto RemoveBlock(int x, int y) -> bool;
auto MoveBlockAt(int x, int y, Direction dir) -> bool;
auto GetNextStates() -> std::vector<State>;
};
#endif

View File

@ -6,6 +6,7 @@
#include <raymath.h>
#include <vector>
#include "klotski.hpp"
#include "mass_springs.hpp"
using Edge3Set = std::vector<std::pair<Vector3, Vector3>>;
@ -19,10 +20,12 @@ private:
int width;
int height;
RenderTexture2D render_target;
RenderTexture2D klotski_target;
public:
Renderer(int width, int height) : width(width), height(height) {
render_target = LoadRenderTexture(width, height);
klotski_target = LoadRenderTexture(width, height);
}
Renderer(const Renderer &copy) = delete;
@ -30,7 +33,10 @@ public:
Renderer(Renderer &&move) = delete;
Renderer &operator=(Renderer &&move) = delete;
~Renderer() { UnloadRenderTexture(render_target); }
~Renderer() {
UnloadRenderTexture(render_target);
UnloadRenderTexture(klotski_target);
}
private:
auto Rotate(const Vector3 &a, const float cos_angle, const float sin_angle)
@ -49,6 +55,10 @@ public:
auto DrawMassSprings(const Edge2Set &edges, const Vertex2Set &vertices)
-> void;
auto DrawKlotski(State &state) -> void;
auto DrawTextures() -> void;
};
#endif

130
src/klotski.cpp Normal file
View File

@ -0,0 +1,130 @@
#include "klotski.hpp"
auto Block::Hash() -> int {
std::string s = std::format("{},{},{},{}", x, y, width, height);
return std::hash<std::string>{}(s);
}
auto Block::Invalid() -> Block const {
Block block = Block(0, 0, 1, 1, false);
block.width = 0;
block.height = 0;
return block;
}
auto Block::IsValid() -> bool { return width != 0 && height != 0; }
auto Block::ToString() -> std::string {
if (target) {
return std::format("{}{}",
static_cast<char>(width + static_cast<int>('a') - 1),
static_cast<char>(height + static_cast<int>('a') - 1));
} else {
return std::format("{}{}", width, height);
}
}
auto Block::Covers(int xx, int yy) -> bool {
return xx >= x && xx < x + width && yy >= y && yy < y + height;
}
auto Block::Collides(const Block &other) -> bool {
return x < other.x + other.width && x + width > other.x &&
y < other.y + other.height && y + height > other.y;
}
auto State::Hash() -> int { return std::hash<std::string>{}(state); }
auto State::AddBlock(Block block) -> bool {
if (block.x + block.width > width || block.y + block.height > height) {
return false;
}
for (Block b : *this) {
if (b.Collides(block)) {
return false;
}
}
int index = 4 + (width * block.y + block.x) * 2;
state.replace(index, 2, block.ToString());
return true;
}
auto State::GetBlock(int x, int y) -> Block {
if (x >= width || y >= height) {
return Block::Invalid();
}
for (Block b : *this) {
if (b.Covers(x, y)) {
return b;
}
}
return Block::Invalid();
}
auto State::RemoveBlock(int x, int y) -> bool {
Block b = GetBlock(x, y);
if (!b.IsValid()) {
return false;
}
int index = 4 + (width * b.y + b.x) * 2;
state.replace(index, 2, "..");
return true;
}
auto State::MoveBlockAt(int x, int y, Direction dir) -> bool {
Block block = GetBlock(x, y);
if (!block.IsValid()) {
return false;
}
// Get target block
int target_x = block.x;
int target_y = block.y;
switch (dir) {
case Direction::NOR:
if (target_y < 1) {
return false;
}
target_y--;
break;
case Direction::EAS:
if (target_x + block.width >= width) {
return false;
}
target_x++;
break;
case Direction::SOU:
if (target_y + block.height >= height) {
return false;
}
target_y++;
break;
case Direction::WES:
if (target_x < 1) {
return false;
}
target_x--;
break;
}
Block target =
Block(target_x, target_y, block.width, block.height, block.target);
// Check collisions
for (Block b : *this) {
if (b != block && b.Collides(target)) {
return false;
}
}
RemoveBlock(x, y);
AddBlock(target);
return true;
}

View File

@ -1,5 +1,3 @@
#include "mass_springs.hpp"
#define VERLET_UPDATE
#include <iostream>
@ -7,6 +5,8 @@
#include <raymath.h>
#include "config.hpp"
#include "klotski.hpp"
#include "mass_springs.hpp"
#include "renderer.hpp"
auto main(int argc, char *argv[]) -> int {
@ -21,8 +21,7 @@ auto main(int argc, char *argv[]) -> int {
SetConfigFlags(FLAG_VSYNC_HINT);
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(WIDTH, HEIGHT, "MassSprings");
InitWindow(WIDTH * 2, HEIGHT, "MassSprings");
MassSpringSystem mass_springs;
mass_springs.AddMass(1.0, Vector3(-0.5, 0.5, 0.0), true);
@ -33,6 +32,18 @@ auto main(int argc, char *argv[]) -> int {
mass_springs.AddSpring(1, 2, DEFAULT_SPRING_CONSTANT,
DEFAULT_DAMPENING_CONSTANT, DEFAULT_REST_LENGTH);
State s = State(4, 5);
Block a = Block(0, 0, 2, 1, false);
Block b = Block(0, 1, 1, 3, true);
Block c = Block(0, 2, "45");
Block d = Block(0, 3, "de");
s.AddBlock(a);
s.AddBlock(b);
for (Block block : s) {
std::cout << "Block (" << block.x << ", " << block.y << ")" << std::endl;
}
Renderer renderer(WIDTH, HEIGHT);
Edge2Set edges;
edges.reserve(mass_springs.springs.size());
@ -55,8 +66,9 @@ auto main(int argc, char *argv[]) -> int {
renderer.Transform(edges, vertices, mass_springs, abstime * ROTATION_SPEED,
CAMERA_DISTANCE);
renderer.DrawMassSprings(edges, vertices);
renderer.DrawKlotski(s); // TODO: Don't need to render each frame
renderer.DrawTextures();
renderer.Draw(viewport);
abstime += frametime;
}

View File

@ -1,7 +1,8 @@
#include "renderer.hpp"
#include <raylib.h>
#include "config.hpp"
#include "mass_springs.hpp"
auto Renderer::Rotate(const Vector3 &a, const float cos_angle,
const float sin_angle) -> Vector3 {
@ -63,11 +64,75 @@ auto Renderer::DrawMassSprings(const Edge2Set &edges,
}
DrawLine(0, 0, 0, height, BLACK);
EndTextureMode();
}
auto Renderer::DrawKlotski(State &state) -> void {
BeginTextureMode(klotski_target);
ClearBackground(RAYWHITE);
// Draw Board
const int board_width = width - 2 * BOARD_PADDING;
const int board_height = height - 2 * BOARD_PADDING;
float block_size;
float x_offset = 0.0;
float y_offset = 0.0;
if (state.width > state.height) {
block_size =
static_cast<float>(board_width) / state.width - 2 * BLOCK_PADDING;
y_offset = (board_height - block_size * state.height -
BLOCK_PADDING * 2 * state.height) /
2.0;
} else {
block_size =
static_cast<float>(board_height) / state.height - 2 * BLOCK_PADDING;
x_offset = (board_width - block_size * state.width -
BLOCK_PADDING * 2 * state.width) /
2.0;
}
DrawRectangle(0, 0, width, height, RAYWHITE);
DrawRectangle(x_offset, y_offset,
board_width - 2 * x_offset + 2 * BOARD_PADDING,
board_height - 2 * y_offset + 2 * BOARD_PADDING, LIGHTGRAY);
for (int x = 0; x < state.width; ++x) {
for (int y = 0; y < state.height; ++y) {
DrawRectangle(x_offset + BOARD_PADDING + x * BLOCK_PADDING * 2 +
BLOCK_PADDING + x * block_size,
y_offset + BOARD_PADDING + y * BLOCK_PADDING * 2 +
BLOCK_PADDING + y * block_size,
block_size, block_size, WHITE);
}
}
// Draw Blocks
for (Block block : state) {
Color c = EDGE_COLOR;
if (block.target) {
c = RED;
}
DrawRectangle(x_offset + BOARD_PADDING + block.x * BLOCK_PADDING * 2 +
BLOCK_PADDING + block.x * block_size,
y_offset + BOARD_PADDING + block.y * BLOCK_PADDING * 2 +
BLOCK_PADDING + block.y * block_size,
block.width * block_size + block.width * 2 * BLOCK_PADDING -
2 * BLOCK_PADDING,
block.height * block_size + block.height * 2 * BLOCK_PADDING -
2 * BLOCK_PADDING,
c);
}
DrawLine(width - 1, 0, width - 1, height, BLACK);
EndTextureMode();
}
auto Renderer::DrawTextures() -> void {
BeginDrawing();
DrawTextureRec(render_target.texture,
DrawTextureRec(klotski_target.texture,
Rectangle(0, 0, (float)width, -(float)height), Vector2(0, 0),
WHITE);
DrawFPS(10, 10);
DrawTextureRec(render_target.texture,
Rectangle(0, 0, (float)width, -(float)height),
Vector2(width, 0), WHITE);
DrawFPS(width + 10, 10);
EndDrawing();
}