From dd89c1fd6cc371dd32d6c636f4b94ddd90967388 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 3 May 2023 23:56:29 -0700 Subject: [PATCH] Move solving logic to a separate class --- CMakeLists.txt | 1 + solver.cpp | 13 +++++-------- solver/solver.cpp | 20 ++++++++++++++++++++ solver/solver.h | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 solver/solver.cpp create mode 100644 solver/solver.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f315f6c..22167ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(SOURCE_FILES solver/cell.cpp solver/puzzle.cpp + solver/solver.cpp solver.cpp ) diff --git a/solver.cpp b/solver.cpp index a4d2241..5bc8946 100644 --- a/solver.cpp +++ b/solver.cpp @@ -1,16 +1,13 @@ +#include "solver/solver.h" + #include -#include "solver/puzzle.h" - int main(int argc, char** argv) { - Puzzle puzzle = Puzzle::FromString( + Solver solver( "3..4.162.1...8.4....5.2.83..578........7..5.3..29.4..748.53..1.2.3.9...." ".7...6.9."); - while (puzzle.ApplyNextStep()) - ; - if (!puzzle.IsSolved()) { + if (!solver.Solve()) { std::cout << "Error! Couldn't Solve" << std::endl; } - std::cout << "https://tiramisu.one/sudoku.html?p=" << puzzle.CurrentState() - << "&m=" << puzzle.PencilMarkState() << std::endl; + std::cout << solver.StateUrl() << std::endl; } diff --git a/solver/solver.cpp b/solver/solver.cpp new file mode 100644 index 0000000..ebf74b1 --- /dev/null +++ b/solver/solver.cpp @@ -0,0 +1,20 @@ +#include "solver.h" + +#include + +Solver::Solver(std::string string) : puzzle_(Puzzle::FromString(string)) {} + +bool Solver::Solve() { + while (puzzle_.ApplyNextStep()) { + } + return puzzle_.IsSolved(); +} + +std::string Solver::State() { return puzzle_.CurrentState(); } + +std::string Solver::StateUrl() { + std::ostringstream stream; + stream << "https://tiramisu.one/sudoku.html?p=" << puzzle_.CurrentState() + << "&m=" << puzzle_.PencilMarkState(); + return stream.str(); +} diff --git a/solver/solver.h b/solver/solver.h new file mode 100644 index 0000000..f59bb03 --- /dev/null +++ b/solver/solver.h @@ -0,0 +1,14 @@ +#pragma once + +#include "puzzle.h" + +class Solver { + public: + Solver(std::string puzz_string); + bool Solve(); + std::string State(); + std::string StateUrl(); + + private: + Puzzle puzzle_; +};