back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/puzzle.cpp
blob: 6d7d6351f99b306d4ea0d1305445bf69e1147e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "puzzle.hpp"

#include <stdlib.h>
#include <string>
#include <FL/Fl_JPEG_Image.H>

void GameParams::CalculatePuzzlePos()
{
    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;
            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::CreateNewPuzzles()
{
    int idx_random_puzzle;
    /*
     * check if puzzles already were created
     */
    for(int i = 0; i < puzzle_pieces; ++i)
        if(puzzles[i])
            delete puzzles[i];
    /*
     * ======== creating puzzles ===========
     */
    for(int i = 0; i < puzzle_pieces; ++i) {
        puzzles[i] =
            new Puzzle(puzzle_coordinates[i].x, 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());
        // TODO check fails img->fail();
        puzzles[i]->image(img);
    }
}

void GameParams::NewGame()
{
    ResetFreePuzzles();
    CreateNewPuzzles();
}