fixed puzzle solver!

This commit is contained in:
Asuro
2025-03-17 18:33:09 +01:00
parent ffd2e45d81
commit e16c9e0a2f

View File

@@ -180,23 +180,42 @@ namespace Puzzle
positionQueue[0] = pos; positionQueue[0] = pos;
positionQueueCount++; positionQueueCount++;
while (positionQueueCount > 0) while (currentPositionQueueIdx < positionQueueCount)
{ {
assert(currentPositionQueueIdx < Puzzle::Config::MaxTilesInPuzzle); assert(currentPositionQueueIdx < Puzzle::Config::MaxTilesInPuzzle);
PuzPos currentPos = positionQueue[currentPositionQueueIdx]; PuzPos currentPos = positionQueue[currentPositionQueueIdx];
for (PuzPos dir : Dirs) for (PuzPos dir : Dirs)
{ {
PuzPos nextPos = currentPos + dir; PuzPos nextPos = currentPos + dir;
if (!(nextPos.X >= 0 && nextPos.Y >= 0 && nextPos.X < puzzle.WidthTiles &&
nextPos.Y < puzzle.HeightTiles))
continue;
if (IsValidGoalConnection(puzzle, nextPos, currentPos, goalType)) if (IsValidGoalConnection(puzzle, nextPos, currentPos, goalType))
{ {
if (nextPos.X == 0 && nextPos.Y == 4)
{
LOG("test");
}
if (IsValidSource(GetNodeAt(puzzle, nextPos), goalType)) if (IsValidSource(GetNodeAt(puzzle, nextPos), goalType))
{ {
return true; return true;
} }
bool found = false;
for (int32_t i = 0; i < positionQueueCount; ++i)
{
if (positionQueue[i].X == nextPos.X && positionQueue[i].Y == nextPos.Y)
{
found = true;
break;
}
}
if (!found)
{
positionQueue[positionQueueCount] = nextPos; positionQueue[positionQueueCount] = nextPos;
positionQueueCount++; positionQueueCount++;
} }
} }
}
currentPositionQueueIdx++; currentPositionQueueIdx++;
} }
return false; return false;
@@ -213,7 +232,7 @@ namespace Puzzle
if (goalType == PuzzleElementType::WaterGoal) if (goalType == PuzzleElementType::WaterGoal)
{ {
return from == PuzzleElementType::WaterIn || from == PuzzleElementType::WaterChannel || return from == PuzzleElementType::WaterIn || from == PuzzleElementType::WaterChannel ||
from == PuzzleElementType::WaterGoal; from == PuzzleElementType::WaterGoal || from == PuzzleElementType::Bridge;
} }
else if (goalType == PuzzleElementType::ElectricGoal) else if (goalType == PuzzleElementType::ElectricGoal)
{ {
@@ -366,13 +385,21 @@ namespace Puzzle
ImVec2 puzCursorStart = ImGui::GetCursorScreenPos(); ImVec2 puzCursorStart = ImGui::GetCursorScreenPos();
auto& drawList = *ImGui::GetWindowDrawList(); auto& drawList = *ImGui::GetWindowDrawList();
obj.GoalPositionCount = 0;
for (int32_t y = 0; y < obj.HeightTiles; ++y) for (int32_t y = 0; y < obj.HeightTiles; ++y)
{ {
ImGui::PushID(y); ImGui::PushID(y);
for (int32_t x = 0; x < obj.WidthTiles; ++x) for (int32_t x = 0; x < obj.WidthTiles; ++x)
{ {
ImGui::PushID(x); ImGui::PushID(x);
auto node = GetNodeAt(obj, {int8_t(x), int8_t(y)}); PuzPos nodePos = {int8_t(x), int8_t(y)};
auto node = GetNodeAt(obj, nodePos);
if (node == PuzzleElementType::WaterGoal)
{
obj.GoalPositions[obj.GoalPositionCount] = nodePos;
obj.GoalPositionCount++;
}
ImVec2 pos = ImVec2{puzCursorStart.x + x * UIPuzBoxSize + 5, puzCursorStart.y + y * UIPuzBoxSize}; ImVec2 pos = ImVec2{puzCursorStart.x + x * UIPuzBoxSize + 5, puzCursorStart.y + y * UIPuzBoxSize};
ImGui::SetCursorScreenPos(pos); ImGui::SetCursorScreenPos(pos);
ImGui::Text("%s", GetShortNodeName(node)); ImGui::Text("%s", GetShortNodeName(node));
@@ -463,6 +490,11 @@ namespace Puzzle
ImGui::SetCursorScreenPos(pos); ImGui::SetCursorScreenPos(pos);
ImGui::NewLine(); ImGui::NewLine();
ImGui::Separator();
PuzzleSolver solver;
ImGui::Text("Solved: %s", solver.IsPuzzleSolved(obj) ? "yes!" : "no.");
bool bAvailOpen = bool bAvailOpen =
ImGui::TreeNodeEx("Available Cards", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed); ImGui::TreeNodeEx("Available Cards", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed);
if (ImGui::BeginDragDropTarget()) if (ImGui::BeginDragDropTarget())
@@ -573,8 +605,10 @@ namespace Generated
} }
PuzPos operator+(PuzPos lhs, const PuzPos& rhs) PuzPos operator+(PuzPos lhs, const PuzPos& rhs)
{ {
lhs += rhs; PuzPos res = lhs;
return lhs; res.X += rhs.X;
res.Y += rhs.Y;
return res;
} }
PuzPos operator-=(PuzPos lhs, const PuzPos& rhs) PuzPos operator-=(PuzPos lhs, const PuzPos& rhs)
{ {
@@ -584,7 +618,9 @@ namespace Generated
} }
PuzPos operator-(PuzPos lhs, const PuzPos& rhs) PuzPos operator-(PuzPos lhs, const PuzPos& rhs)
{ {
lhs -= rhs; PuzPos res = lhs;
return lhs; res.X += rhs.X;
res.Y += rhs.Y;
return res;
} }
} // namespace Generated } // namespace Generated