back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/puzzle.cpp
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-10-25 00:40:58 +0300
committerscratko <m@scratko.xyz>2024-10-25 00:50:53 +0300
commit365c27f05d5739213d3a98aa41dcc7f1cb0a6c60 (patch)
tree96fd9f7a1a808b6ab96e310037b9db376921d2f1 /puzzle.cpp
download15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.tar.gz
15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.tar.bz2
15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.zip
Initial commitHEADmaster
Creating widgets with a split image
Diffstat (limited to 'puzzle.cpp')
-rw-r--r--puzzle.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/puzzle.cpp b/puzzle.cpp
new file mode 100644
index 0000000..6d7d635
--- /dev/null
+++ b/puzzle.cpp
@@ -0,0 +1,68 @@
+#include "puzzle.hpp"
+
+#include <stdlib.h>
+#include <string>
+#include <FL/Fl_JPEG_Image.H>
+
+void GameParams::CalculatePuzzlePos()
+{
+ coordinates tmp;
+
+ int i, j, k = 0;
+ for(i = 0; i < puzzles_per_side; ++i)
+ for(j = 0; j < puzzles_per_side; ++j, ++k) {
+ if(i == puzzles_per_side-1 && j == puzzles_per_side-1)
+ break;
+ tmp.x = i * (puzzle_size + spacing) + spacing;
+ tmp.y = j * (puzzle_size + spacing) + spacing;
+ puzzle_coordinates[k] = tmp;
+ }
+}
+
+void GameParams::ResetFreePuzzles()
+{
+ for(int i = 0; i < puzzle_pieces; ++i)
+ free_puzzles[i] = 1;
+}
+
+static void find_path_to_picture(std::string& path, int number)
+{
+ path =
+ "resources/tucan/" + std::to_string(number / puzzles_per_side) +
+ std::to_string(number % puzzles_per_side) + ".jpg";
+}
+
+void GameParams::CreateNewPuzzles()
+{
+ int idx_random_puzzle;
+ /*
+ * check if puzzles already were created
+ */
+ for(int i = 0; i < puzzle_pieces; ++i)
+ if(puzzles[i])
+ delete puzzles[i];
+ /*
+ * ======== creating puzzles ===========
+ */
+ for(int i = 0; i < puzzle_pieces; ++i) {
+ puzzles[i] =
+ new Puzzle(puzzle_coordinates[i].x, puzzle_coordinates[i].y);
+ idx_random_puzzle =
+ 0 + (int)((double)puzzle_pieces * rand()/(RAND_MAX + 1.0));
+ while(!free_puzzles[idx_random_puzzle])
+ idx_random_puzzle =
+ 0 + (int)((double)puzzle_pieces * rand()/(RAND_MAX + 1.0));
+ free_puzzles[idx_random_puzzle] = 0;
+ puzzles[i]->sequence_number = idx_random_puzzle;
+ find_path_to_picture(puzzles[i]->path, puzzles[i]->sequence_number);
+ Fl_JPEG_Image *img = new Fl_JPEG_Image(puzzles[i]->path.c_str());
+ // TODO check fails img->fail();
+ puzzles[i]->image(img);
+ }
+}
+
+void GameParams::NewGame()
+{
+ ResetFreePuzzles();
+ CreateNewPuzzles();
+}