简体   繁体   English

在Boggle板上查找单词的算法

[英]Algorithm to find word on Boggle board

I'm building a boggle game in vb .net. 我正在vb .net中建立一个炫目游戏。 Right now, my dices are as a 2d array (0,0 0,1 ) etc... 现在,我的骰子是二维数组(0,0 0,1)等...

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. 我想要做的是,当我输入单词时,它会使用button(x,y).doclick子高亮显示它在板上。 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. 现在,我的实现找到第一个字母,然后继续尝试每个字母,直到它满足8个转角条件(即与最后一个字母相邻),但这并不总是有效。 If there are say 2 "G"'s on the board and I want the bottom one, this will not work. 如果板上有2个“ G”,而我想要最下面的一个,那么这将不起作用。 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. 为了解决这个问题,我已经迷迷了近6个小时。 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. 给定字符串s的答案将是路径列表,其中路径是网格坐标的列表。 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. 恐怕我不知道如何编写VB代码。 Since you asked for pseudocode, here's some rough python-like pseudocode instead. 由于您要求使用伪代码,因此这里有一些类似python的粗糙伪代码。 I'm coding the boggle grid as a list of 16 items. 我将沼泽网格编码为16个项目的列表。 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]). neighbors(x)函数返回相邻位置的列表(边缘情况除外,[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. 例如,如果玩家输入“ go”,则(a)玩家输入“ g”,代码将调用firstletter(“ g”)并获取其中包含“ g”的位置列表“答案” 。 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". (b)玩家键入“ o”,代码调用addletter(answer,“ o”)并获得网格中“ go”的路径列表。 Again, highlight the first one. 同样,突出显示第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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