From e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf Mon Sep 17 00:00:00 2001 From: scratko Date: Sat, 9 Nov 2024 20:00:19 +0300 Subject: Moving slides Changed to 8 puzzles Added reaction to clicking a puzzle Puzzles are moved to vector --- Makefile | 23 +++++++++++++++++++++++ gameplay.cpp | 30 ++++++++++++++++++++++++++++++ gameplay.hpp | 8 ++++++++ main.cpp | 2 +- puzzle.cpp | 29 ++++++++++++++++++----------- puzzle.hpp | 32 +++++++++++++++++++------------- resources/tucan/00.jpg | Bin 2683 -> 4134 bytes resources/tucan/01.jpg | Bin 2150 -> 2860 bytes resources/tucan/02.jpg | Bin 2001 -> 3273 bytes resources/tucan/03.jpg | Bin 2463 -> 0 bytes resources/tucan/10.jpg | Bin 3049 -> 4344 bytes resources/tucan/11.jpg | Bin 2357 -> 3495 bytes resources/tucan/12.jpg | Bin 2388 -> 3051 bytes resources/tucan/13.jpg | Bin 2076 -> 0 bytes resources/tucan/20.jpg | Bin 3028 -> 2283 bytes resources/tucan/21.jpg | Bin 2134 -> 3272 bytes resources/tucan/22.jpg | Bin 2472 -> 1836 bytes resources/tucan/23.jpg | Bin 1693 -> 0 bytes resources/tucan/30.jpg | Bin 1435 -> 0 bytes resources/tucan/31.jpg | Bin 1860 -> 0 bytes resources/tucan/32.jpg | Bin 2276 -> 0 bytes resources/tucan/33.jpg | Bin 1481 -> 0 bytes 22 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 Makefile create mode 100644 gameplay.cpp create mode 100644 gameplay.hpp delete mode 100644 resources/tucan/03.jpg delete mode 100644 resources/tucan/13.jpg delete mode 100644 resources/tucan/23.jpg delete mode 100644 resources/tucan/30.jpg delete mode 100644 resources/tucan/31.jpg delete mode 100644 resources/tucan/32.jpg delete mode 100644 resources/tucan/33.jpg diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a73ba82 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +SRCMODULES = puzzle.cpp gameplay.cpp main.cpp +OBJMODULES = $(SRCMODULES:.cpp=.o) +CXX = g++ +CXXFLAGS = -Wall -g +LIBS = -lfltk -lfltk_images + +all: main + +%.o: %.сpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +main: $(OBJMODULES) + $(CXX) $^ $(LIBS) -o $@ + +ifneq (clean, $(MAKECMDGOALS)) +-include deps.mk +endif + +deps.mk: $(SRCMODULES) + $(CC) -MM $^ > $@ + +clean: + rm -f *.o main diff --git a/gameplay.cpp b/gameplay.cpp new file mode 100644 index 0000000..d80a4c9 --- /dev/null +++ b/gameplay.cpp @@ -0,0 +1,30 @@ +#include "gameplay.hpp" +#include "puzzle.hpp" +#include "FL/Fl_Group.H" + +static bool is_next_to_empty_box(GameParams::coordinates empty_box_pos, + GameParams::coordinates current_pos) +{ + return + (current_pos.x - spacing - puzzle_size == empty_box_pos.x && + current_pos.y == empty_box_pos.y) || + (current_pos.x == empty_box_pos.x && + current_pos.y - spacing - puzzle_size == empty_box_pos.y) || + (current_pos.x + puzzle_size + spacing == empty_box_pos.x && + current_pos.y == empty_box_pos.y) || + (current_pos.x == empty_box_pos.x && + current_pos.y + puzzle_size + spacing == empty_box_pos.y); +} + +void press_button_callback(Fl_Widget *w, void *params) +{ + GameParams *gp = reinterpret_cast(params); + GameParams::coordinates current_pos = { w->x(), w->y() }; + if(is_next_to_empty_box(gp->GetXYEmptyBox(), current_pos)) { + w->position(gp->GetXYEmptyBox().x, gp->GetXYEmptyBox().y); + gp->SetXYEmptyBox(current_pos.x, current_pos.y); + } + w->redraw(); + w->parent()->redraw(); +} + diff --git a/gameplay.hpp b/gameplay.hpp new file mode 100644 index 0000000..22e0ab7 --- /dev/null +++ b/gameplay.hpp @@ -0,0 +1,8 @@ +#ifndef GAMEPLAY_HPP_SENTRY +#define GAMEPLAY_HPP_SENTRY + +#include "puzzle.hpp" + +void press_button_callback(Fl_Widget *w, void *params); + +#endif diff --git a/main.cpp b/main.cpp index caf1a17..0cbe9ea 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,7 @@ int main() { srand(time(nullptr)); Fl_Window *win = new Fl_Window(325, 325, "15 puzzle"); - GameParams *params = GameParams::StartParams(); + GameParams *params = GameParams::SetUpParams(); params->NewGame(); win->end(); win->show(); diff --git a/puzzle.cpp b/puzzle.cpp index 6d7d635..0e3574b 100644 --- a/puzzle.cpp +++ b/puzzle.cpp @@ -1,10 +1,13 @@ #include "puzzle.hpp" +#include "gameplay.hpp" #include #include #include +#include +#include -void GameParams::CalculatePuzzlePos() +void GameParams::CalculateStandardPuzzlePos() { coordinates tmp; @@ -15,7 +18,7 @@ void GameParams::CalculatePuzzlePos() break; tmp.x = i * (puzzle_size + spacing) + spacing; tmp.y = j * (puzzle_size + spacing) + spacing; - puzzle_coordinates[k] = tmp; + standard_puzzle_coordinates[k] = tmp; } } @@ -35,29 +38,32 @@ static void find_path_to_picture(std::string& path, int number) void GameParams::CreateNewPuzzles() { int idx_random_puzzle; + std::unique_ptr tmp_puzzle; /* * check if puzzles already were created */ - for(int i = 0; i < puzzle_pieces; ++i) - if(puzzles[i]) - delete puzzles[i]; + if(puzzles.size() != 0) + puzzles.clear(); /* * ======== creating puzzles =========== */ for(int i = 0; i < puzzle_pieces; ++i) { - puzzles[i] = - new Puzzle(puzzle_coordinates[i].x, puzzle_coordinates[i].y); + tmp_puzzle = + std::unique_ptr(new Puzzle(standard_puzzle_coordinates[i].x, + standard_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()); + tmp_puzzle->sequence_number = idx_random_puzzle; + find_path_to_picture(tmp_puzzle->path, tmp_puzzle->sequence_number); + Fl_JPEG_Image *img = new Fl_JPEG_Image(tmp_puzzle->path.c_str()); // TODO check fails img->fail(); - puzzles[i]->image(img); + tmp_puzzle->image(img); + tmp_puzzle->callback(press_button_callback, this); + puzzles.push_back(std::move(tmp_puzzle)); } } @@ -65,4 +71,5 @@ void GameParams::NewGame() { ResetFreePuzzles(); CreateNewPuzzles(); + SetXYEmptyBox(215, 215); } diff --git a/puzzle.hpp b/puzzle.hpp index 6ceb6e1..5b2b2ab 100644 --- a/puzzle.hpp +++ b/puzzle.hpp @@ -4,40 +4,46 @@ #include #include #include +#include +#include enum { - puzzle_pieces = 15, - puzzles_per_side = 4, - puzzle_size = 75, + puzzle_pieces = 8, + puzzles_per_side = 3, + puzzle_size = 100, spacing = 5 }; class Puzzle : Fl_Button{ - unsigned int sequence_number; + unsigned char sequence_number; std::string path; Puzzle(int x, int y) - : Fl_Button(x, y, puzzle_size, puzzle_size), - sequence_number(0) {} + : Fl_Button(x, y, puzzle_size, puzzle_size) {} friend class GameParams; }; class GameParams { +public: struct coordinates { int x, y; }; - coordinates puzzle_coordinates[puzzle_pieces]; +private: + coordinates standard_puzzle_coordinates[puzzle_pieces]; char free_puzzles[puzzle_pieces]; - Puzzle *puzzles[puzzle_pieces]; - GameParams() : puzzles{nullptr} {} + coordinates empty_box; + std::vector> puzzles; + + GameParams() {} + void CalculateStandardPuzzlePos(); void ResetFreePuzzles(); - void CalculatePuzzlePos(); void CreateNewPuzzles(); public: void NewGame(); - - static GameParams *StartParams() { + void SetXYEmptyBox(int x, int y) { empty_box.x = x; empty_box. y = y; } + coordinates GetXYEmptyBox() { return empty_box; } + static GameParams *SetUpParams() { GameParams *gi = new GameParams; - gi->CalculatePuzzlePos(); + gi->CalculateStandardPuzzlePos(); return gi; } }; diff --git a/resources/tucan/00.jpg b/resources/tucan/00.jpg index a60e2c6..ad5dbc5 100644 Binary files a/resources/tucan/00.jpg and b/resources/tucan/00.jpg differ diff --git a/resources/tucan/01.jpg b/resources/tucan/01.jpg index a6da468..151e6fd 100644 Binary files a/resources/tucan/01.jpg and b/resources/tucan/01.jpg differ diff --git a/resources/tucan/02.jpg b/resources/tucan/02.jpg index 01566e6..96d34d6 100644 Binary files a/resources/tucan/02.jpg and b/resources/tucan/02.jpg differ diff --git a/resources/tucan/03.jpg b/resources/tucan/03.jpg deleted file mode 100644 index bb04af4..0000000 Binary files a/resources/tucan/03.jpg and /dev/null differ diff --git a/resources/tucan/10.jpg b/resources/tucan/10.jpg index caf0e45..b7f5cf6 100644 Binary files a/resources/tucan/10.jpg and b/resources/tucan/10.jpg differ diff --git a/resources/tucan/11.jpg b/resources/tucan/11.jpg index 2c5e0be..eb8a2b3 100644 Binary files a/resources/tucan/11.jpg and b/resources/tucan/11.jpg differ diff --git a/resources/tucan/12.jpg b/resources/tucan/12.jpg index f7df64c..0b18b7f 100644 Binary files a/resources/tucan/12.jpg and b/resources/tucan/12.jpg differ diff --git a/resources/tucan/13.jpg b/resources/tucan/13.jpg deleted file mode 100644 index cea5587..0000000 Binary files a/resources/tucan/13.jpg and /dev/null differ diff --git a/resources/tucan/20.jpg b/resources/tucan/20.jpg index 3a68e06..98b6996 100644 Binary files a/resources/tucan/20.jpg and b/resources/tucan/20.jpg differ diff --git a/resources/tucan/21.jpg b/resources/tucan/21.jpg index 7d6cad7..636b70b 100644 Binary files a/resources/tucan/21.jpg and b/resources/tucan/21.jpg differ diff --git a/resources/tucan/22.jpg b/resources/tucan/22.jpg index d3b5a36..b1c9171 100644 Binary files a/resources/tucan/22.jpg and b/resources/tucan/22.jpg differ diff --git a/resources/tucan/23.jpg b/resources/tucan/23.jpg deleted file mode 100644 index 3c69d59..0000000 Binary files a/resources/tucan/23.jpg and /dev/null differ diff --git a/resources/tucan/30.jpg b/resources/tucan/30.jpg deleted file mode 100644 index 870a002..0000000 Binary files a/resources/tucan/30.jpg and /dev/null differ diff --git a/resources/tucan/31.jpg b/resources/tucan/31.jpg deleted file mode 100644 index 5e4296a..0000000 Binary files a/resources/tucan/31.jpg and /dev/null differ diff --git a/resources/tucan/32.jpg b/resources/tucan/32.jpg deleted file mode 100644 index f431879..0000000 Binary files a/resources/tucan/32.jpg and /dev/null differ diff --git a/resources/tucan/33.jpg b/resources/tucan/33.jpg deleted file mode 100644 index 337aa29..0000000 Binary files a/resources/tucan/33.jpg and /dev/null differ -- cgit v1.2.3