back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-11-09 20:00:19 +0300
committerscratko <m@scratko.xyz>2024-11-09 20:12:48 +0300
commite64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf (patch)
tree5d42ff77afaeb101ff260ddf06b733cf0ef6fa61
parent365c27f05d5739213d3a98aa41dcc7f1cb0a6c60 (diff)
downloadpicture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.tar.gz
picture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.tar.bz2
picture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.zip
Moving slides
Changed to 8 puzzles Added reaction to clicking a puzzle Puzzles are moved to vector
-rw-r--r--Makefile23
-rw-r--r--gameplay.cpp30
-rw-r--r--gameplay.hpp8
-rw-r--r--main.cpp2
-rw-r--r--puzzle.cpp29
-rw-r--r--puzzle.hpp32
-rw-r--r--resources/tucan/00.jpgbin2683 -> 4134 bytes
-rw-r--r--resources/tucan/01.jpgbin2150 -> 2860 bytes
-rw-r--r--resources/tucan/02.jpgbin2001 -> 3273 bytes
-rw-r--r--resources/tucan/03.jpgbin2463 -> 0 bytes
-rw-r--r--resources/tucan/10.jpgbin3049 -> 4344 bytes
-rw-r--r--resources/tucan/11.jpgbin2357 -> 3495 bytes
-rw-r--r--resources/tucan/12.jpgbin2388 -> 3051 bytes
-rw-r--r--resources/tucan/13.jpgbin2076 -> 0 bytes
-rw-r--r--resources/tucan/20.jpgbin3028 -> 2283 bytes
-rw-r--r--resources/tucan/21.jpgbin2134 -> 3272 bytes
-rw-r--r--resources/tucan/22.jpgbin2472 -> 1836 bytes
-rw-r--r--resources/tucan/23.jpgbin1693 -> 0 bytes
-rw-r--r--resources/tucan/30.jpgbin1435 -> 0 bytes
-rw-r--r--resources/tucan/31.jpgbin1860 -> 0 bytes
-rw-r--r--resources/tucan/32.jpgbin2276 -> 0 bytes
-rw-r--r--resources/tucan/33.jpgbin1481 -> 0 bytes
22 files changed, 99 insertions, 25 deletions
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<GameParams*>(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 <stdlib.h>
#include <string>
#include <FL/Fl_JPEG_Image.H>
+#include <utility>
+#include <memory>
-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<Puzzle> 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<Puzzle>(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 <FL/Enumerations.H>
#include <FL/Fl_Button.H>
#include <string>
+#include <vector>
+#include <memory>
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<std::unique_ptr<Puzzle>> 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
--- a/resources/tucan/00.jpg
+++ b/resources/tucan/00.jpg
Binary files differ
diff --git a/resources/tucan/01.jpg b/resources/tucan/01.jpg
index a6da468..151e6fd 100644
--- a/resources/tucan/01.jpg
+++ b/resources/tucan/01.jpg
Binary files differ
diff --git a/resources/tucan/02.jpg b/resources/tucan/02.jpg
index 01566e6..96d34d6 100644
--- a/resources/tucan/02.jpg
+++ b/resources/tucan/02.jpg
Binary files differ
diff --git a/resources/tucan/03.jpg b/resources/tucan/03.jpg
deleted file mode 100644
index bb04af4..0000000
--- a/resources/tucan/03.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/10.jpg b/resources/tucan/10.jpg
index caf0e45..b7f5cf6 100644
--- a/resources/tucan/10.jpg
+++ b/resources/tucan/10.jpg
Binary files differ
diff --git a/resources/tucan/11.jpg b/resources/tucan/11.jpg
index 2c5e0be..eb8a2b3 100644
--- a/resources/tucan/11.jpg
+++ b/resources/tucan/11.jpg
Binary files differ
diff --git a/resources/tucan/12.jpg b/resources/tucan/12.jpg
index f7df64c..0b18b7f 100644
--- a/resources/tucan/12.jpg
+++ b/resources/tucan/12.jpg
Binary files differ
diff --git a/resources/tucan/13.jpg b/resources/tucan/13.jpg
deleted file mode 100644
index cea5587..0000000
--- a/resources/tucan/13.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/20.jpg b/resources/tucan/20.jpg
index 3a68e06..98b6996 100644
--- a/resources/tucan/20.jpg
+++ b/resources/tucan/20.jpg
Binary files differ
diff --git a/resources/tucan/21.jpg b/resources/tucan/21.jpg
index 7d6cad7..636b70b 100644
--- a/resources/tucan/21.jpg
+++ b/resources/tucan/21.jpg
Binary files differ
diff --git a/resources/tucan/22.jpg b/resources/tucan/22.jpg
index d3b5a36..b1c9171 100644
--- a/resources/tucan/22.jpg
+++ b/resources/tucan/22.jpg
Binary files differ
diff --git a/resources/tucan/23.jpg b/resources/tucan/23.jpg
deleted file mode 100644
index 3c69d59..0000000
--- a/resources/tucan/23.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/30.jpg b/resources/tucan/30.jpg
deleted file mode 100644
index 870a002..0000000
--- a/resources/tucan/30.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/31.jpg b/resources/tucan/31.jpg
deleted file mode 100644
index 5e4296a..0000000
--- a/resources/tucan/31.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/32.jpg b/resources/tucan/32.jpg
deleted file mode 100644
index f431879..0000000
--- a/resources/tucan/32.jpg
+++ /dev/null
Binary files differ
diff --git a/resources/tucan/33.jpg b/resources/tucan/33.jpg
deleted file mode 100644
index 337aa29..0000000
--- a/resources/tucan/33.jpg
+++ /dev/null
Binary files differ