From 9023f43291b894f7670df2ffe513808ef6360954 Mon Sep 17 00:00:00 2001 From: scratko Date: Sun, 10 Nov 2024 15:16:31 +0300 Subject: Added end game check --- gameplay.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- gameplay.hpp | 6 ++++++ main.cpp | 8 ++++---- puzzle.cpp | 9 ++------- puzzle.hpp | 17 +++++++++++------ 5 files changed, 63 insertions(+), 19 deletions(-) diff --git a/gameplay.cpp b/gameplay.cpp index d80a4c9..0c6a1ab 100644 --- a/gameplay.cpp +++ b/gameplay.cpp @@ -1,6 +1,11 @@ #include "gameplay.hpp" #include "puzzle.hpp" -#include "FL/Fl_Group.H" +#include +#include + +#include +#include +#include static bool is_next_to_empty_box(GameParams::coordinates empty_box_pos, GameParams::coordinates current_pos) @@ -16,6 +21,27 @@ static bool is_next_to_empty_box(GameParams::coordinates empty_box_pos, current_pos.y + puzzle_size + spacing == empty_box_pos.y); } +static bool is_coordinate_match(GameParams::coordinates& first, + GameParams::coordinates& second) +{ + return first.x == second.x && first.y == second.y; +} + +bool PuzzleGame::IsFinalPlacement(GameParams *gp) +{ + bool is_match = 1; + auto lambda = [gp, &is_match](std::unique_ptr& next_puzzle) { + GameParams::coordinates pos_next_puzzle = + { next_puzzle->x(), next_puzzle->y() }; + GameParams::coordinates target_pos = + gp->standard_puzzle_coordinates[next_puzzle->sequence_number]; + if(!is_coordinate_match(pos_next_puzzle, target_pos)) + is_match = 0; + }; + std::for_each(gp->puzzles.begin(), gp->puzzles.end(), lambda); + return is_match; +} + void press_button_callback(Fl_Widget *w, void *params) { GameParams *gp = reinterpret_cast(params); @@ -24,7 +50,19 @@ void press_button_callback(Fl_Widget *w, void *params) w->position(gp->GetXYEmptyBox().x, gp->GetXYEmptyBox().y); gp->SetXYEmptyBox(current_pos.x, current_pos.y); } - w->redraw(); w->parent()->redraw(); + if(PuzzleGame::IsFinalPlacement(gp)) { + int answer = fl_choice("You win. Play again?", "No", "Yes", nullptr); + if(answer) + PuzzleGame::StartGame(gp); + else + gp->win->hide(); + } } +void PuzzleGame::StartGame(GameParams *gp) +{ + gp->ResetFreePuzzles(); + gp->CreateNewPuzzles(); + gp->win->redraw(); +} diff --git a/gameplay.hpp b/gameplay.hpp index 22e0ab7..0069772 100644 --- a/gameplay.hpp +++ b/gameplay.hpp @@ -5,4 +5,10 @@ void press_button_callback(Fl_Widget *w, void *params); +class PuzzleGame { +public: + static bool IsFinalPlacement(GameParams *gp); + static void StartGame(GameParams *gp); +}; + #endif diff --git a/main.cpp b/main.cpp index 0cbe9ea..d5d20d5 100644 --- a/main.cpp +++ b/main.cpp @@ -5,14 +5,14 @@ #include #include "puzzle.hpp" +#include "gameplay.hpp" int main() { srand(time(nullptr)); - Fl_Window *win = new Fl_Window(325, 325, "15 puzzle"); - GameParams *params = GameParams::SetUpParams(); - params->NewGame(); - win->end(); + Fl_Window *win = new Fl_Window(325, 325, "Picture puzzle"); + GameParams *params = GameParams::SetUpParams(win); + PuzzleGame::StartGame(params); win->show(); return Fl::run(); return 0; diff --git a/puzzle.cpp b/puzzle.cpp index 50b32af..ca55ef9 100644 --- a/puzzle.cpp +++ b/puzzle.cpp @@ -47,6 +47,7 @@ void GameParams::NextUntestedPuzzles() /* * ======== creating puzzles =========== */ + win->begin(); for(int i = 0; i < puzzle_pieces; ++i) { tmp_puzzle = std::unique_ptr(new Puzzle(standard_puzzle_coordinates[i].x, @@ -65,6 +66,7 @@ void GameParams::NextUntestedPuzzles() tmp_puzzle->callback(press_button_callback, this); puzzles.push_back(std::move(tmp_puzzle)); } + win->end(); ResetFreePuzzles(); } @@ -84,10 +86,3 @@ void GameParams::CreateNewPuzzles() NextUntestedPuzzles(); } while(!IsSolvability()); } - -void GameParams::NewGame() -{ - ResetFreePuzzles(); - CreateNewPuzzles(); - SetXYEmptyBox(215, 215); -} diff --git a/puzzle.hpp b/puzzle.hpp index 5f51877..39a4752 100644 --- a/puzzle.hpp +++ b/puzzle.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -14,12 +15,12 @@ enum { spacing = 5 }; -class Puzzle : Fl_Button{ +class Puzzle : public Fl_Button{ +public: unsigned char sequence_number; std::string path; Puzzle(int x, int y) : Fl_Button(x, y, puzzle_size, puzzle_size) {} - friend class GameParams; }; class GameParams { @@ -32,20 +33,24 @@ private: char free_puzzles[puzzle_pieces]; coordinates empty_box; std::vector> puzzles; + Fl_Window *win; + + GameParams(Fl_Window *a_win = nullptr) : win(a_win) {} - GameParams() {} void CalculateStandardPuzzlePos(); void ResetFreePuzzles(); void NextUntestedPuzzles(); bool IsSolvability(); void CreateNewPuzzles(); + friend class PuzzleGame; + friend void press_button_callback(Fl_Widget*, void*); public: - void NewGame(); 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; + static GameParams *SetUpParams(Fl_Window *win) { + GameParams *gi = new GameParams(win); gi->CalculateStandardPuzzlePos(); + gi->SetXYEmptyBox(215, 215); return gi; } }; -- cgit v1.2.3