944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3403
  • C++ RSS
May 12th, 2006
0

print out the solve word puzzle

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. using namespace std;
  8. #define MAXLENGTH 20
  9. #define WORDLENGTH 20
  10. enum dir { LEFT=0,UPLEFT,UP,UPRIGHT,RIGHT,BOTTOMRIGHT,BOTTOM,BOTTOMLEFT};
  11. enum menu { LOAD=0,SOLVE,CREATE,QUIT };
  12. struct WORD
  13. {
  14. char word[WORDLENGTH];
  15. int length;
  16. int row;
  17. int col;
  18. dir d;
  19. };
  20. typedef struct WORD word;
  21.  
  22. word wordlist[100];
  23. char puzzle[MAXLENGTH][MAXLENGTH];
  24. int nrow,ncol,nwords;
  25. char direction[8][15]={"LEFT","UPPERLEFT","UP","UPPERRIGHT","RIGHT","BOTTOMRIGHT","BOTTOM","BOTTOMLEFT"};
  26. int loaded;
  27. enum menu readMenu();
  28. int loadPuzzle();
  29. int solvePuzzle();
  30. int createPuzzle();
  31. void printUnsolvedPuzzle();
  32. int main()
  33. {
  34. menu menuItem;
  35. loaded=-1;
  36. while(1)
  37. {
  38. menuItem=readMenu();
  39. if(menuItem==LOAD)
  40. {
  41. loaded=loadPuzzle();
  42. }
  43. else if(menuItem==SOLVE)
  44. {
  45. solvePuzzle();
  46. }
  47. else if(menuItem==CREATE)
  48. {
  49. createPuzzle();
  50. }
  51. else if(menuItem==QUIT)
  52. {
  53. cout<<"\nGood bye ! demo version by inadilemma";
  54. cin.get();
  55. return 0;
  56. }
  57. }
  58. }
  59. enum menu readMenu()
  60. {
  61. while(1)
  62. {
  63. fflush(stdin);
  64. cout<<"\n\t\t**********************";
  65. cout<<"\n\t\t* *";
  66. cout<<"\n\t\t* FIND A WORD *";
  67. cout<<"\n\t\t* *";
  68. cout<<"\n\t\t**********************\n";
  69. cout<<"\n1: Load Puzzle.\n2: Solve Puzzle.\n3: Create Puzzle.\n4: Quit.";
  70. cout<<"\nEnter your choice: ";
  71. char ch;
  72. cin>>ch;
  73. if(ch=='1')return LOAD;
  74. if(ch=='2')return SOLVE;
  75. if(ch=='3')return CREATE;
  76. if(ch=='4')return QUIT;
  77. cout<<"\nInvalid input !";
  78. }
  79. }
  80. int loadPuzzle()
  81. {
  82. char filename[256];
  83. int i;
  84. cout<<"\nEnter filename : ";
  85. cin>>filename;
  86. ifstream fin(filename);
  87. if(fin==NULL)
  88. {
  89. cout<<"\nInvalid filename \nPress any key...";
  90. cin.get();
  91. return -1;
  92. }
  93. fin>>nrow>>ncol;
  94. nwords=0;
  95. for(i=0;i<nrow;i++)
  96. {
  97. fin>>puzzle[i];
  98. if(strlen(puzzle[i])!=ncol)
  99. {
  100. cout<<"\nInvalid puzzle \nPress any key...";
  101. cin.get();
  102. return -1;
  103. }
  104. }
  105. while(1)
  106. {
  107. fin>>wordlist[nwords].word;
  108. wordlist[nwords].length=strlen(wordlist[nwords].word);
  109. wordlist[nwords].row=-1;
  110. wordlist[nwords].col=-1;
  111. nwords++;
  112. if(fin.eof())break;
  113. }
  114. cout <<"\nPuzzle: "<< filename <<"\n\n";
  115. printUnsolvedPuzzle();
  116. fin.close();
  117. return 0;
  118. }
  119. void printUnsolvedPuzzle()
  120. {
  121. int i,j;
  122. for(i=0;i<nrow;i++)
  123. {
  124. for(j=0;j<ncol;j++)
  125. {
  126. cout<<"\t"<<puzzle[i][j] <<" ";
  127. }
  128. cout<<"\n";
  129. }
  130.  
  131. cout<<"\n\nWords :\n--------";
  132. for(i=0;i<nwords;i++)
  133. {
  134. if(i%4==0)cout<<"\n";
  135. cout<<wordlist[i].word<<"\t\t\t";
  136. }
  137. cout<<"\n";
  138. cin.get();
  139. }
  140. int createPuzzle()
  141. {
  142. cout<<"\nPuzzle created";
  143. cout<<"\nPress any key to continue...";
  144. cin.get();
  145. return 0;
  146. }
  147. int solvePuzzle()
  148. {
  149. if(loaded!=0)
  150. {
  151. cout<<"\nNo puzzle loaded";
  152. cout<<"\nPress any key to continue...";
  153. cin.get();
  154. return -1;
  155. }
  156. int i,j,k,n,found=0;
  157. for(i=0;i<nwords;i++)
  158. {
  159. found=0;
  160. for(j=0;j<nrow;j++)
  161. {
  162. for(k=0;k<ncol;k++)
  163. {
  164. if(puzzle[j][k]==wordlist[i].word[0])
  165. {
  166. //left
  167. if(wordlist[i].length<=(k+1))
  168. {
  169. for(n=0;n<wordlist[i].length;n++)
  170. if(wordlist[i].word[n]!=puzzle[j][k-n])break;
  171. if(n==wordlist[i].length)
  172. {
  173. found=1;
  174. wordlist[i].row=j;
  175. wordlist[i].col=k;
  176. wordlist[i].d=LEFT;
  177. }
  178. }
  179. //uppperleft
  180. if(wordlist[i].length<=(k+1) && wordlist[i].length<=(j+1))
  181. {
  182. for(n=0;n<wordlist[i].length;n++)
  183. if(wordlist[i].word[n]!=puzzle[j-n][k-n])break;
  184. if(n==wordlist[i].length)
  185. {
  186. found=1;
  187. wordlist[i].row=j;
  188. wordlist[i].col=k;
  189. wordlist[i].d=UPLEFT;
  190. }
  191. }
  192. if(wordlist[i].length<=(j+1)) //up direction
  193. {
  194. for(n=0;n<wordlist[i].length;n++)
  195. if(wordlist[i].word[n]!=puzzle[j-n][k])break;
  196. if(n==wordlist[i].length)
  197. {
  198. found=1;
  199. wordlist[i].row=j;
  200. wordlist[i].col=k;
  201. wordlist[i].d=UP;
  202. }
  203. }
  204. if(wordlist[i].length<=(ncol-k) && wordlist[i].length<=(j+1)) //upper right
  205. {
  206. for(n=0;n<wordlist[i].length;n++)
  207. if(wordlist[i].word[n]!=puzzle[j-n][k+n])break;
  208. if(n==wordlist[i].length)
  209. {
  210. found=1;
  211. wordlist[i].row=j;
  212. wordlist[i].col=k;
  213. wordlist[i].d=UPRIGHT;
  214. }
  215. }
  216. if(wordlist[i].length<=(ncol-k)) //right
  217. {
  218. for(n=0;n<wordlist[i].length;n++)
  219. if(wordlist[i].word[n]!=puzzle[j][k+n])break;
  220. if(n==wordlist[i].length)
  221. {
  222. found=1;
  223. wordlist[i].row=j;
  224. wordlist[i].col=k;
  225. wordlist[i].d=RIGHT;
  226. }
  227. }
  228. //bottom right
  229. if(wordlist[i].length<=(ncol-k) && wordlist[i].length<=(nrow-j))
  230. {
  231. for(n=0;n<wordlist[i].length;n++)
  232. if(wordlist[i].word[n]!=puzzle[j+n][k+n])break;
  233. if(n==wordlist[i].length)
  234. {
  235. found=1;
  236. wordlist[i].row=j;
  237. wordlist[i].col=k;
  238. wordlist[i].d=BOTTOMRIGHT;
  239. }
  240. }
  241. if(wordlist[i].length<=(nrow-j))
  242. {
  243. for(n=0;n<wordlist[i].length;n++)
  244. if(wordlist[i].word[n]!=puzzle[j+n][k])break;
  245. if(n==wordlist[i].length)
  246. {
  247. found=1;
  248. wordlist[i].row=j;
  249. wordlist[i].col=k;
  250. wordlist[i].d=BOTTOM;
  251. }
  252. }
  253. if(wordlist[i].length<=(nrow-j) && wordlist[i].length<= (k+1))
  254. {
  255. for(n=0;n<wordlist[i].length;n++)
  256. if(wordlist[i].word[n]!=puzzle[j+n][k-n])break;
  257. if(n==wordlist[i].length)
  258. {
  259. found=1;
  260. wordlist[i].row=j;
  261. wordlist[i].col=k;
  262. wordlist[i].d=BOTTOMLEFT;
  263. }
  264. }
  265. }
  266. if(found==1)break;
  267. }
  268. if(found==1)break;
  269. }
  270. }
  271. for(i=0;i<nwords;i++)
  272. {
  273. if(wordlist[i].row!=-1)
  274. {
  275. cout<<"\nFound "<<wordlist[i].word<<" at ("<<(wordlist[i].row+1)<<","<<(wordlist[i].col+1)<<")";
  276. cout<<"\nDirection: "<<direction[wordlist[i].d];
  277. cout<<"\nWordlength "<<wordlist[i].length<<"\n";
  278. }
  279. if((i+1)%5==0)
  280. {
  281. cout<<"\npress any key to continue";
  282. cin.get();
  283. }
  284. }
  285. return 0;
  286. }



