#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
#define MAXLENGTH 20
#define WORDLENGTH 20
enum dir { LEFT=0,UPLEFT,UP,UPRIGHT,RIGHT,BOTTOMRIGHT,BOTTOM,BOTTOMLEFT};
enum menu { LOAD=0,SOLVE,CREATE,QUIT };
struct WORD
{
char word[WORDLENGTH];
int length;
int row;
int col;
dir d;
};
typedef struct WORD word;
word wordlist[100];
char puzzle[MAXLENGTH][MAXLENGTH];
int nrow,ncol,nwords;
char direction[8][15]={"LEFT","UPPERLEFT","UP","UPPERRIGHT","RIGHT","BOTTOMRIGHT","BOTTOM","BOTTOMLEFT"};
int loaded;
enum menu readMenu();
int loadPuzzle();
int solvePuzzle();
int createPuzzle();
void printUnsolvedPuzzle();
int main()
{
menu menuItem;
loaded=-1;
while(1)
{
menuItem=readMenu();
if(menuItem==LOAD)
{
loaded=loadPuzzle();
}
else if(menuItem==SOLVE)
{
solvePuzzle();
}
else if(menuItem==CREATE)
{
createPuzzle();
}
else if(menuItem==QUIT)
{
cout<<"\nGood bye ! demo version by inadilemma";
cin.get();
return 0;
}
}
}
enum menu readMenu()
{
while(1)
{
fflush(stdin);
cout<<"\n\t\t**********************";
cout<<"\n\t\t* *";
cout<<"\n\t\t* FIND A WORD *";
cout<<"\n\t\t* *";
cout<<"\n\t\t**********************\n";
cout<<"\n1: Load Puzzle.\n2: Solve Puzzle.\n3: Create Puzzle.\n4: Quit.";
cout<<"\nEnter your choice: ";
char ch;
cin>>ch;
if(ch=='1')return LOAD;
if(ch=='2')return SOLVE;
if(ch=='3')return CREATE;
if(ch=='4')return QUIT;
cout<<"\nInvalid input !";
}
}
int loadPuzzle()
{
char filename[256];
int i;
cout<<"\nEnter filename : ";
cin>>filename;
ifstream fin(filename);
if(fin==NULL)
{
cout<<"\nInvalid filename \nPress any key...";
cin.get();
return -1;
}
fin>>nrow>>ncol;
nwords=0;
for(i=0;i<nrow;i++)
{
fin>>puzzle[i];
if(strlen(puzzle[i])!=ncol)
{
cout<<"\nInvalid puzzle \nPress any key...";
cin.get();
return -1;
}
}
while(1)
{
fin>>wordlist[nwords].word;
wordlist[nwords].length=strlen(wordlist[nwords].word);
wordlist[nwords].row=-1;
wordlist[nwords].col=-1;
nwords++;
if(fin.eof())break;
}
cout <<"\nPuzzle: "<< filename <<"\n\n";
printUnsolvedPuzzle();
fin.close();
return 0;
}
void printUnsolvedPuzzle()
{
int i,j;
for(i=0;i<nrow;i++)
{
for(j=0;j<ncol;j++)
{
cout<<"\t"<<puzzle[i][j] <<" ";
}
cout<<"\n";
}
cout<<"\n\nWords :\n--------";
for(i=0;i<nwords;i++)
{
if(i%4==0)cout<<"\n";
cout<<wordlist[i].word<<"\t\t\t";
}
cout<<"\n";
cin.get();
}
int createPuzzle()
{
cout<<"\nPuzzle created";
cout<<"\nPress any key to continue...";
cin.get();
return 0;
}
int solvePuzzle()
{
if(loaded!=0)
{
cout<<"\nNo puzzle loaded";
cout<<"\nPress any key to continue...";
cin.get();
return -1;
}
int i,j,k,n,found=0;
for(i=0;i<nwords;i++)
{
found=0;
for(j=0;j<nrow;j++)
{
for(k=0;k<ncol;k++)
{
if(puzzle[j][k]==wordlist[i].word[0])
{
//left
if(wordlist[i].length<=(k+1))
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j][k-n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=LEFT;
}
}
//uppperleft
if(wordlist[i].length<=(k+1) && wordlist[i].length<=(j+1))
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j-n][k-n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=UPLEFT;
}
}
if(wordlist[i].length<=(j+1)) //up direction
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j-n][k])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=UP;
}
}
if(wordlist[i].length<=(ncol-k) && wordlist[i].length<=(j+1)) //upper right
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j-n][k+n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=UPRIGHT;
}
}
if(wordlist[i].length<=(ncol-k)) //right
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j][k+n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=RIGHT;
}
}
//bottom right
if(wordlist[i].length<=(ncol-k) && wordlist[i].length<=(nrow-j))
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j+n][k+n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=BOTTOMRIGHT;
}
}
if(wordlist[i].length<=(nrow-j))
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j+n][k])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=BOTTOM;
}
}
if(wordlist[i].length<=(nrow-j) && wordlist[i].length<= (k+1))
{
for(n=0;n<wordlist[i].length;n++)
if(wordlist[i].word[n]!=puzzle[j+n][k-n])break;
if(n==wordlist[i].length)
{
found=1;
wordlist[i].row=j;
wordlist[i].col=k;
wordlist[i].d=BOTTOMLEFT;
}
}
}
if(found==1)break;
}
if(found==1)break;
}
}
for(i=0;i<nwords;i++)
{
if(wordlist[i].row!=-1)
{
cout<<"\nFound "<<wordlist[i].word<<" at ("<<(wordlist[i].row+1)<<","<<(wordlist[i].col+1)<<")";
cout<<"\nDirection: "<<direction[wordlist[i].d];
cout<<"\nWordlength "<<wordlist[i].length<<"\n";
}
if((i+1)%5==0)
{
cout<<"\npress any key to continue";
cin.get();
}
}
return 0;
}