-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzleSolver.cpp
More file actions
145 lines (133 loc) · 4.22 KB
/
PuzzleSolver.cpp
File metadata and controls
145 lines (133 loc) · 4.22 KB
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 <tuple>
#include "PuzzleSolver.h"
PuzzleSolver::PuzzleSolver(int length) {
combinationLength = length;
}
void PuzzleSolver::findCombinationsFromPos(string combination, array<int, 2> currentPos) {
cout << "\ncombination so far: " << combination;
// Clear the combination if it contains the error-character "*" or if it contains >2 vowels
if (combination[combination.length() - 1] == *"*" || checkForInvalidVowels(combination)) {
cout << " - Bad combo";
return;
}
// Save combination and return if combination is correct length
else if (combination.length() >= combinationLength) {
combinations.push_back(combination);
return;
}
else {
// Else recursively call self up to 8 times, once for each possible move
array<int, 2> newPos;
char newChar;
newPos = moveNNE(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveNEE(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveSEE(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveSSE(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveSSW(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveSWW(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveNWW(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
newPos = moveNNW(currentPos);
if (newPos[0] >= 0 && newPos[1] >= 0) {
newChar = getLetterAtPos(newPos);
findCombinationsFromPos(combination + newChar, newPos);
}
}
}
char PuzzleSolver::getLetterAtPos(array<int, 2> pos) {
// Get the letter found at a given position (specified by [row, col])
int row = pos[0];
int col = pos[1];
if (col < 0 || col >= keyboard[0].size() || row < 0 || row >= keyboard.size())
// If specified position is out of range of the table, return '*' to signify Invalid.
return '*';
else {
return keyboard[row][col];
}
}
bool PuzzleSolver::checkForInvalidVowels(string combination) {
// Test if there are more than 2 vowels in a combination
int count = 0;
for (char c : combination) {
if (vowels.find(c) != vowels.end())
count++;
if (count > 2)
return true;
}
return false;
}
array<int, 2> PuzzleSolver::moveSEE(array<int, 2> pos) {
// Move South, East, East, then find all new combinations stemming from that position
int newRow = pos[0] + 1;
int newCol = pos[1] + 2;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveSSE(array<int, 2> pos) {
// Move South, South, East
int newRow = pos[0] + 2;
int newCol = pos[1] + 1;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveSSW(array<int, 2> pos) {
// Move South, South, West
int newRow = pos[0] + 2;
int newCol = pos[1] - 1;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveSWW(array<int, 2> pos) {
// Move South, West, West
int newRow = pos[0] + 1;
int newCol = pos[1] - 2;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveNWW(array<int, 2> pos) {
// Move North, West, West
int newRow = pos[0] - 1;
int newCol = pos[1] - 2;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveNNW(array<int, 2> pos) {
// Move North, North, West
int newRow = pos[0] - 2;
int newCol = pos[1] - 1;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveNNE(array<int, 2> pos) {
// Move North, North, East
int newRow = pos[0] - 2;
int newCol = pos[1] + 1;
return array<int, 2>{newRow, newCol};
}
array<int, 2> PuzzleSolver::moveNEE(array<int, 2> pos) {
// Move North, East, East
int newRow = pos[0] - 1;
int newCol = pos[1] + 2;
return array<int, 2>{newRow, newCol};
}