read in the puzzle.txt file

10
10
nxchtrtvrv
oakbeyqhlf
itmtpnbket
tpuedulcni
crnksdziju
nfehupiclk
uiufloathi
fqastructr
gfbmkhriec
xspiiifdef
;enum
float
function
include
int
main
namespace
return
struct
type

I search the word where is inside the word puzzle, put I don't know hot to output the puzzle again and only show the find word into upper case. as below

*******************************
* Find a Word * *******************************
1. Load puzzle
2. Solve puzzle
3. Create puzzle
4. Quit

choice: 2

Solved Puzzle: puzzle.txt

N . . . . R T . . .
O A . . E Y . . . .
I . M T P . . . . T
T . U E D U L C N I
C R N . S . . I . .
N . E . . P . . . .
U I . F L O A T . .
F . A S T R U C T .
. . . M . . . . E .
. . . . . . . . . .

Words:
enum float function include
int main namespace return
struct type
Last edited by cscgal; May 12th, 2006 at 9:24 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maxilt is offline Offline
4 posts
since Apr 2006
May 12th, 2006
0

Re: print out the solve word puzzle

You have included the C++ string library in your code, so why don't you actually use it? C-Style strings (char[] and/or char* ) are considered 'bad' in C++ - They are just making your life harder, and your code less readable.

