diff options
author | scratko <m@scratko.xyz> | 2024-11-09 23:56:45 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-11-09 23:56:45 +0300 |
commit | 0ddc597a0f2eeb0dc49f8a6ab4a8af0d615b6ece (patch) | |
tree | e2020d1a908b589aadad78351b05ab54eaea2148 | |
parent | e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf (diff) | |
download | picture-puzzle-0ddc597a0f2eeb0dc49f8a6ab4a8af0d615b6ece.tar.gz picture-puzzle-0ddc597a0f2eeb0dc49f8a6ab4a8af0d615b6ece.tar.bz2 picture-puzzle-0ddc597a0f2eeb0dc49f8a6ab4a8af0d615b6ece.zip |
Checking the solvability of puzzles
-rw-r--r-- | puzzle.cpp | 20 | ||||
-rw-r--r-- | puzzle.hpp | 2 |
2 files changed, 21 insertions, 1 deletions
@@ -35,7 +35,7 @@ static void find_path_to_picture(std::string& path, int number) std::to_string(number % puzzles_per_side) + ".jpg"; } -void GameParams::CreateNewPuzzles() +void GameParams::NextUntestedPuzzles() { int idx_random_puzzle; std::unique_ptr<Puzzle> tmp_puzzle; @@ -65,6 +65,24 @@ void GameParams::CreateNewPuzzles() tmp_puzzle->callback(press_button_callback, this); puzzles.push_back(std::move(tmp_puzzle)); } + ResetFreePuzzles(); +} + +bool GameParams::IsSolvability() +{ + int counter = 0; + for(size_t i = 0; i < puzzles.size(); ++i) + for(size_t j = i+1; j < puzzles.size(); ++j) + if(puzzles[i]->sequence_number > puzzles[j]->sequence_number) + ++counter; + return counter % 2 == 0; +} + +void GameParams::CreateNewPuzzles() +{ + do { + NextUntestedPuzzles(); + } while(!IsSolvability()); } void GameParams::NewGame() @@ -36,6 +36,8 @@ private: GameParams() {} void CalculateStandardPuzzlePos(); void ResetFreePuzzles(); + void NextUntestedPuzzles(); + bool IsSolvability(); void CreateNewPuzzles(); public: void NewGame(); |