简体   繁体   中英

Algorithm to find word on Boggle board

I'm building a boggle game in vb .net. Right now, my dices are as a 2d array (0,0 0,1 ) etc...

What I want it to do is, as I'm typing the word, that it highlights it on the board using the button(x,y).doclick sub which highlights it. Right now my implementation finds the first letter, then keeps trying each letter until it meets the 8 corner condition (ie it is neighbored to the last one) but this does not always work. If there are say 2 "G"'s on the board and I want the bottom one, this will not work. Can somebody give me an example of psuedocode of what needs to happen. I've been stumped for almost 6 hours trying to figure this out. Thanks

If I understand correctly, given a string you want to highlight one path through the dice that matches the string. Sometimes there are several possible choices, so adding a letter may completely change what is highlighted. It may be a good approach here to keep results from the previous substring, so we don't have to start over. Then a reasonable thing to do would be to compute all possible paths.

The answer for a given string s would be a list of paths, where a path is a list of grid coordinates. Each path is something you could reasonably highlight, so you just highlight the first one. When adding a letter to the string, you find paths you can expand and remove the ones you can't expand.

I'm afraid I don't know how to write vb code. Since you asked for pseudocode, here's some rough python-like pseudocode instead. I'm coding the boggle grid as a list of 16 items. The neighbors(x) function returns a list of the neighboring positions (except for edge cases that's going to be [x-1, x+1, x-4, x+4]).

def firstLetter(typed):
  answer = []
  for pos in range(16): if grid[pos]==typed: answer += [pos]
  return answer

def addletter(partialanswer, typed):
  answer2 = []
  for partial in partialanswer:
      for neighbor in neighbors(partial[-1]):
          if grid[neighbor]==typed: 
             # partial+[neighbor] is a list. answer2 is a list of such lists.
             answer2 += partial + [neighbor]
  return answer2

If the player types "go", for example, then (a) player types "g", code calls firstletter("g") and gets a list "answer" of the positions in the grid that have a "g" in them. Highlight, say, the first one. (b) player types "o", code calls addletter(answer, "o") and gets a list of the paths in the grid that say "go". Again, highlight the first one.

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