also, this line is bad (even in C, I believe this is a bad idea)
C++ Syntax (Toggle Plain Text)
  1. fflush(stdin);
instead, use std::cin.ignore(); to ignore the next character (probably just a newline char), or std::cin.ignore(INT_MAX, '\n'); to ignore all characters up to a newline.

I'm not totally clear what you want you mean by "output the puzzle again" output the puzzle after what?

Use std::toupper() from the <cctype> library to put a single character in uppercase.

To perform toupper on a string, you can either loop through each element of the string, or you can use the STL algorithm std::transform()

another issue making your code less readable - you've got alot of repeated code in your solvepuzzle() function - maybe you could break that down into smaller sub-functions.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 13th, 2006
0

Re: print out the solve word puzzle

C++ Syntax (Toggle Plain Text)
  1. int solvePuzzle()
  2. {
  3. if ( loaded!=0 )
  4. {
  5. cout<<"\nNo puzzle loaded";
  6. cout<<"\nPress any key to continue...";
  7. cin.get();
  8. return -1;
  9. }
  10. int i,j,k,n,found=0;
  11. for ( i=0;i<nwords;i++ )
  12. {
  13. found=0;
  14. for ( j=0;j<nrow;j++ )
  15. {
  16. for ( k=0;k<ncol;k++ )
  17. {
  18. if ( puzzle[j][k]==wordlist[i].word[0] )
  19. {
  20. //left
  21. if ( wordlist[i].length<=(k+1) )
  22. {
  23. for ( n=0;n<wordlist[i].length;n++ )
  24. if ( wordlist[i].word[n]!=puzzle[j][k-n] )break;
  25. if ( n==wordlist[i].length )
  26. {
  27. found=1;
  28. wordlist[i].row=j;
  29. wordlist[i].col=k;
  30. wordlist[i].d=LEFT;
  31. }
  32. }
  33. //uppperleft
  34. if ( wordlist[i].length<=(k+1) && wordlist[i].length<=(j+1) )
  35. {
  36. for ( n=0;n<wordlist[i].length;n++ )
  37. if ( wordlist[i].word[n]!=puzzle[j-n][k-n] )break;
  38. if ( n==wordlist[i].length )
  39. {
  40. found=1;
  41. wordlist[i].row=j;
  42. wordlist[i].col=k;
  43. wordlist[i].d=UPLEFT;
  44. }
  45. }
  46. if ( wordlist[i].length<=(j+1) ) //up direction
  47. {
  48. for ( n=0;n<wordlist[i].length;n++ )
  49. if ( wordlist[i].word[n]!=puzzle[j-n][k] )break;
  50. if ( n==wordlist[i].length )
  51. {
  52. found=1;
  53. wordlist[i].row=j;
  54. wordlist[i].col=k;
  55. wordlist[i].d=UP;
  56. }
  57. }
  58. if ( wordlist[i].length<=(ncol-k) && wordlist[i].length<=(j+1) ) //upper right
  59. {
  60. for ( n=0;n<wordlist[i].length;n++ )
  61. if ( wordlist[i].word[n]!=puzzle[j-n][k+n] )break;
  62. if ( n==wordlist[i].length )
  63. {
  64. found=1;
  65. wordlist[i].row=j;
  66. wordlist[i].col=k;
  67. wordlist[i].d=UPRIGHT;
  68. }
  69. }
  70. if ( wordlist[i].length<=(ncol-k) ) //right
  71. {
  72. for ( n=0;n<wordlist[i].length;n++ )
  73. if ( wordlist[i].word[n]!=puzzle[j][k+n] )break;
  74. if ( n==wordlist[i].length )
  75. {
  76. found=1;
  77. wordlist[i].row=j;
  78. wordlist[i].col=k;
  79. wordlist[i].d=RIGHT;
  80. }
  81. }
  82. //bottom right
  83. if ( wordlist[i].length<=(ncol-k) && wordlist[i].length<=(nrow-j) )
  84. {
  85. for ( n=0;n<wordlist[i].length;n++ )
  86. if ( wordlist[i].word[n]!=puzzle[j+n][k+n] )break;
  87. if ( n==wordlist[i].length )
  88. {
  89. found=1;
  90. wordlist[i].row=j;
  91. wordlist[i].col=k;
  92. wordlist[i].d=BOTTOMRIGHT;
  93. }
  94. }
  95. if ( wordlist[i].length<=(nrow-j) )
  96. {
  97. for ( n=0;n<wordlist[i].length;n++ )
  98. if ( wordlist[i].word[n]!=puzzle[j+n][k] )break;
  99. if ( n==wordlist[i].length )
  100. {
  101. found=1;
  102. wordlist[i].row=j;
  103. wordlist[i].col=k;
  104. wordlist[i].d=BOTTOM;
  105. }
  106. }
  107. if ( wordlist[i].length<=(nrow-j) && wordlist[i].length<= (k+1) )
  108. {
  109. for ( n=0;n<wordlist[i].length;n++ )
  110. if ( wordlist[i].word[n]!=puzzle[j+n][k-n] )break;
  111. if ( n==wordlist[i].length )
  112. {
  113. found=1;
  114. wordlist[i].row=j;
  115. wordlist[i].col=k;
  116. wordlist[i].d=BOTTOMLEFT;
  117. }
  118. }
  119. }
  120. if ( found==1 )break;
  121. }
  122. if ( found==1 )break;
  123. }
  124. }
  125. for ( i=0;i<nwords;i++ )
  126. {
  127. if ( wordlist[i].row!=-1 )
  128. {
  129. cout<<"\nFound "<<wordlist[i].word<<" at ("<<(wordlist[i].row+1)<<","<<(wordlist[i].col+1)<<")";
  130. cout<<"\nDirection: "<<direction[wordlist[i].d];
  131. cout<<"\nWordlength "<<wordlist[i].length<<"\n";
  132. }
  133. if ( (i+1)%5==0 )
  134. {
  135. cout<<"\npress any key to continue";
  136. cin.get();
  137. }
  138. }
  139. return 0;
  140. }


