diff options
author | scratko <m@scratko.xyz> | 2024-10-25 00:40:58 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-10-25 00:50:53 +0300 |
commit | 365c27f05d5739213d3a98aa41dcc7f1cb0a6c60 (patch) | |
tree | 96fd9f7a1a808b6ab96e310037b9db376921d2f1 | |
download | 15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.tar.gz 15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.tar.bz2 15-puzzle-365c27f05d5739213d3a98aa41dcc7f1cb0a6c60.zip |
Creating widgets with a split image
-rw-r--r-- | main.cpp | 19 | ||||
-rw-r--r-- | puzzle.cpp | 68 | ||||
-rw-r--r-- | puzzle.hpp | 45 | ||||
-rw-r--r-- | resources/tucan/00.jpg | bin | 0 -> 2683 bytes | |||
-rw-r--r-- | resources/tucan/01.jpg | bin | 0 -> 2150 bytes | |||
-rw-r--r-- | resources/tucan/02.jpg | bin | 0 -> 2001 bytes | |||
-rw-r--r-- | resources/tucan/03.jpg | bin | 0 -> 2463 bytes | |||
-rw-r--r-- | resources/tucan/10.jpg | bin | 0 -> 3049 bytes | |||
-rw-r--r-- | resources/tucan/11.jpg | bin | 0 -> 2357 bytes | |||
-rw-r--r-- | resources/tucan/12.jpg | bin | 0 -> 2388 bytes | |||
-rw-r--r-- | resources/tucan/13.jpg | bin | 0 -> 2076 bytes | |||
-rw-r--r-- | resources/tucan/20.jpg | bin | 0 -> 3028 bytes | |||
-rw-r--r-- | resources/tucan/21.jpg | bin | 0 -> 2134 bytes | |||
-rw-r--r-- | resources/tucan/22.jpg | bin | 0 -> 2472 bytes | |||
-rw-r--r-- | resources/tucan/23.jpg | bin | 0 -> 1693 bytes | |||
-rw-r--r-- | resources/tucan/30.jpg | bin | 0 -> 1435 bytes | |||
-rw-r--r-- | resources/tucan/31.jpg | bin | 0 -> 1860 bytes | |||
-rw-r--r-- | resources/tucan/32.jpg | bin | 0 -> 2276 bytes | |||
-rw-r--r-- | resources/tucan/33.jpg | bin | 0 -> 1481 bytes |
19 files changed, 132 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..caf1a17 --- /dev/null +++ b/main.cpp @@ -0,0 +1,19 @@ +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_Box.H> +#include <stdlib.h> +#include <time.h> + +#include "puzzle.hpp" + +int main() +{ + srand(time(nullptr)); + Fl_Window *win = new Fl_Window(325, 325, "15 puzzle"); + GameParams *params = GameParams::StartParams(); + params->NewGame(); + win->end(); + win->show(); + return Fl::run(); + return 0; +} 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(); +} diff --git a/puzzle.hpp b/puzzle.hpp new file mode 100644 index 0000000..6ceb6e1 --- /dev/null +++ b/puzzle.hpp @@ -0,0 +1,45 @@ +#ifndef PUZZLE_HPP_SENTRY +#define PUZZLE_HPP_SENTRY + +#include <FL/Enumerations.H> +#include <FL/Fl_Button.H> +#include <string> + +enum { + puzzle_pieces = 15, + puzzles_per_side = 4, + puzzle_size = 75, + spacing = 5 +}; + +class Puzzle : Fl_Button{ + unsigned int sequence_number; + std::string path; + Puzzle(int x, int y) + : Fl_Button(x, y, puzzle_size, puzzle_size), + sequence_number(0) {} + friend class GameParams; +}; + +class GameParams { + struct coordinates { + int x, y; + }; + coordinates puzzle_coordinates[puzzle_pieces]; + char free_puzzles[puzzle_pieces]; + Puzzle *puzzles[puzzle_pieces]; + GameParams() : puzzles{nullptr} {} + void ResetFreePuzzles(); + void CalculatePuzzlePos(); + void CreateNewPuzzles(); +public: + void NewGame(); + + static GameParams *StartParams() { + GameParams *gi = new GameParams; + gi->CalculatePuzzlePos(); + return gi; + } +}; + +#endif diff --git a/resources/tucan/00.jpg b/resources/tucan/00.jpg Binary files differnew file mode 100644 index 0000000..a60e2c6 --- /dev/null +++ b/resources/tucan/00.jpg diff --git a/resources/tucan/01.jpg b/resources/tucan/01.jpg Binary files differnew file mode 100644 index 0000000..a6da468 --- /dev/null +++ b/resources/tucan/01.jpg diff --git a/resources/tucan/02.jpg b/resources/tucan/02.jpg Binary files differnew file mode 100644 index 0000000..01566e6 --- /dev/null +++ b/resources/tucan/02.jpg diff --git a/resources/tucan/03.jpg b/resources/tucan/03.jpg Binary files differnew file mode 100644 index 0000000..bb04af4 --- /dev/null +++ b/resources/tucan/03.jpg diff --git a/resources/tucan/10.jpg b/resources/tucan/10.jpg Binary files differnew file mode 100644 index 0000000..caf0e45 --- /dev/null +++ b/resources/tucan/10.jpg diff --git a/resources/tucan/11.jpg b/resources/tucan/11.jpg Binary files differnew file mode 100644 index 0000000..2c5e0be --- /dev/null +++ b/resources/tucan/11.jpg diff --git a/resources/tucan/12.jpg b/resources/tucan/12.jpg Binary files differnew file mode 100644 index 0000000..f7df64c --- /dev/null +++ b/resources/tucan/12.jpg diff --git a/resources/tucan/13.jpg b/resources/tucan/13.jpg Binary files differnew file mode 100644 index 0000000..cea5587 --- /dev/null +++ b/resources/tucan/13.jpg diff --git a/resources/tucan/20.jpg b/resources/tucan/20.jpg Binary files differnew file mode 100644 index 0000000..3a68e06 --- /dev/null +++ b/resources/tucan/20.jpg diff --git a/resources/tucan/21.jpg b/resources/tucan/21.jpg Binary files differnew file mode 100644 index 0000000..7d6cad7 --- /dev/null +++ b/resources/tucan/21.jpg diff --git a/resources/tucan/22.jpg b/resources/tucan/22.jpg Binary files differnew file mode 100644 index 0000000..d3b5a36 --- /dev/null +++ b/resources/tucan/22.jpg diff --git a/resources/tucan/23.jpg b/resources/tucan/23.jpg Binary files differnew file mode 100644 index 0000000..3c69d59 --- /dev/null +++ b/resources/tucan/23.jpg diff --git a/resources/tucan/30.jpg b/resources/tucan/30.jpg Binary files differnew file mode 100644 index 0000000..870a002 --- /dev/null +++ b/resources/tucan/30.jpg diff --git a/resources/tucan/31.jpg b/resources/tucan/31.jpg Binary files differnew file mode 100644 index 0000000..5e4296a --- /dev/null +++ b/resources/tucan/31.jpg diff --git a/resources/tucan/32.jpg b/resources/tucan/32.jpg Binary files differnew file mode 100644 index 0000000..f431879 --- /dev/null +++ b/resources/tucan/32.jpg diff --git a/resources/tucan/33.jpg b/resources/tucan/33.jpg Binary files differnew file mode 100644 index 0000000..337aa29 --- /dev/null +++ b/resources/tucan/33.jpg |