Skip to content

Commit

Permalink
Fix state tracer
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Jan 25, 2024
1 parent a65f5ba commit 4e50255
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ void Pipe::rotate(bool clockwise)
}
}

std::vector<Side> Pipe::connections() const
{
std::vector<Side> sides;
sides.reserve(Side::max);

for (const Side s : { Side::Top, Side::Right, Side::Bottom, Side::Left }) {
if (get(s)) {
sides.push_back(s);
}
}

return sides;
}

bool Pipe::get(const Side& side) const
{
return sides[side];
Expand Down
7 changes: 7 additions & 0 deletions src/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <bitset>
#include <vector>

/** Position (coordinates). */
struct Position {
Expand Down Expand Up @@ -58,6 +59,12 @@ class Pipe {
*/
void rotate(bool clockwise);

/**
* Gat connection sides.
* @return array with connected sides
*/
std::vector<Side> connections() const;

/**
* Check if side connected.
* @param side to check
Expand Down
36 changes: 24 additions & 12 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,33 @@ void Level::apply_path(const Position& start, const Level::Path& path)
}
}

void Level::trace_state(const Position& pos)
void Level::trace_state(const Position& start)
{
Cell& cell = get_cell(pos);
const Side sides[] = { Side::Top, Side::Right, Side::Bottom, Side::Left };

for (const Side side : sides) {
if (cell.pipe.get(side)) {
const Position next_pos = neighbor(pos, side);
Cell& next_cell = get_cell(next_pos);
if (!next_cell.rotation() && !next_cell.active &&
next_cell.pipe.get(side.opposite())) {
next_cell.active = true;
trace_state(next_pos);
Position curr_pos = start;

while (true) {
Cell& curr_cell = get_cell(curr_pos);
curr_cell.active = true;

// get possible ways
std::vector<Position> connected;
for (const Side side : curr_cell.pipe.connections()) {
const Position next_pos = neighbor(curr_pos, side);
const Cell& next_cell = get_cell(next_pos);
if (next_pos != curr_pos && !next_cell.rotation() &&
!next_cell.active && next_cell.pipe.get(side.opposite())) {
connected.push_back(next_pos);
}
}
if (connected.empty()) {
break;
}

curr_pos = connected[0];

if (connected.size() > 1) { // fork
trace_state(connected[1]);
}
}
}

Expand Down

0 comments on commit 4e50255

Please sign in to comment.