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/Validator.cpp | |
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/Validator.cpp')
-rw-r--r-- | Source/Validator.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/Source/Validator.cpp b/Source/Validator.cpp new file mode 100644 index 0000000..82d6779 --- /dev/null +++ b/Source/Validator.cpp | |||
@@ -0,0 +1,97 @@ | |||
1 | #include "Validator.h" | ||
2 | #include "Puzzle.h" | ||
3 | |||
4 | void Validator::Validate(Puzzle& p) { | ||
5 | // console.log('Validating', puzzle); | ||
6 | p.valid = true; // Assume valid until we find an invalid element | ||
7 | p.invalidElements.clear(); | ||
8 | p.negations.clear(); | ||
9 | |||
10 | bool puzzleHasSymbols = false; | ||
11 | bool puzzleHasStart = false; | ||
12 | bool puzzleHasEnd = false; | ||
13 | // Validate gap failures as an early exit. | ||
14 | for (int x = 0; x < p.width; x++) { | ||
15 | for (int y = 0; y < p.height; y++) { | ||
16 | Cell cell = p.grid[x][y]; | ||
17 | auto decoration = cell.decoration; | ||
18 | if (decoration) { | ||
19 | if (decoration->type == Type::Stone || | ||
20 | decoration->type == Type::Star || | ||
21 | decoration->type == Type::Nega || | ||
22 | decoration->type == Type::Poly || | ||
23 | decoration->type == Type::Ylop) { | ||
24 | puzzleHasSymbols = true; | ||
25 | continue; | ||
26 | } | ||
27 | if (decoration->type == Type::Triangle) { | ||
28 | int actualCount = 0; | ||
29 | if (p.GetLine(x - 1, y) != Cell::Color::NONE) actualCount++; | ||
30 | if (p.GetLine(x + 1, y) != Cell::Color::NONE) actualCount++; | ||
31 | if (p.GetLine(x, y - 1) != Cell::Color::NONE) actualCount++; | ||
32 | if (p.GetLine(x, y + 1) != Cell::Color::NONE) actualCount++; | ||
33 | if (decoration->count != actualCount) { | ||
34 | // console.log('Triangle at grid['+x+']['+y+'] has', actualCount, 'borders') | ||
35 | p.invalidElements.emplace_back(Pos{x, y}); | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | if (cell.gap != Cell::Gap::NONE && cell.color != Cell::Color::NONE) { | ||
40 | // console.log('Gap at', x, y, 'is covered') | ||
41 | p.valid = false; | ||
42 | } | ||
43 | if (cell.dot != Cell::Dot::NONE) { | ||
44 | if (cell.color == Cell::Color::NONE) { | ||
45 | // console.log('Dot at', x, y, 'is not covered') | ||
46 | p.invalidElements.emplace_back(Pos{x, y}); | ||
47 | } else if (cell.color == Cell::Color::BLUE && cell.dot == Cell::Dot::YELLOW) { | ||
48 | // console.log('Yellow dot at', x, y, 'is covered by blue line') | ||
49 | p.valid = false; | ||
50 | } else if (cell.color == Cell::Color::YELLOW && cell.dot == Cell::Dot::BLUE) { | ||
51 | // console.log('Blue dot at', x, y, 'is covered by yellow line') | ||
52 | p.valid = false; | ||
53 | } | ||
54 | } | ||
55 | if (cell.color != Cell::Color::NONE) { | ||
56 | if (cell.start == true) puzzleHasStart = true; | ||
57 | if (cell.end != Cell::Dir::NONE) puzzleHasEnd = true; | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | if (!puzzleHasStart || !puzzleHasEnd) { | ||
62 | // console.log('There is no covered start or endpoint') | ||
63 | p.valid = false; | ||
64 | } | ||
65 | |||
66 | // Perf optimization: We can skip computing regions if the grid has no symbols. | ||
67 | if (!puzzleHasSymbols) { // No additional symbols, and we already checked dots & gaps | ||
68 | p.valid &= (p.invalidElements.size() == 0); | ||
69 | } else { // Additional symbols, so we need to discard dots & divide them by region | ||
70 | /* | ||
71 | p.invalidElements.clear(); | ||
72 | std::vector<Region> regions = p.GetRegions(); | ||
73 | // console.log('Found', regions.length, 'regions'); | ||
74 | // console.debug(regions); | ||
75 | |||
76 | for (const Region& region : regions) { | ||
77 | std::string key = region.grid.ToString(); | ||
78 | auto regionData = puzzle.regionCache[key]; | ||
79 | if (regionData == undefined) { | ||
80 | console.log('Cache miss for region', region, 'key', key); | ||
81 | regionData = _regionCheckNegations(puzzle, region); | ||
82 | // Entirely for convenience | ||
83 | regionData.valid = (regionData.invalidElements.size() == 0) | ||
84 | // console.log('Region valid:', regionData.valid); | ||
85 | |||
86 | if (!DISABLE_CACHE) { | ||
87 | p.regionCache[key] = regionData; | ||
88 | } | ||
89 | } | ||
90 | p.negations = p.negations.concat(regionData.negations); | ||
91 | p.invalidElements = p.invalidElements.concat(regionData.invalidElements); | ||
92 | p.valid = p.valid && regionData.valid; | ||
93 | } | ||
94 | */ | ||
95 | } | ||
96 | // console.log('Puzzle has', puzzle.invalidElements.length, 'invalid elements') | ||
97 | } | ||