From 503e7350fa3d8065e2f4814181664382154dc702 Mon Sep 17 00:00:00 2001 From: scratko Date: Mon, 18 Nov 2024 18:01:32 +0300 Subject: Random image selection Added a dialog box about. Checks if the selected file is correct. --- puzzle.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'puzzle.cpp') diff --git a/puzzle.cpp b/puzzle.cpp index 0d9dc11..3fbd026 100644 --- a/puzzle.cpp +++ b/puzzle.cpp @@ -1,11 +1,15 @@ #include "puzzle.hpp" #include "gameplay.hpp" +#include #include #include #include #include #include +#include +#include + GameParams* GameParams::instance = nullptr; @@ -28,10 +32,11 @@ void GameParams::ResetFreePuzzles() free_puzzles[i] = 1; } -static void find_path_to_picture(std::string& path, int number) +static void find_path_to_picture(std::string& path, + const std::string& cur_directory, int number) { path = - "resources/tucan/" + std::to_string(number / puzzles_per_side) + + cur_directory + std::to_string(number / puzzles_per_side) + std::to_string(number % puzzles_per_side) + ".png"; } @@ -66,7 +71,8 @@ void GameParams::NextUntestedPuzzles() std::unique_ptr(new Puzzle(standard_puzzle_coordinates[i].x, standard_puzzle_coordinates[i].y)); tmp_puzzle->sequence_number = idx_random_puzzle; - find_path_to_picture(tmp_puzzle->path, tmp_puzzle->sequence_number); + find_path_to_picture(tmp_puzzle->path, cur_directory, + tmp_puzzle->sequence_number); Fl_PNG_Image *img = new Fl_PNG_Image(tmp_puzzle->path.c_str()); // TODO check fails img->fail(); tmp_puzzle->image(img); @@ -93,3 +99,20 @@ void GameParams::CreateNewPuzzles() NextUntestedPuzzles(); } while(!IsSolvability()); } + +void GameParams::SelectRandomPicture() +{ + std::vector choices; + for (const auto& entry : std::filesystem::directory_iterator("resources")) { + choices.emplace_back(entry.path().string()); + } + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(choices.begin(), choices.end(), g); + cur_directory = choices[0]; +#if defined(_WIN32) + cur_directory.append("\\"); +#else + cur_directory.append("/"); +#endif +} -- cgit v1.2.3