Wednesday, June 7, 2017

Wordament Boggle solver

Here is the code which will work on all unix systems and give all the words that can be formed in wordament game.

Few highlights for the code :

1) Code can be easily modified to give only big words for more points.
2) Also longer words are printed first. This feature can also be easily changed.
3) To compile the code just use g++ compiler. (g++ test.cpp).
4) and run it using ./a.out
5) input the wordament game in row first format.
6) This code was written using code from g4g site.
7) I combined the code given in "g4g boggle solver" and "g4g tries" and combined them to solve wordament.

Code :
 
#include<iostream>
#include<cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <pthread.h>
using namespace std;

#define M 4
#define N 4
#define MAX 15
#define MIN 7

struct TrieNode *dictionary;//  = getNode();

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

// Alphabet size (# of symbols)
#define ALPHABET_SIZE (26)

// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')

string boggle[M][N];// = {{'G','I','Z'},
// trie node
vector<string> ans;

struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];

// isLeaf is true if the node represents
// end of a word
bool isLeaf;
};

// Returns new trie node (initialized to NULLs)
struct TrieNode *getNode(void)
{
struct TrieNode *pNode = NULL;

pNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));

if (pNode)
{
int i;

pNode->isLeaf = false;

for (i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
}

return pNode;
}

// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;

struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();

pCrawl = pCrawl->children[index];
}

// mark last node as leaf
pCrawl->isLeaf = true;
}

// Returns true if key presents in trie, else false
bool search(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;
struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);

if (!pCrawl->children[index])
return false;

pCrawl = pCrawl->children[index];
}

return (pCrawl != NULL && pCrawl->isLeaf);
}




// A given function to check if a given string is present in
// dictionary. The implementation is naive for simplicity. As
// per the question dictionary is givem to us.
bool isWord(string &str)
{
if (search(dictionary, str) != 0)
return true;
return false;
}

// A recursive function to print all words present on boggle
void findWordsUtil(bool visited[M][N], int i,
int j, string &str, int size)
{
// Mark current cell as visited and append current character
// to str
//int size;
visited[i][j] = true;
str = str + boggle[i][j];
size = size+boggle[i][j].size();

// If str is present in dictionary, then print it
if (size<= MAX  && size>=MIN && isWord(str))
ans.push_back(str);//cout << str << endl;

// Traverse 8 adjacent cells of boggle[i][j]
for (int row=i-1; row<=i+1 && row<M; row++)
for (int col=j-1; col<=j+1 && col<N; col++)
if (row>=0 && col>=0 && !visited[row][col])
findWordsUtil(visited, row, col, str, size);

// Erase current character from string and mark visited
// of current cell as false
str.erase(str.length()-boggle[i][j].size());
size = size - boggle[i][j].size();
visited[i][j] = false;
}

// Prints all words present in dictionary.
void findWords() {
bool visited[M][N] = {{false}};
string str = "";
for (int i=0; i<M; i++)
      for (int j=0; j<N; j++)
findWordsUtil(visited, i, j, str, 0);

}

int all_chars(string s) {
int t = s.size();
for (int i  = 0; i<t; i++) {
if (!(s[i] >= 'a' && s[i] <= 'z'))
return 0;
}
return 1;
}

void load_dictionary() {
string line;
int n = 0;
ifstream myfile("/usr/share/dict/words");
dictionary = getNode();
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
//std::transform(line.begin(), line.end(), line.begin(), ::tolower);
//dictionary.push_back(line);
if (all_chars(line) && line.size()<=MAX && line.size()>=MIN) {
insert(dictionary, line);
n++;
}
}
myfile.close();
}
cout <<"words added = "<<n<<endl;
}


void enter_boog(string boggle[M][N]) {
int i,j;
for (i= 0; i<M; i++) {
for (j = 0; j<N; j++) {
cin>>boggle[i][j];
}
}
}

bool compare(string s1, string s2) {
int i = s1.size();
int j = s2.size();
if ( i != j)
return i > j;
else
return (s1[0] - s2[0] > 0);
}
void display_words() {
sort(ans.begin(), ans.end(), compare);
for (int i=0; i<ans.size(); i++) {
cout<<ans[i]<<endl;
}
}
// Driver program to test above function
int main()
{
load_dictionary();
while (1) {
cout<<"enter boggle matrix";
enter_boog(boggle);
for (int i= 0; i<M; i++) {
for (int j = 0; j<N; j++) {
cout<<boggle[i][j]<<" ";
}
cout<<endl;
}

cout << "Following words of dictionary are present\n";

findWords();
display_words();
ans.resize(0);
}
return 0;
}