From 3ece17f201686342e8aff90148f360245788b234 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Tue, 3 Mar 2020 21:25:56 -0800 Subject: add support for puzzle serialization --- Source/Puzzle.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'Source/Puzzle.cpp') 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 { return Pos{x, y}; } +#include "json.hpp" +nlohmann::json newGrid(int width, int height) { + using namespace nlohmann; + + json grid; + for (int x=0; xcolor == 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; -- cgit 1.4.1