diff options
| -rw-r--r-- | Makefile | 23 | ||||
| -rw-r--r-- | gameplay.cpp | 30 | ||||
| -rw-r--r-- | gameplay.hpp | 8 | ||||
| -rw-r--r-- | main.cpp | 2 | ||||
| -rw-r--r-- | puzzle.cpp | 29 | ||||
| -rw-r--r-- | puzzle.hpp | 32 | ||||
| -rw-r--r-- | resources/tucan/00.jpg | bin | 2683 -> 4134 bytes | |||
| -rw-r--r-- | resources/tucan/01.jpg | bin | 2150 -> 2860 bytes | |||
| -rw-r--r-- | resources/tucan/02.jpg | bin | 2001 -> 3273 bytes | |||
| -rw-r--r-- | resources/tucan/03.jpg | bin | 2463 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/10.jpg | bin | 3049 -> 4344 bytes | |||
| -rw-r--r-- | resources/tucan/11.jpg | bin | 2357 -> 3495 bytes | |||
| -rw-r--r-- | resources/tucan/12.jpg | bin | 2388 -> 3051 bytes | |||
| -rw-r--r-- | resources/tucan/13.jpg | bin | 2076 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/20.jpg | bin | 3028 -> 2283 bytes | |||
| -rw-r--r-- | resources/tucan/21.jpg | bin | 2134 -> 3272 bytes | |||
| -rw-r--r-- | resources/tucan/22.jpg | bin | 2472 -> 1836 bytes | |||
| -rw-r--r-- | resources/tucan/23.jpg | bin | 1693 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/30.jpg | bin | 1435 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/31.jpg | bin | 1860 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/32.jpg | bin | 2276 -> 0 bytes | |||
| -rw-r--r-- | resources/tucan/33.jpg | bin | 1481 -> 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 @@ -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(); @@ -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);  } @@ -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 Binary files differindex a60e2c6..ad5dbc5 100644 --- a/resources/tucan/00.jpg +++ b/resources/tucan/00.jpg diff --git a/resources/tucan/01.jpg b/resources/tucan/01.jpg Binary files differindex a6da468..151e6fd 100644 --- a/resources/tucan/01.jpg +++ b/resources/tucan/01.jpg diff --git a/resources/tucan/02.jpg b/resources/tucan/02.jpg Binary files differindex 01566e6..96d34d6 100644 --- a/resources/tucan/02.jpg +++ b/resources/tucan/02.jpg diff --git a/resources/tucan/03.jpg b/resources/tucan/03.jpg Binary files differdeleted file mode 100644 index bb04af4..0000000 --- a/resources/tucan/03.jpg +++ /dev/null diff --git a/resources/tucan/10.jpg b/resources/tucan/10.jpg Binary files differindex caf0e45..b7f5cf6 100644 --- a/resources/tucan/10.jpg +++ b/resources/tucan/10.jpg diff --git a/resources/tucan/11.jpg b/resources/tucan/11.jpg Binary files differindex 2c5e0be..eb8a2b3 100644 --- a/resources/tucan/11.jpg +++ b/resources/tucan/11.jpg diff --git a/resources/tucan/12.jpg b/resources/tucan/12.jpg Binary files differindex f7df64c..0b18b7f 100644 --- a/resources/tucan/12.jpg +++ b/resources/tucan/12.jpg diff --git a/resources/tucan/13.jpg b/resources/tucan/13.jpg Binary files differdeleted file mode 100644 index cea5587..0000000 --- a/resources/tucan/13.jpg +++ /dev/null diff --git a/resources/tucan/20.jpg b/resources/tucan/20.jpg Binary files differindex 3a68e06..98b6996 100644 --- a/resources/tucan/20.jpg +++ b/resources/tucan/20.jpg diff --git a/resources/tucan/21.jpg b/resources/tucan/21.jpg Binary files differindex 7d6cad7..636b70b 100644 --- a/resources/tucan/21.jpg +++ b/resources/tucan/21.jpg diff --git a/resources/tucan/22.jpg b/resources/tucan/22.jpg Binary files differindex d3b5a36..b1c9171 100644 --- a/resources/tucan/22.jpg +++ b/resources/tucan/22.jpg diff --git a/resources/tucan/23.jpg b/resources/tucan/23.jpg Binary files differdeleted file mode 100644 index 3c69d59..0000000 --- a/resources/tucan/23.jpg +++ /dev/null diff --git a/resources/tucan/30.jpg b/resources/tucan/30.jpg Binary files differdeleted file mode 100644 index 870a002..0000000 --- a/resources/tucan/30.jpg +++ /dev/null diff --git a/resources/tucan/31.jpg b/resources/tucan/31.jpg Binary files differdeleted file mode 100644 index 5e4296a..0000000 --- a/resources/tucan/31.jpg +++ /dev/null diff --git a/resources/tucan/32.jpg b/resources/tucan/32.jpg Binary files differdeleted file mode 100644 index f431879..0000000 --- a/resources/tucan/32.jpg +++ /dev/null diff --git a/resources/tucan/33.jpg b/resources/tucan/33.jpg Binary files differdeleted file mode 100644 index 337aa29..0000000 --- a/resources/tucan/33.jpg +++ /dev/null  | 
