about summary refs log tree commit diff stats
path: root/Source/Puzzle.cpp
blob: 101ec7c83f6a5d99a5c7bc81e5ac22c2dbad26bc (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "pch.h"
#include "Puzzle.h"

Cell Puzzle::GetCell(int x, int y) const {
    x = Mod(x);
    if (!SafeCell(x, y)) return Cell::Undefined();
    return grid[x][y];
}

Cell::Color Puzzle::GetLine(int x, int y) const {
    return grid[x][y].color;
}

void Puzzle::NewGrid(int newWidth, int newHeight) {
    if (newWidth == 0) {
        assert(false);
        newWidth = width;
        newHeight = height;
    } else {
        // @Cleanup! This should be in the ctor...
        width = 2*newWidth + 1;
        height = 2*newHeight + 1;
    }
    grid.clear();
    grid.resize(width);
    for (int x=0; x<width; x++) grid[x].resize(height);
}

Pos Puzzle::GetSymmetricalPos(int x, int y) const {
    if (symmetry != Symmetry::NONE) {
        if (pillar) {
            x += width/2;
            if (symmetry & Symmetry::X) {
                x = width - x;
            }
        } else {
            if (symmetry & Symmetry::X) {
                x = (width-1) - x;
            }
        }
        if (symmetry & Symmetry::Y) {
            y = (height-1) - y;
        }
    }
    return Pos{x, y};
}

#include "json.hpp"
nlohmann::json newGrid(int width, int height) {
    using namespace nlohmann;

    json grid;
    for (int x=0; x<width; x++) {
        grid[x] = json::array();
        for (int y=0; y<height; y++) {
            if (x%2 == 1 && y%2 == 1) grid[x][y] = json::object();
            else grid[x][y] = {{"type", "line"}, {"color", 0}};
        }
    }
    return grid;
}

std::string Puzzle::Serialize() {
    using namespace nlohmann;

    json puzzle;
    puzzle["grid"] = {};
    for (int x=0; x<width; x++) {
        puzzle["grid"][x] = json::array();
        for (int y=0; y<height; y++) {
            if (x%2 == 1 && y%2 == 1) puzzle["grid"][x][y] = json::object();
            else puzzle["grid"][x][y] = {{"type", "line"}, {"color", 0}};
        }
    }
    puzzle["pillar"] = pillar;
    if (symmetry == Symmetry::X)        puzzle["symmetry"] = {{"x", true},  {"y", false}};
    else if (symmetry == Symmetry::Y)   puzzle["symmetry"] = {{"x", false}, {"y", true}};
    else if (symmetry == Symmetry::XY)  puzzle["symmetry"] = {{"x", true},  {"y", true}};

    for (int x=0; x<width; x++) {
        for (int y=0; y<height; y++) {
            const Cell& cell = grid[x][y];
            json j = puzzle["grid"][x][y];
            if (cell.dot != Cell::Dot::NONE) {
                if (cell.dot == Cell::Dot::BLACK)       j["dot"] = 1;
                if (cell.dot == Cell::Dot::BLUE)        j["dot"] = 2;
                if (cell.dot == Cell::Dot::YELLOW)      j["dot"] = 3;
                if (cell.dot == Cell::Dot::INVISIBLE)   j["dot"] = 4;
            } else if (cell.gap != Cell::Gap::NONE) {
                if (cell.gap == Cell::Gap::BREAK)       j["gap"] = 1;
                if (cell.gap == Cell::Gap::FULL)        j["gap"] = 2;
            } else if (cell.decoration != nullptr) {
                if (cell.decoration->color == Color::Black)         j["color"] = "black";
                else if (cell.decoration->color == Color::Blue)     j["color"] = "blue";
                else if (cell.decoration->color == Color::Cyan)     j["color"] = "cyan";
                else if (cell.decoration->color == Color::Gray)     j["color"] = "gray";
                else if (cell.decoration->color == Color::Green)    j["color"] = "green";
                else if (cell.decoration->color == Color::Orange)   j["color"] = "orange";
                else if (cell.decoration->color == Color::Pink)     j["color"] = "pink";
                else if (cell.decoration->color == Color::Purple)   j["color"] = "purple";
                else if (cell.decoration->color == Color::White)    j["color"] = "white";
                else if (cell.decoration->color == Color::Yellow)   j["color"] = "yellow";

                if (cell.decoration->type == Type::Stone) {
                    j["type"] = "square";
                } else if (cell.decoration->type == Type::Star) {
                    j["type"] = "star";
                } else if (cell.decoration->type == Type::Poly) {
                    j["type"] = "poly";
                    j["polyshape"] = cell.decoration->polyshape;
                } else if (cell.decoration->type == Type::Ylop) {
                    j["type"] = "ylop";
                    j["polyshape"] = cell.decoration->polyshape;
                } else if (cell.decoration->type == Type::Nega) {
                    j["type"] = "nega";
                } else if (cell.decoration->type == Type::Triangle) {
                    j["type"] = "triangle";
                    j["count"] = cell.decoration->count;
                }
            }

            if (cell.start) j["start"] = true;
            if (cell.end != Cell::Dir::NONE) {
                if (cell.end == Cell::Dir::LEFT)        j["end"] = "left";
                else if (cell.end == Cell::Dir::RIGHT)  j["end"] = "right";
                else if (cell.end == Cell::Dir::UP)     j["end"] = "top";
                else if (cell.end == Cell::Dir::DOWN)   j["end"] = "bottom";
            }
            puzzle["grid"][x][y] = j;
        }
    }

    return puzzle.dump();
}

int Puzzle::Mod(int x) const {
    if (!pillar) return x;
    return (x + width * height * 2) % width;
}

bool Puzzle::SafeCell(int x, int y) const {
    if (x < 0 || x >= width) return false;
    if (y < 0 || y >= height) return false;
    return true;
}