read in the puzzle.txt file


I not allow to using STL and String in this program. I oonly can using basic c++.

and I want to output the solve puzzle as look like below output

Before solve

**********************
* *
* FIND A WORD *
* *
**********************
1: Load Puzzle.
2: Solve Puzzle.
3: Create Puzzle.
4: Quit.
Enter your choice: 1
Enter file to load: puzzle.txt
Puzzle: puzzle.txt
n x c h t r t v r v
o a k b e y q h l f
i t m t p n b k e t
t p u e d u l c n i
c r n k s d z i j u
n f e h u p i c l k
u i u f l o a t h i
f q a s t r u c t r
g f b m k h r i e c
x s p i i i f d e f

Words :
--------
enum float function include
int main namespace return
struct type


After solve
*******************************
* Find a Word * *******************************
1. Load puzzle
2. Solve puzzle
3. Create puzzle
4. Quit

choice: 2

Solved Puzzle: puzzle.txt

N . . . . R T . . .
O A . . E Y . . . .
I . M T P . . . . T
T . U E D U L C N I
C R N . S . . I . .
N . E . . P . . . .
U I . F L O A T . .
F . A S T R U C T .
. . . M . . . . E .
. . . . . . . . . .

Words:
enum float function include
int main namespace return
struct type
Last edited by Dave Sinkula; May 13th, 2006 at 10:19 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maxilt is offline Offline
4 posts
since Apr 2006
May 13th, 2006
0

