back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/puzzle.cpp
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-11-18 18:01:32 +0300
committerscratko <m@scratko.xyz>2024-11-18 20:36:37 +0300
commit503e7350fa3d8065e2f4814181664382154dc702 (patch)
tree18ec4fd2dc2d55e286b164c79f673b9e67e4c1bd /puzzle.cpp
parent7fea2267e78de935af6010d5ac7300e51f471601 (diff)
downloadpicture-puzzle-503e7350fa3d8065e2f4814181664382154dc702.tar.gz
picture-puzzle-503e7350fa3d8065e2f4814181664382154dc702.tar.bz2
picture-puzzle-503e7350fa3d8065e2f4814181664382154dc702.zip
Random image selection
Added a dialog box about. Checks if the selected file is correct.
Diffstat (limited to 'puzzle.cpp')
-rw-r--r--puzzle.cpp29
1 files changed, 26 insertions, 3 deletions
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 <algorithm>
#include <stdlib.h>
#include <string>
#include <FL/Fl_PNG_Image.H>
#include <utility>
#include <memory>
+#include <filesystem>
+#include <random>
+
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<Puzzle>(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<std::string> 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
+}