使用文本文件进行单词搜索python

时间:2017-08-23 22:57:14

标签: python text

我想创建一个单词搜索生成器,但我发现它有点困难。我看过网上发现这个单词搜索生成器运行良好,但我想从文本文件中使用我自己的网格。有没有办法我可以编码并放置我的文本文件而不是随机选择字母。

##while True:
##    intext=input('Enter file name: ')
##    
##    if not intext=='grid.txt':
##        print("No such file was found! Enter file name.")
##        continue
##    else:
##
##        break
##grid= open(intext,'r')


import string
import random

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)

    print([x,y])

    for i in range(0,len(word)):
        grid[y + d[1]*i][x + d[0]*i] = word[i]
    return grid

grid = [[random.choice(string.ascii_uppercase) for i in range(0,width)] for j in range(0,height)]

for word in ["HELLO", "THERE", "AGAIN"]:
    grid = put_word(word, grid)

print("\n".join(map(lambda row: " ".join(row), grid))
grid.txt文件中的

是:

xmfycxvtljlqbbybkoumjqwbt
caubmeknbeydqmcnzyjpvrank
aqactivexnyvwdvcoshoyaohg
paghzkctudptjdphsztprhttl
sbsnakjwqbouftmgnjqbtlinu
tsewohvobdsduqjiffkyylodo
oukwwefroyamapmlrrpvdolop
cqkfxtlksjvtmtrsbycmqrrri
kfervlqidqaxaoanfqjlmcpjh
yoyywrbpfcjlfbcbbcoecspwl
twbxetyuyufvvmauawjmbwlqh
txokdexmdbtgvhpsvsqtmljdx
dcatenrehteoxqdgeueljtrrn
jarteqvtxejfsqddkbuhcysfq
hpdrowssapxtrxhpdxcdhicon

0 个答案:

没有答案