From e16c9e0a2fe29d4a73050c03d261772458de8181 Mon Sep 17 00:00:00 2001 From: Asuro Date: Mon, 17 Mar 2025 18:33:09 +0100 Subject: [PATCH] fixed puzzle solver! --- src/game/Puzzle.cpp | 54 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/game/Puzzle.cpp b/src/game/Puzzle.cpp index 680aa85..ca0c054 100644 --- a/src/game/Puzzle.cpp +++ b/src/game/Puzzle.cpp @@ -180,21 +180,40 @@ namespace Puzzle positionQueue[0] = pos; positionQueueCount++; - while (positionQueueCount > 0) + while (currentPositionQueueIdx < positionQueueCount) { assert(currentPositionQueueIdx < Puzzle::Config::MaxTilesInPuzzle); PuzPos currentPos = positionQueue[currentPositionQueueIdx]; for (PuzPos dir : Dirs) { 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 (nextPos.X == 0 && nextPos.Y == 4) + { + LOG("test"); + } if (IsValidSource(GetNodeAt(puzzle, nextPos), goalType)) { return true; } - positionQueue[positionQueueCount] = nextPos; - positionQueueCount++; + 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; + positionQueueCount++; + } } } currentPositionQueueIdx++; @@ -213,7 +232,7 @@ namespace Puzzle if (goalType == PuzzleElementType::WaterGoal) { return from == PuzzleElementType::WaterIn || from == PuzzleElementType::WaterChannel || - from == PuzzleElementType::WaterGoal; + from == PuzzleElementType::WaterGoal || from == PuzzleElementType::Bridge; } else if (goalType == PuzzleElementType::ElectricGoal) { @@ -366,13 +385,21 @@ namespace Puzzle ImVec2 puzCursorStart = ImGui::GetCursorScreenPos(); auto& drawList = *ImGui::GetWindowDrawList(); + obj.GoalPositionCount = 0; for (int32_t y = 0; y < obj.HeightTiles; ++y) { ImGui::PushID(y); for (int32_t x = 0; x < obj.WidthTiles; ++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}; ImGui::SetCursorScreenPos(pos); ImGui::Text("%s", GetShortNodeName(node)); @@ -463,6 +490,11 @@ namespace Puzzle ImGui::SetCursorScreenPos(pos); ImGui::NewLine(); + ImGui::Separator(); + + PuzzleSolver solver; + ImGui::Text("Solved: %s", solver.IsPuzzleSolved(obj) ? "yes!" : "no."); + bool bAvailOpen = ImGui::TreeNodeEx("Available Cards", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed); if (ImGui::BeginDragDropTarget()) @@ -573,8 +605,10 @@ namespace Generated } PuzPos operator+(PuzPos lhs, const PuzPos& rhs) { - lhs += rhs; - return lhs; + PuzPos res = lhs; + res.X += rhs.X; + res.Y += rhs.Y; + return res; } PuzPos operator-=(PuzPos lhs, const PuzPos& rhs) { @@ -584,7 +618,9 @@ namespace Generated } PuzPos operator-(PuzPos lhs, const PuzPos& rhs) { - lhs -= rhs; - return lhs; + PuzPos res = lhs; + res.X += rhs.X; + res.Y += rhs.Y; + return res; } } // namespace Generated