如何生成一个单词搜索网格,使用相同的字母启用单词

时间:2017-01-18 21:42:29

标签: python grid crossword

我试图用一个给定单词列表创建一个单词搜索网格。 我的问题是某些词语没有正确显示。

我已更新代码,请参阅下文

我已经尝试修复上一个错误,现在我收到一个超出范围的错误,我无法解决,如果有人可以帮助我会很感激

注1:我没有包含所有程序功能,如果需要,我将在稍后包含它们

程序接收如下文本文件:

9 9  
white  
black  
blue  
green  
pink  
yellow  
red  
grey  
purple  

其中文件中的前2个数字是网格的尺寸,其余的是要放在网格中的单词。

import random
import string
fi=input('Insert the entry file name(entry.txt): ')
fo=input('Insert the exit file name(.txt): ')
grid_size=[]
words=[]
matrix=[]

def read_file(storage):
    file=open(storage)

    n=file.readline()
    lista=n.split()
    lista=list(map(int,lista))  #sets the size of the grid
    for i in lista:
        grid_size.append(i)

    for line in file:
        line=line.replace("\n","")
        words.append(line)
    file.close()

def grid_generator(grid_size):
    n, p = grid_size
    for i in range(n):
        matriz.append([])
        for j in range(p):
            matriz[i].append(".")   

def sets_word_inside(grid_size, word, grid):
      n, p = grid_size
      word = random.choice([word,word[::-1]])  
                #horizontal,vertical,diagonal
      d = random.choice([[1,0],[0,1],[1,1]]) 

      xsize = n  if d[0] == 0 else n  - len(word)
      ysize = p if d[1] == 0 else p - len(word)

      x= random.randrange(0,xsize)
      y= random.randrange(0,ytsize)  #position

      for i, letter in enumerate(word):
           char = grid[y+d[1]*i][x+d[0]*i]   
           if char != " " and char != letter:
               # If it reaches an already filled space - restart the            process.
               # The second condition allow the words that cross with repeated words are created.

               return False
           grid[y+d[1]*i][x+d[0]*i] = letter[i]
      return True

现在代码的输出是这样的:

9  
white  
black  
blue  
green  
pink  
yellow  
red  
grey  
purple  
p w b i t y l d i  
p v w o l e e y t  
x g a x j r i m g  
q i c j b g j e x  
s s k g q l g r r  
p i n k i o u t r  
e l p r u p g e o  
l a b l s r p g y  
c o r y e u f r x  

1 个答案:

答案 0 :(得分:0)

我看到两个基本问题:你用新的单词覆盖现有的单词,然后你写出超出实际单词的结尾。

第一个问题是,在添加新单词之前,您没有费心去寻找先前的单词。 第二个问题是因为您无法从输入中删除空白区域。

我添加了一些简单的调试工具来跟踪它。首先,我将网格初始化为点而不是字母,所以我可以看到发生了什么:

<Center>
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=2><TR><TD>
<TABLE  CELLSPACING=0 CELLPADDING=2 BORDER=0>
<TR><TD BGCOLOR=3399FF ALIGN=CENTER><NOBR><FONT FACE="Arial" SIZE=+1><B>&nbsp;&nbsp; Search Results &nbsp;&nbsp;</B></FONT></NOBR></TD></TR>
<TR><TD><TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0>
    <Center>
    <table width="100%" cellpadding="5" cellspacing="0">

        <tr bgcolor=A9A9A9>

        <th>HEADER1</th>
        <th>HEADER2</th>
        <th>HEADER3</th>
        <th>HEADER4</th>
        <th>HEADER5</th>
        <th>HEADER6</th>
        <th>HEADER7</th>
        <th>HEADER8</th>
        <th>HEADER9</th>
        <th>HEADER10</th>
        <th>HEADER11</th>
        <th>HEADER12</th>
        <th>HEADER13</th>

        </tr>

            <tr >

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000> </td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000> </td>

            <tr>

            <tr bgcolor=C0C0C0>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000> </td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000> </td>

            <tr>

            <tr >

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>

                <td nowrap><font size= "-1" color=000000> </td>
                <td nowrap><font size= "-1" color=000000>**RESULTS**</td>
                <td nowrap><font size= "-1" color=000000> </td>

            <tr>
</TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
</Center>

然后,我在添加每个单词后打印矩阵:

for j in range(p):
    matrix[i].append(".")

然后我跑了它并得到了这个输出:

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

print "\nUpdated grid with\t", word, "\n",
for row in range(n):
    print " ".join(matrix[row])

return grid

你看到发生了什么:你写得太远了,你崩溃了现有的条目。

<强>解

我建议你按照我目前所做的做法:初始化为一些易于阅读的非字母。现在,在向网格中输入单词之前,请检查路径是否清晰。更好的是,如果相交的字母匹配,则允许在之前的单词中书写。

之后你已经放置了列表中的所有单词,然后填写网格的其余部分。