如何在令人难以置信的游戏中递归检查答案
作为练习,我一直在尝试用 python 构建一个非 GUI 类型的游戏。到目前为止,用户可以输入板尺寸(4x4、5x5 等)。字母“数组”出现,然后用户可以输入他们认为有效的单词。
我想使用递归函数检查输入的单词是否有效。在小板上我的解决方案似乎工作得很好。然而,在较大的板上,具有相似开头和多个路径的单词不会被记录。我有一种感觉,这是因为如果当前路径在没有找到正确单词的情况下结束,我的函数的最后一部分就无法后退足够远。
这是我到目前为止所得到的:
def isAdjacent(word, wordLetter, possibleStarts, arrayDict, currWord, start):
#'word' is the word entered. 'wordLetter' is the current letter being looked for.
#'possibleStarts' is a list of tuples of the possible starting locations and subsequent positions of each found letter.
#'arrayDict' is a dictionary associating each position ((0,1), etc) with a game letter.
#'currWord' is used to check whether or not a word has been found.
#'start' is the tuple in possibleStarts that should be used.
if currWord == word:
return 1
x = possibleStarts[start][0]
y = possibleStarts[start][1]
arrayDict[x,y] = 0
optionsList = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
newStarts = []
count = 0
count2 = 0
for option in optionsList:
count += 1
if option in arrayDict:
if arrayDict[option] == word[wordLetter]:
if count2 < 1:
currWord += word[wordLetter]
arrayDict[option] = 0
count2 += 1
newStarts.append(option)
if count == 8 and newStarts:
return isAdjacent(word, wordLetter + 1, newStarts, arrayDict, currWord, start)
try:
if currWord != word:
if wordLetter > 2:
return isAdjacent(word, wordLetter - 1, possibleStarts, arrayDict, currWord[:-1], start - 1)
else:
return isAdjacent(word, wordLetter, possibleStarts, arrayDict, currWord, start - 1)
except:
pass
我相信至少部分问题在于函数末尾的 try 块。如果单词不太长或者没有太多可能性,它就有效。例如,尝试在下面找到“raws”,即使它在那里,也是行不通的:
W T S V
A X A H
S R T S
A B A W
我确信这可以通过一个相当简单的递归函数来完成,但几个小时后,我迷失了。 哦,我宁愿不预先生成所有可能的单词。其目标是使用递归来查找输入的单词。
非常感谢任何帮助!
As an exercise, I have been trying to build a non-GUI boggle type game in python. So far, the user is able to enter a board size (4x4,5x5,etc). The 'array' of letters appears and then the user may type in a word that they think is a valid option.
I wanted to check if the entered word was valid by using a recursive function. On small boards my solution seems to work fine. On larger boards however, words with similar starts and multiple paths don't register. I have a feeling that it is because the last part of my function is not able to step back far enough if the current path ends without finding a correct word.
Here is what I have so far:
def isAdjacent(word, wordLetter, possibleStarts, arrayDict, currWord, start):
#'word' is the word entered. 'wordLetter' is the current letter being looked for.
#'possibleStarts' is a list of tuples of the possible starting locations and subsequent positions of each found letter.
#'arrayDict' is a dictionary associating each position ((0,1), etc) with a game letter.
#'currWord' is used to check whether or not a word has been found.
#'start' is the tuple in possibleStarts that should be used.
if currWord == word:
return 1
x = possibleStarts[start][0]
y = possibleStarts[start][1]
arrayDict[x,y] = 0
optionsList = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
newStarts = []
count = 0
count2 = 0
for option in optionsList:
count += 1
if option in arrayDict:
if arrayDict[option] == word[wordLetter]:
if count2 < 1:
currWord += word[wordLetter]
arrayDict[option] = 0
count2 += 1
newStarts.append(option)
if count == 8 and newStarts:
return isAdjacent(word, wordLetter + 1, newStarts, arrayDict, currWord, start)
try:
if currWord != word:
if wordLetter > 2:
return isAdjacent(word, wordLetter - 1, possibleStarts, arrayDict, currWord[:-1], start - 1)
else:
return isAdjacent(word, wordLetter, possibleStarts, arrayDict, currWord, start - 1)
except:
pass
I believe that at least part of the problem lies in the try block at the end of the function. It works if the word is not too long or if there are not too many possibilites. For example, attempting to find 'raws' in the following, will not work, even though it is there:
W T S V
A X A H
S R T S
A B A W
I am certain that this can be done with a rather simple recursive function, but after many hours, I am lost.
Oh, I would rather not generate all of the possible words beforehand. The goal of this was to use recursion to find an entered word.
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的练习,我已经尝试过了。我发布了下面的代码,因此请将此视为剧透警报。对于一般提示:
就是这样。我对 Boggle 的完整规则有点生疏,而且我不完全确定你一直在做什么,但这就是我的想法:
Interesting exercise, I had a crack at it. I post the code below so consider this a spoiler alert. For general tips:
That's about it. I'm a bit rusty on the complete rules of Boggle and I'm not completely sure what you are doing the entire time, but this what I've come up: