about summary refs log tree commit diff stats
path: root/Source/Puzzle.cpp
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2020-03-03 21:25:56 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2020-03-03 21:25:56 -0800
commit3ece17f201686342e8aff90148f360245788b234 (patch)
treea1deba5c1b09ecdc56d2c94b880ef8767289371d /Source/Puzzle.cpp
parente77cfa1596f996cc50c1601ee29891a7fb7eb62d (diff)
downloadwitness-tutorializer-3ece17f201686342e8aff90148f360245788b234.tar.gz
witness-tutorializer-3ece17f201686342e8aff90148f360245788b234.tar.bz2
witness-tutorializer-3ece17f201686342e8aff90148f360245788b234.zip
add support for puzzle serialization
Diffstat (limited to 'Source/Puzzle.cpp')
-rw-r--r--Source/Puzzle.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/Source/Puzzle.cpp b/Source/Puzzle.cpp index f0664c7..101ec7c 100644 --- a/Source/Puzzle.cpp +++ b/Source/Puzzle.cpp
@@ -45,6 +45,94 @@ Pos Puzzle::GetSymmetricalPos(int x, int y) const {
45 return Pos{x, y}; 45 return Pos{x, y};
46} 46}
47 47
48#include "json.hpp"
49nlohmann::json newGrid(int width, int height) {
50 using namespace nlohmann;
51
52 json grid;
53 for (int x=0; x<width; x++) {
54 grid[x] = json::array();
55 for (int y=0; y<height; y++) {
56 if (x%2 == 1 && y%2 == 1) grid[x][y] = json::object();
57 else grid[x][y] = {{"type", "line"}, {"color", 0}};
58 }
59 }
60 return grid;
61}
62
63std::string Puzzle::Serialize() {
64 using namespace nlohmann;
65
66 json puzzle;
67 puzzle["grid"] = {};
68 for (int x=0; x<width; x++) {
69 puzzle["grid"][x] = json::array();
70 for (int y=0; y<height; y++) {
71 if (x%2 == 1 && y%2 == 1) puzzle["grid"][x][y] = json::object();
72 else puzzle["grid"][x][y] = {{"type", "line"}, {"color", 0}};
73 }
74 }
75 puzzle["pillar"] = pillar;
76 if (symmetry == Symmetry::X) puzzle["symmetry"] = {{"x", true}, {"y", false}};
77 else if (symmetry == Symmetry::Y) puzzle["symmetry"] = {{"x", false}, {"y", true}};
78 else if (symmetry == Symmetry::XY) puzzle["symmetry"] = {{"x", true}, {"y", true}};
79
80 for (int x=0; x<width; x++) {
81 for (int y=0; y<height; y++) {
82 const Cell& cell = grid[x][y];
83 json j = puzzle["grid"][x][y];
84 if (cell.dot != Cell::Dot::NONE) {
85 if (cell.dot == Cell::Dot::BLACK) j["dot"] = 1;
86 if (cell.dot == Cell::Dot::BLUE) j["dot"] = 2;
87 if (cell.dot == Cell::Dot::YELLOW) j["dot"] = 3;
88 if (cell.dot == Cell::Dot::INVISIBLE) j["dot"] = 4;
89 } else if (cell.gap != Cell::Gap::NONE) {
90 if (cell.gap == Cell::Gap::BREAK) j["gap"] = 1;
91 if (cell.gap == Cell::Gap::FULL) j["gap"] = 2;
92 } else if (cell.decoration != nullptr) {
93 if (cell.decoration->color == Color::Black) j["color"] = "black";
94 else if (cell.decoration->color == Color::Blue) j["color"] = "blue";
95 else if (cell.decoration->color == Color::Cyan) j["color"] = "cyan";
96 else if (cell.decoration->color == Color::Gray) j["color"] = "gray";
97 else if (cell.decoration->color == Color::Green) j["color"] = "green";
98 else if (cell.decoration->color == Color::Orange) j["color"] = "orange";
99 else if (cell.decoration->color == Color::Pink) j["color"] = "pink";
100 else if (cell.decoration->color == Color::Purple) j["color"] = "purple";
101 else if (cell.decoration->color == Color::White) j["color"] = "white";
102 else if (cell.decoration->color == Color::Yellow) j["color"] = "yellow";
103
104 if (cell.decoration->type == Type::Stone) {
105 j["type"] = "square";
106 } else if (cell.decoration->type == Type::Star) {
107 j["type"] = "star";
108 } else if (cell.decoration->type == Type::Poly) {
109 j["type"] = "poly";
110 j["polyshape"] = cell.decoration->polyshape;
111 } else if (cell.decoration->type == Type::Ylop) {
112 j["type"] = "ylop";
113 j["polyshape"] = cell.decoration->polyshape;
114 } else if (cell.decoration->type == Type::Nega) {
115 j["type"] = "nega";
116 } else if (cell.decoration->type == Type::Triangle) {
117 j["type"] = "triangle";
118 j["count"] = cell.decoration->count;
119 }
120 }
121
122 if (cell.start) j["start"] = true;
123 if (cell.end != Cell::Dir::NONE) {
124 if (cell.end == Cell::Dir::LEFT) j["end"] = "left";
125 else if (cell.end == Cell::Dir::RIGHT) j["end"] = "right";
126 else if (cell.end == Cell::Dir::UP) j["end"] = "top";
127 else if (cell.end == Cell::Dir::DOWN) j["end"] = "bottom";
128 }
129 puzzle["grid"][x][y] = j;
130 }
131 }
132
133 return puzzle.dump();
134}
135
48int Puzzle::Mod(int x) const { 136int Puzzle::Mod(int x) const {
49 if (!pillar) return x; 137 if (!pillar) return x;
50 return (x + width * height * 2) % width; 138 return (x + width * height * 2) % width;