简体   繁体   中英

Word Puzzle - How to fix it going out of path?

I'm hoping someone can point me in the right direction with this issue.

For my project in school, we need to make a "Word Puzzle" solver. So there is a .txt file with a "grid" of letters, along with actual words under this grid that the puzzle needs to look for and see if they exist in the puzzle.

example:
4 4
s e l f
w t r g
a o o v
g x q e
frog
swag
self

The 4 4 represents the puzzle will be 4 letters wide, and 4 letters high.

The problem I am running into is that if, for example, we are searching for "frog". My program finds the 'f', then moves on to the next character in the word 'r'. It finds the 'r', then moves onto the 'o'. This is where my problem is. The way my program is designed uses a bunch of if else statements to check if the letter above, below, left, right, upright, downright, upleft, or downleft of the current location contains the next letter we are looking for.

So what happens is that it will pick the wrong 'o', and then try to find the 'g' after moving to 'o's location, but it can't because it took the wrong path.

My question is this: does anyone have an idea on how to fix this issue? I don't want a direct coding answer if possible, since this is an academic assignment and I don't want to cheat, but if someone could lead me in the right direction, I would be ecstatic!

Thank you.

EDIT : Here is the code: http://pastebin.com/SJ4EePk7

their are 4 orientations in any your word could be found.

  1. Horizontal (4 rows)
  2. Vertical (4 rows)
  3. Diagonal 1 - topleft to bottomright (7 rows)
  4. Diagonal 2 - topright to bottomleft (7 rows)

Create program to get strings from all these orientations

s e l f  
w t r g  
a o o v  
g x q e  
  1. Horizontal : [ self , wtrg , aoov , gxqe ]
  2. Vertical : [ swag , etox , lroq , fgve ]
  3. Diagonal 1 : [ s , ew , lta , frog , gox , vq , e ]
  4. Diagonal 2 : [ f , lg , erv , stoe , woq , ax , g ]

Now to check if your strings are found in these strings you can use String.contains(...) function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM