#include "gameplay.hpp" #include "puzzle.hpp" #include #include #include #include #include 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); } 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); 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->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->SelectRandomPicture(); gp->ResetFreePuzzles(); gp->CreateNewPuzzles(); gp->win->redraw(); }