diff options
author | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-09 13:39:10 -0800 |
---|---|---|
committer | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-09 13:39:10 -0800 |
commit | 36be1ed32ac9a554f0b11fcc13b5699e717b81f2 (patch) | |
tree | 383618d781bc5b4701b31555f90b8a629fe6d205 /Source/Puzzle.h | |
parent | 413e1f0aaae961660781675158e38520126c11b6 (diff) | |
download | witness-tutorializer-36be1ed32ac9a554f0b11fcc13b5699e717b81f2.tar.gz witness-tutorializer-36be1ed32ac9a554f0b11fcc13b5699e717b81f2.tar.bz2 witness-tutorializer-36be1ed32ac9a554f0b11fcc13b5699e717b81f2.zip |
Functioning solver/validator (at least for mazes)
Diffstat (limited to 'Source/Puzzle.h')
-rw-r--r-- | Source/Puzzle.h | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/Source/Puzzle.h b/Source/Puzzle.h new file mode 100644 index 0000000..7a98a78 --- /dev/null +++ b/Source/Puzzle.h | |||
@@ -0,0 +1,142 @@ | |||
1 | #pragma once | ||
2 | #include <memory> | ||
3 | #include <vector> | ||
4 | |||
5 | class Memory; | ||
6 | |||
7 | enum Type { | ||
8 | Stone = 0x100, | ||
9 | Square = Stone, // @Deprecated | ||
10 | Star = 0x200, | ||
11 | Poly = 0x400, | ||
12 | Eraser = 0x500, | ||
13 | Nega = Eraser, // @Deprecated | ||
14 | Triangle = 0x600, | ||
15 | RPoly = 0x1000, // @Cleanup! | ||
16 | Ylop = 0x2000, | ||
17 | }; | ||
18 | |||
19 | enum Color { | ||
20 | Black = 0x1, | ||
21 | White = 0x2, | ||
22 | Gray = 0x3, | ||
23 | Purple = 0x4, | ||
24 | Green = 0x5, | ||
25 | Cyan = 0x6, | ||
26 | Pink = 0x7, | ||
27 | Yellow = 0x8, | ||
28 | Blue = 0x9, | ||
29 | Orange = 0xA, | ||
30 | }; | ||
31 | |||
32 | struct Decoration { | ||
33 | Type type; | ||
34 | Color color; | ||
35 | int polyshape = 0; | ||
36 | // For triangles only | ||
37 | int count = 0; | ||
38 | }; | ||
39 | |||
40 | |||
41 | struct Cell { | ||
42 | inline static Cell Undefined() { | ||
43 | Cell c; | ||
44 | c.undefined = true; | ||
45 | return c; | ||
46 | } | ||
47 | bool undefined = false; | ||
48 | |||
49 | bool start = false; | ||
50 | enum class Dir {NONE, LEFT, RIGHT, UP, DOWN}; | ||
51 | Dir end = Dir::NONE; | ||
52 | std::shared_ptr<Decoration> decoration = nullptr; | ||
53 | enum class Dot {NONE, BLACK, BLUE, YELLOW, INVISIBLE}; | ||
54 | Dot dot = Dot::NONE; | ||
55 | enum class Gap {NONE, BREAK, FULL}; | ||
56 | Gap gap = Gap::NONE; | ||
57 | |||
58 | // Line color | ||
59 | enum class Color {NONE, BLACK, BLUE, YELLOW}; | ||
60 | Color color = Color::NONE; | ||
61 | }; | ||
62 | |||
63 | struct Negation {}; | ||
64 | struct Pos {int x; int y;}; | ||
65 | |||
66 | struct Puzzle { | ||
67 | int16_t height; | ||
68 | int16_t width; | ||
69 | bool hasDecorations = false; | ||
70 | |||
71 | enum class Symmetry {NONE, X, Y, XY}; | ||
72 | Symmetry sym = Symmetry::NONE; | ||
73 | bool pillar = false; | ||
74 | |||
75 | inline Cell GetCell(int x, int y) const { | ||
76 | x = Mod(x); | ||
77 | if (!SafeCell(x, y)) return Cell::Undefined(); | ||
78 | return grid[x][y]; | ||
79 | } | ||
80 | inline Cell::Color GetLine(int x, int y) const { | ||
81 | return grid[x][y].color; | ||
82 | } | ||
83 | // @TODO: | ||
84 | Pos GetSymmetricalPos(int x, int y); | ||
85 | |||
86 | bool valid; | ||
87 | std::vector<Negation> negations; | ||
88 | std::vector<Pos> invalidElements; | ||
89 | |||
90 | // private: | ||
91 | std::vector<std::vector<Cell>> grid; | ||
92 | |||
93 | private: | ||
94 | inline int Mod(int x) const { | ||
95 | if (!pillar) return x; | ||
96 | return (x + width * height * 2) % width; | ||
97 | } | ||
98 | |||
99 | inline bool SafeCell(int x, int y) const { | ||
100 | if (x < 0 || x >= width) return false; | ||
101 | if (y < 0 || y >= height) return false; | ||
102 | return true; | ||
103 | } | ||
104 | }; | ||
105 | |||
106 | class PuzzleSerializer { | ||
107 | public: | ||
108 | PuzzleSerializer(const std::shared_ptr<Memory>& memory); | ||
109 | Puzzle ReadPuzzle(int id); | ||
110 | void WritePuzzle(const Puzzle& p, int id); | ||
111 | |||
112 | private: | ||
113 | // @Bug: Blue and orange are swapped? | ||
114 | enum Flags { | ||
115 | IS_ENDPOINT = 0x1, | ||
116 | IS_STARTPOINT = 0x2, | ||
117 | IS_FULL_GAP = 0x8, | ||
118 | HAS_DOT = 0x20, | ||
119 | DOT_IS_BLUE = 0x100, | ||
120 | DOT_IS_ORANGE = 0x200, | ||
121 | DOT_IS_INVISIBLE = 0x1000, | ||
122 | HAS_ONE_CONN = 0x100000, | ||
123 | HAS_VERTI_CONN = 0x200000, | ||
124 | HAS_HORIZ_CONN = 0x400000, | ||
125 | }; | ||
126 | |||
127 | void ReadIntersections(Puzzle& p, int id); | ||
128 | void ReadDecorations(Puzzle& p, int id); | ||
129 | void WriteIntersections(const Puzzle& p, int id); | ||
130 | void WriteDecorations(const Puzzle& p, int id); | ||
131 | |||
132 | std::tuple<int, int> loc_to_xy(const Puzzle& p, int location) const; | ||
133 | int xy_to_loc(const Puzzle& p, int x, int y) const; | ||
134 | // Decoration location | ||
135 | std::tuple<int, int> dloc_to_xy(const Puzzle& p, int location) const; | ||
136 | int xy_to_dloc(const Puzzle& p, int x, int y) const; | ||
137 | Cell::Dot FlagsToDot(int flags) const; | ||
138 | // Iterate connection lists for another location which is connected to us; return that other location. | ||
139 | int FindConnection(int i, const std::vector<int>& connections_a, const std::vector<int>& connections_b) const; | ||
140 | |||
141 | std::shared_ptr<Memory> _memory; | ||
142 | }; | ||