在此功能中,程序将用户输入的四个单词放入列表中,并将其随机放入10x10矩阵中。问题有时是由于random.randiant
,来自水平/垂直/对角线的单词彼此重叠。
有办法避免这种情况发生吗?
def add(word, puzzle):
temp = random.randint(0, 2)
if temp == 0:
row = random.randint(0, 14)
column = random.randint(0, 12-len(word))
for i in range(0, len(word)):
puzzle[row][column+i] = word[i]
elif temp == 1:
column = random.randint(0, 14)
row = random.randint(0, 14-len(word))
for i in range(0, len(word)):
puzzle[row+i][column] = word[i]
else:
row = random.randint(0, 14-len(word))
for i in range(0, len(word)):
puzzle[row+i][row+i+1] = word[i]