【问题标题】:How to generate a word search grid that enables words using the same letter如何生成允许单词使用相同字母的单词搜索网格
【发布时间】:2017-06-03 10:55:57
【问题描述】:

我正在尝试创建一个包含给定单词列表的单词搜索网格。 我的问题是有些词显示不正确。

我已经更新了代码,见下文

我已尝试修复以前的错误,但现在我遇到了无法修复的超出范围错误,如果有人可以提供帮助,我将不胜感激

注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  

【问题讨论】:

  • 这听起来像是我曾经解决过的编程竞赛的问题。这听起来像是约束(逻辑)编程求解器的理想任务。
  • Willem Van Onsem 你能帮帮我吗?
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。 “他们中的一些人搞混了”不是问题描述。
  • 顺便说一下,这是一个“单词搜索”拼图,而不是填字游戏。

标签: python grid crossword


【解决方案1】:

我看到两个基本问题:用新词覆盖现有词,并且超出实际词的结尾。

第一个问题是您在添加新词之前没有费心查找之前的词。 第二个问题是因为您未能从输入中去除空白。

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

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

然后我运行它并得到这个输出:

Updated grid with   white   
. . . . . . . . .
. . . . . . . . .
. w h i t e     .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .

Updated grid with     kcalb 
.   . . . . . . .
. .   . . . . . .
. w h k t e     .
. . . . c . . . .
. . . . . a . . .
. . . . . . l . .
. . . . . . . b .
. . . . . . . . .
. . . . . . . . .

Updated grid with   blue   
.   . . . . . . .
. . b . . . . . .
. w h l t e     .
. . . . u . . . .
. . . . . e . . .
. . . . . .   . .
. . . . . . .   .
. . . . . . . . .
. . . . . . . . .

Updated grid with     neerg 
    . . . . . . .
.   b . . . . . .
. w n l t e     .
. . . e u . . . .
. . . . e e . . .
. . . . . r   . .
. . . . . . g   .
. . . . . . . . .
. . . . . . . . .

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

解决方案

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

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

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多