Re: print out the solve word puzzle

I am not sure I understand you.
But if you want to replace found words in the puzzle with dots,
here goes :

first change :
C++ Syntax (Toggle Plain Text)
  1. char puzzle[MAXLENGTH][MAXLENGTH];

to
C++ Syntax (Toggle Plain Text)
  1. typedef struct
  2. {
  3. char ch;
  4. int isdot;
  5. }PUZZLE_S ;
  6. PUZZLE_S puzzle[MAXLENGTH][MAXLENGTH];


while reading the puzzle from the file, set isdot = 0.
C++ Syntax (Toggle Plain Text)
  1. for(i=0;i<nrow;i++)
  2. for ( j = 0 ; j < ncol ; ++j )
  3. {
  4. fin >> puzzle[i][j].ch;
  5. puzzle[i][j].isdot = 0;
  6. }

Now every time you find a word in the puzzle, set isdot to one.
And leave the filed ch unchanged. Because it might serve for
anthoer word.


Last print the puzzle with a function like this
C++ Syntax (Toggle Plain Text)
  1. int print_ch(PUZLE_S *puzle)
  2. {
  3. if ( puzle->isdot)
  4. return('.');
  5. else if (puzle->ch >= 'a' && puzle->ch <= 'z' )
  6. return(puzle->ch - 'a' + 'A');
  7. return(puzle->ch);
  8. }
Reputation Points: 12
Solved Threads: 0
Light Poster
dude543 is offline Offline
26 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sequencer
Next Thread in C++ Forum Timeline: best compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC