The image shows the output. You can clearly see how the letters are not perfectly aligned. 我们正在尝试为学校项目制作游戏,并选择了Word Search。我们研究并找到了一段编码,帮助我们制作网格以及将我们的文字放入网格中。但我们现在面临的问题是网格没有正确对齐。 (即第一行的第3个字母不完全高于第二行的第三个字母。)。这使得很难找到这个词。但遗憾的是,我们还没有研究如何使用random.choice
,似乎无法理解如何正确对齐字母。所有的想法和意见都表示赞赏。
import string
import random
print '''Welcome to WORD SEARCH. This game has five stages. You will have a crossword in each stage with one element hidden in each puzzle.
However, do remember that the game is over once you make a mistake. So, think carefully and play.
Good luck!!!!'''
width = 12
height = 12
def put_word(word,grid):
word = random.choice([word,word[::-1]])
d = random.choice([[1,0],[0,1],[1,1]])
xsize = width if d[0] == 0 else width - len(word)
ysize = height if d[1] == 0 else height - len(word)
x = random.randrange(0,xsize)
y = random.randrange(0,ysize)
for i in range(0,len(word)):
grid[y + d[1]*i][x + d[0]*i] = word[i]
return grid
谢谢
答案 0 :(得分:2)
似乎对我打印很好......也许你应该在你的终端/无论你打印出网格的地方使用Monospaced字体。
grid = [['_' for _ in range(width)] for _ in range(height)]
def print_grid():
global grid
for row in grid:
print " ".join(row)
put_word("hello", grid)
put_word("world", grid)
print_grid()
你可能想要修复这个算法,因为单词是重叠的,你看我添加了"hello"
,但"herlo"
代替...
示例输出
_ _ _ h _ _ _ _ _ _ _ _
_ _ _ _ e _ _ _ _ _ _ _
_ _ _ d l r o w _ _ _ _
_ _ _ _ _ _ l _ _ _ _ _
_ _ _ _ _ _ _ o _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _