我正在尝试为学校完成这个boggle游戏Python挑战,我创建了一个绘制板功能,创建一个16个随机字母的板。如何使用def函数要求用户输入一个单词然后对其进行评分,具体取决于单词的长度?一旦完成,游戏应该正常工作。任何帮助将不胜感激:)
import random
def loadwords ():
print "Loading word list from file.. "
wordList = []
inFile = open ('words.txt','r')
for line in inFile:
wordList.append(line.strip().lower())
#inFile : locates file in the folder, opens it
#wordlist: list of the words (strings)
print " " , len(wordList), "words loaded"
inFile.close()
return wordList
def spellCheck (word, wordList):
if (word in wordList) == True:
return True
else:
return False
def drawBoard (randomLetters):
'''Takes a randomList of 16 characters
Prints out a 4 x 4 grid'''
print " %s %s %s %s " %(randomLetters [0], randomLetters [1], randomLetters [2], randomLetters [3])
print " %s %s %s %s " %(randomLetters [4], randomLetters [5], randomLetters [6], randomLetters [7])
print " %s %s %s %s " %(randomLetters [8], randomLetters [9], randomLetters [10], randomLetters [11])
print " %s %s %s %s " %(randomLetters [12], randomLetters [13], randomLetters [14], randomLetters [15])
def wordinput ():
#asks user to input the longest word they can from grid
wordinput = raw_input ("Enter a word made up of the letters in the 4x4 table")
for letters in wordinput:
letters == randomLetters
def randomLetters ():
letters = []
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','v','w','x','y','z']
for i in range (0,16,1):
letters.append(random.choice(alphabet))
return letters
dictionary = loadwords()
letterList = randomLetters()
drawBoard(letterList)
wordinput(randomLetters)
答案 0 :(得分:2)
raw_input
(在Python 2中)或input
(在Python 3中)允许读取字符串。
然后你需要检查字母表中是否包含所有字母......
def wordInLetter(word, letters):
for i in range(word):
if word[i] not in letters:
return False
return True
或更短:
def wordInLetter(word, letters):
return all(letter in letters for letter in word)
但是等等,你不想让几次使用一个字母!
让我们使用Counter
来跟踪每个字母在信件袋中的次数并从中开始工作:
from collections import Counter
def wordInLetter(word, letters):
available = Counter(letters)
for letter in word:
if available[letter] == 0:
return False
available[letter] -= 1
return True
这似乎很好用。我希望这就是你所需要的。
In [3]: wordInLetter('kos','akos')
Out[3]: True
In [4]: wordInLetter('kos','ako')
Out[4]: False
In [5]: wordInLetter('koss','akos')
Out[5]: False
修改
因此,我们不仅对word
中letters
是否可以合并感兴趣,而且还想知道是否可以通过匹配相邻字母来完成。所以另一个尝试:
import math
def wordInLetterSearch(word, letters, startX, startY):
# Assume: letters is a list of X letters, where X is a k*k square
k = int(len(letters) ** 0.5)
if len(word) == 0:
return True
def letter(x, y):
return letters[x + y*k]
def adjacent(x, y):
if x > 0:
yield x-1, y
if x < k-1:
yield x+1, y
if y > 0:
yield x, y-1
if y < k-1:
yield x, y+1
# try to move in all 4 directions
return any(letter(x2, y2) == word[0] and wordInLetterSearch(word[1:], letters, x2, y2)
for x2, y2 in adjacent(startX, startY))
def wordInLetter(word, letters):
k = int(len(letters) ** 0.5)
def coords(i):
return i%k, i/k
# look for a starting point
return any(letter == word[0]
and wordInLetterSearch(word[1:], letters,
coords(i)[0], coords(i)[1])
for i, letter in enumerate(letters)) coords(i)[1])
for i, letter in enumerate(letters))
这有点复杂。基本上它是一个两步搜索:首先我们寻找一个起始点(letters
内的位置,其中字母与单词的第一个字符匹配),然后我们在可能的情况下,以递归的方式将蛇形移动到相邻的字段。
完全匹配Boggle规则需要很少的修改:
adjacent
以允许对角线(简单),wordInLetterSearch
,使用set
)。我会将这些作为练习。
答案 1 :(得分:1)
这是另一种变体。它应该更容易阅读,但想法是一样的:简单backtracking。有时候,更容易在调用者端中止迭代(不执行下一步),有时在调用端(在每次迭代时,检查前置条件并在无效时中止)。
def wordInBoardIter(letters, word, x, y):
n = int(len(letters)**0.5)
if word == "": return True # empty - all letters are found
if x<0 or y<0 or x>=n or y>= n: return False #outside of board
if letters[x+y*n] != word[0]: return False # we are looking at the wrong position
# one step further:
return any(wordInBoardIter(letters, word[1:], x+dx,y+dy) for dx,dy in [(1,0), (0,1), (-1,0), (0,-1)])
def wordInBoard(letters, word):
n = int(len(letters)**0.5)
return any(any(wordInBoardIter(letters, word, x,y) for x in range(n)) for y in range(n))
if __name__ == '__main__':
letters = ['a', 'h', 'e', 'l',
'x', 'd', 'l', 'l',
'y', 'v', 'r', 'o',
'z', 'w', 'o', 'w']
print "hello : %s" % ("found" if wordInBoard(letters, "hello") else "not found")
print "helloworld: %s" % ("found" if wordInBoard(letters, "helloworld") else "not found")
print "foobar : %s" % ("found" if wordInBoard(letters, "foobar") else "not found")
此版本中有相同的练习(对角线拼贴,不允许重复使用相同的字母两次)。
答案 2 :(得分:0)
我没有玩过很多关于myslef的问题,但是你可以用来做这个的解决方案就是取用户输入的单词并使用len()
命令返回单词的长度。然后取这个长度并得分。这是一个基本的例子(修改它以适应游戏规则):
def wordinput ():
#asks user to input the longest word they can from grid
wordinput = raw_input ("Enter a word made up of the letters in the 4x4 table")
for letters in wordinput:
letters == randomLetters
scoreWord(wordInput)
def scoreWord(word)
#finds the amount of characters in the word
wordLength = len(word)
#multiplies the maunt of letters in the word by two to get the score
#(not sure how boggle scoring works)
score = wordLength * 2