#include "puzzle.hpp" #include "gameplay.hpp" #include #include #include #include #include void GameParams::CalculateStandardPuzzlePos() { coordinates tmp; int i, j, k = 0; for(i = 0; i < puzzles_per_side; ++i) for(j = 0; j < puzzles_per_side; ++j, ++k) { if(i == puzzles_per_side-1 && j == puzzles_per_side-1) break; tmp.x = i * (puzzle_size + spacing) + spacing; tmp.y = j * (puzzle_size + spacing) + spacing; standard_puzzle_coordinates[k] = tmp; } } void GameParams::ResetFreePuzzles() { for(int i = 0; i < puzzle_pieces; ++i) free_puzzles[i] = 1; } static void find_path_to_picture(std::string& path, int number) { path = "resources/tucan/" + std::to_string(number / puzzles_per_side) + std::to_string(number % puzzles_per_side) + ".jpg"; } void GameParams::NextUntestedPuzzles() { int idx_random_puzzle; std::unique_ptr tmp_puzzle; /* * check if puzzles already were created */ if(puzzles.size() != 0) puzzles.clear(); /* * ======== creating puzzles =========== */ win->begin(); for(int i = 0; i < puzzle_pieces; ++i) { tmp_puzzle = std::unique_ptr(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; 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(); tmp_puzzle->image(img); tmp_puzzle->callback(press_button_callback, this); puzzles.push_back(std::move(tmp_puzzle)); } win->end(); ResetFreePuzzles(); } bool GameParams::IsSolvability() { int counter = 0; for(size_t i = 0; i < puzzles.size(); ++i) for(size_t j = i+1; j < puzzles.size(); ++j) if(puzzles[i]->sequence_number > puzzles[j]->sequence_number) ++counter; return counter % 2 == 0; } void GameParams::CreateNewPuzzles() { do { NextUntestedPuzzles(); } while(!IsSolvability()); }