将单词的字母与矩阵中的现有字母匹配
Match a word's letters to existing letters in a matrix
我正在用 python 使用递归函数制作填字游戏风格的程序。第一个函数从列表中随机取词,并将它们垂直放置在方阵(可以是任意长度)中,默认项为“-”。第二个函数做同样的事情,但在将单词的字母与已经放置的垂直单词的字母匹配后水平放置单词。由于某种原因,字母匹配不起作用,功能正在用新单词的水平字母替换现有垂直单词的字母。
垂直函数看起来像这样并且工作正常:
def placeWordsVertically(puzzle, row, column): ##puzzle is the matrix, created in earlier helper function
if column >= len(puzzle):
return puzzle
else:
word = getAWord() ## helper function defined elsewhere which chooses a random word from a list
if len(word)<=len(puzzle):
for i in range(len(word)):
puzzle[i][column] = word[i]
column += 2
placeWordsVertically(puzzle, row, column)
else:
placeWordsVertically(puzzle, row, column)
这里是水平函数:
def placeWordsHorizontally(puzzle, row, column):
if row >= len(puzzle):
return puzzle
else:
word = getAWord()
## set length of word as variable 1
l = len(word)
#check if word fits puzzle length
if l <= len(puzzle):
##make sure there's a space ('-') at the end of horizontal words
if puzzle[row][l] == '-':
##BELOW IS THE PART NOT WORKING
for i in range(len(word)):
## existing letters in the row should either match the new word's letters or be '-'
if puzzle[row][i] == word[i] or '-':
puzzle[row][i] = word[i]
else:
placeWordsHorizontally(puzzle, row, column)
# increase row by 2
row += 2
placeWordsHorizontally(puzzle, row, column)
else:
placeWordsHorizontally(puzzle, row, column)
else:
placeWordsHorizontally(puzzle, row, column)
这是我从 运行 程序得到的结果:
t r a v e l e r s - u -
i - i - i - i - i - r -
s t o w i n g - r - g -
i - d - e - h - w - e -
e t h a n o l - o - - -
a - i - s - r - r - - -
s o m n a m b u l a r -
i - g - - - n - h - - -
b i r d e r s - y - - -
m - - - - - - - - - - -
d e s i r e s - - - - -
- - - - - - - - - - - -
竖排的字被覆盖了,乱七八糟。任何人都知道如何将水平单词的字母与现有的垂直单词的字母匹配吗?
问题是 puzzle[row][i] == word[i] or '-':
条件。这将始终 return 正确。
原因是or
运算符没有重复前面比较的左边值,它使用字符'-'的“真值”。
要让它工作,你需要这样写条件:
if puzzle[row][i] == word[i] or puzzle[row][i] == '-':
否则Python将其视为:
if (puzzle[row][i] == word[i]) or bool('-'):
bool('-') # is True
我正在用 python 使用递归函数制作填字游戏风格的程序。第一个函数从列表中随机取词,并将它们垂直放置在方阵(可以是任意长度)中,默认项为“-”。第二个函数做同样的事情,但在将单词的字母与已经放置的垂直单词的字母匹配后水平放置单词。由于某种原因,字母匹配不起作用,功能正在用新单词的水平字母替换现有垂直单词的字母。
垂直函数看起来像这样并且工作正常:
def placeWordsVertically(puzzle, row, column): ##puzzle is the matrix, created in earlier helper function
if column >= len(puzzle):
return puzzle
else:
word = getAWord() ## helper function defined elsewhere which chooses a random word from a list
if len(word)<=len(puzzle):
for i in range(len(word)):
puzzle[i][column] = word[i]
column += 2
placeWordsVertically(puzzle, row, column)
else:
placeWordsVertically(puzzle, row, column)
这里是水平函数:
def placeWordsHorizontally(puzzle, row, column):
if row >= len(puzzle):
return puzzle
else:
word = getAWord()
## set length of word as variable 1
l = len(word)
#check if word fits puzzle length
if l <= len(puzzle):
##make sure there's a space ('-') at the end of horizontal words
if puzzle[row][l] == '-':
##BELOW IS THE PART NOT WORKING
for i in range(len(word)):
## existing letters in the row should either match the new word's letters or be '-'
if puzzle[row][i] == word[i] or '-':
puzzle[row][i] = word[i]
else:
placeWordsHorizontally(puzzle, row, column)
# increase row by 2
row += 2
placeWordsHorizontally(puzzle, row, column)
else:
placeWordsHorizontally(puzzle, row, column)
else:
placeWordsHorizontally(puzzle, row, column)
这是我从 运行 程序得到的结果:
t r a v e l e r s - u -
i - i - i - i - i - r -
s t o w i n g - r - g -
i - d - e - h - w - e -
e t h a n o l - o - - -
a - i - s - r - r - - -
s o m n a m b u l a r -
i - g - - - n - h - - -
b i r d e r s - y - - -
m - - - - - - - - - - -
d e s i r e s - - - - -
- - - - - - - - - - - -
竖排的字被覆盖了,乱七八糟。任何人都知道如何将水平单词的字母与现有的垂直单词的字母匹配吗?
问题是 puzzle[row][i] == word[i] or '-':
条件。这将始终 return 正确。
原因是or
运算符没有重复前面比较的左边值,它使用字符'-'的“真值”。
要让它工作,你需要这样写条件:
if puzzle[row][i] == word[i] or puzzle[row][i] == '-':
否则Python将其视为:
if (puzzle[row][i] == word[i]) or bool('-'):
bool('-') # is True