i'm doing horizontal right now. Fix my code please. Also i need help finding diagonal words.
Printable View
i'm doing horizontal right now. Fix my code please. Also i need help finding diagonal words.
It would be more useful if you post the code directly into your post rather than post a picture of it.
I think I'd adopt a different approach and mimic the same logic I'd use if I were solving it with pen and paper.
1. Scan the Grid looking for the first letter of the word I'm searching for.
2. When I find it I'd then scan in turn, to the right, left, above, below, diagonal left to right down, diagonal right to left down, diagonal right to left up, diagonal left to right up, for the next letter.
3. If I find the next letter I'd continue, in whatever direction I was going, looking for the next letter and so on until I've found all the letters required.
4. If at any stage I didn't find the letter I was loking for, I'd continue changing direction until either I find it or I've looked in all directions.
5. If I've looked in all possible directions and hadn't found a letter then I'd repeat from step (1) looking for the first letter again
You can use the length of the word you're looking for to avoid checking in directions that will go over the edges of the Grid. e.g. If I were looking for 'Flux' and I found an 'F' in Row 2, Col 2 there would be no point in looking in directions that took you to the left or above (since Flux has 4 letters and there are not 3 cells above or to the left of 2,2)
EDIT: A faster method might be to use InStr. e.g. start at 0,0 and copy everything to the right into a string then use InStr to see whether the word is in the string.If it isn't, then copy everything below into a string and use InStr again, if it isn't then copy the diagonal(s) and repeat until the word is found.
Continuing with Doogle's theme it may be more efficient to analyse the grid first, populating an array of strings for all vertical, horizontal and diagonal 'paths'.
i.e. for a 3x3 grid you'd have 3 horizontal paths, 3 vertical paths, 3 diagonal left-to-right/up-to-down paths and 3 diagonal left-to-right/down-to-up paths
e.g. Consider this grid
ABC
DEF
GHI
You'd have 'ABC','DEF','GHI','ADG','BEH','CFI','G','DH','AEI','BF','C','A','DB','GEC','HF','I'
That's all you need. If you have a function that reverses the string you are looking for, you can look it up in the above array...