back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/gameplay.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gameplay.cpp')
-rw-r--r--gameplay.cpp42
1 files changed, 40 insertions, 2 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 <FL/Fl_Group.H>
+#include <FL/fl_ask.H>
+
+#include <algorithm>
+#include <memory>
+#include <vector>
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<Puzzle>& 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<GameParams*>(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();
+}