Pseudocode of this algorithm

Tag: algorithm Author: shiyishen Date: 2009-07-20

I'm making a game that involves a 4*4 grid. The user enters letters and the game has to highlight the letters and they have to be next to one another.

I have a sub that checks if the current letter is next to (or neighbouring) the last letter. Basically what needs to happen is :

for each letter: iterate through the grid and try every possibility if it is next to the previous letter, and the current letter matches the target letter

then it tries every possibility for each leter so it needs to iterate 16 ^ (number of letters)

if someone could lead me in the right direction, or give me a better algorithm id really appreciate it.

I'm using vb .net but pseudocode works too.

Thanks

Could you explain the point of the game more clearly?
I think an example grid and possible highlighting might clarify the intended behaviour. A question I have is whether (assuming I type ABC) the program needs to highlight all A's, then all A's and B's that are adjacent, then all A's that are adjacent to B's that are adjacent to C's. Is that correct?

Best Answer

If I get this right the grid could look like:

A B C D

B A D C

C C A B

D B C A

Then the user should type A B C D B A and so on to win?

One thing to consider is that if you remember all the positions where the first keystroke matched, lets say the user pressed A, then index 0,5,10,15 would be marked.

The next keypress is only valid if the previous key press was next to the current so insted of checking every item in the grid check 0+1, 5+1, 10+1 and 15+1 for the key press.

So if the user presses D, then you would see that index 1 is not a D index 6 is acutally a D so lets mark it, and so on.