-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
286 lines (237 loc) · 7.27 KB
/
main.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
#include <sstream>
#include <set>
#include <thread>
#include "trie.h"
#include "trie.cpp"
#include "pathfinder.h"
#include "pathfinder.cpp"
/*******************************************************************************
* Word (Boggle) Puzzle Solver v0.1
* by Ross Kwong
* Notes to self:
* - Paths are dependent on starting location:
* + Starting @ Corner: 2 paths
* + Starting @ Sides: 3 paths
* + Starting @ Middle: 4 paths
* + Else: -1 paths
* - Keep track of path (no loops!)
* - Words are a minimum 2-16 character length (Min: 2, Max: 16)
* - Reference table of letters can have up to 16 unique characters
* - Three-parter:
* 1) Loading a relevant dictionary
* 2) Searching for words
* 3) Organize words to highest points first & give start point
* - Possible to have up to 16 unique trees
* - There are approximately 470,000 words in the English language
* - Depth First Search for highest point (longer word = more points)
*
*
*******************************************************************************/
bool IsAlphaNumeric (char c) { return isalnum(c); }
int StrLength(std::string str) { return str.length(); }
void DisplayMatrix ( char arr[4][4], const int n );
bool IsWordInCharSet ( std::set <char> CharSet, std::string word );
std::string DictFileName () { return "enable1.txt"; }
std::string ReduceStrSizeByOne (std::string str);
/*
===============================================================================
UniqueCharSet()
Gathers the chars in to a set and returns it
===============================================================================
*/
std::set <char> GetCharSet ( char oldArr[4][4], const int n ) {
std::set <char> uniqueCharSet;
for( int i = 0; i < n; i++ ) {
for ( int j = 0; j < n; j++ ) {
uniqueCharSet.insert( oldArr[i][j] );
}
}
return uniqueCharSet;
}
/*
===============================================================================
ReduceStrSizeByOne()
Shortens the string length by one to remove null terminator char
===============================================================================
*/
std::string ReduceStrSizeByOne(std::string str) {
if (str.length() != 0) {
str.resize( str.length()-1 );
return str;
}
return NULL;
}
/*
===============================================================================
IsWordInCharSet()
Checks word if it belongs to set of CharSet. Returns true if so else false.
===============================================================================
*/
bool IsWordInCharSet ( std::set <char> charSet, std::string word ) {
std::set <char>::iterator it;
for ( int i = 0; i < word.length(); i++ ) {
it = charSet.find( word[i] );
if ( it != charSet.end() ) {
return true;
}
}
return false;
}
/*
===============================================================================
PrintCharSet()
Prints the list passed into the function
===============================================================================
*/
void PrintCharSet( std::set <char> charSet ) {
std::set<char>::iterator it;
std::cout << "Unique char set in the matrix: ";
for ( it = charSet.begin(); it != charSet.end(); it++ ) {
std::cout << ' ' << *it;
}
std::cout << "\n\n";
}
/*
===============================================================================
RemoveStrDelimiter()
Removes delimiter between strings
===============================================================================
*/
std::string RemoveStrDelimiter( std::string str ) {
int strLength = str.length();
if ( strLength > 0 && !IsAlphaNumeric(strLength-1) ) {
str.resize(strLength-1);
}
return str;
}
/*
===============================================================================
numOfElementsInMatrix()
Returns the number of elements in a matrix
===============================================================================
*/
int getNumberOfElementsInMatrix() {
return 16;
}
/*
===============================================================================
isStrLengthInBounds()
Checks to see if str is no bigger than matrix size
===============================================================================
*/
bool isStrLengthInUpperBounds( std::string str ) {
if ( str.length() <= getNumberOfElementsInMatrix() ) {
return true;
}
return false;
}
/*
===============================================================================
LoadTrie()
Loads dictionary based on charSet and matrixSize
===============================================================================
*/
Trie* LoadTrie( std::set <char> charSet ) {
Trie *trie = new Trie();
std::string word;
std::string filename = "enable1.txt";
std::ifstream dict1;
dict1.open( filename, std::ios::binary );
if( dict1.is_open() ) {
while( !dict1.eof() ) {
getline( dict1, word );
std::string formattedWord = RemoveStrDelimiter(word);
bool strInCharSet = IsWordInCharSet( charSet, formattedWord);
bool strLengthInBounds = isStrLengthInUpperBounds( formattedWord );
if ( strLengthInBounds && strInCharSet ) {
trie->AddWord( formattedWord );
}
}
}
dict1.close();
if ( trie != NULL ) {
std::cout << "\nLoading Complete.\n\n";
} else {
std::cout << "\nLoading Failed.\n\n";
}
return trie;
}
/*
===============================================================================
DisplayMatrix()
Displays the matrix in a 2-D NxN matrix
===============================================================================
*/
void DisplayMatrix( char arr[4][4], int n ) {
std::cout << "\n";
for ( int i = 0; i < n; i++ ) {
for ( int j = 0; j < n; j++ ) {
std::cout << arr[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
/*
===============================================================================
Test Functions
===============================================================================
*/
void TestIfWordExists ( Trie* dicTrie, std::string testWord ) {
if ( dicTrie != NULL ) {
std::cout << "Searching for " << testWord << "\n";
std::cout << "Test word Length: " << testWord.length() << "\n";
if (dicTrie->SearchWord( testWord ) ) {
std::cout << "Found " << testWord << "!\n";
std::cout << "Shit works!\n";
} else {
std::cout << "NOT FOUND " << testWord << "\n";
}
} else {
std::cout << "Fuck! Trie is empty. =(\n";
}
}
/*
===============================================================================
Main()
===============================================================================
*/
int main() {
char matrixArr[4][4];
std::string userInput;
std::cout << "Welcome to The Boggle Word-Puzzle Solver."
"Please enter in the 4x4 matrix of letters\n";
for( int i=0; i<4; i++ ) {
std::cout << "Please enter the 4 letters in the ";
switch (i) {
case 1:
std::cout << "2nd row.\n";
break;
case 2:
std::cout << "3rd row.\n";
break;
case 3:
std::cout << "4th row.\n";
break;
default:
std::cout << "1st row.\n";
break;
}
cin >> userInput;
for( int j=0; j<4; j++ ) {
matrixArr[i][j] = userInput.at(j);
}
}
std::set<char> charSet = GetCharSet ( matrixArr, 4 );
Trie *dicTrie = LoadTrie( charSet );
std::cout << "The matrix is: \n";
PathFinder PathFinder( matrixArr, dicTrie );
return 0;
}