如何生成使用相同字母启用单词的单词搜索网格 [英] How to generate a word search grid that enables words using the same letter

查看:150
本文介绍了如何生成使用相同字母启用单词的单词搜索网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用给定的单词列表制作一个单词搜索网格。
我的问题是某些单词无法正常显示。



我已经更新了代码,请参阅下面



我试图修复以前的错误,现在我得到了一个超出范围的错误,我无法修复,如果有人可以帮助,我将不胜感激。 b

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

程序需要这样的文本文件:


  9 9 
白色
黑色
蓝色
绿色
粉红色
黄色
红色
灰色
紫色


其中文件中的前两个数字是网格的维度,其余是要放置在网格中的词。

  import random 
导入字符串
fi = input('插入条目文件名(entry.txt):')
fo = input('插入退出文件名(.txt):')
grid_size = []
word s = []
matrix = []
$ b $ def read_file(存储):
file =打开(存储)

n = file.readline()
lista = n.split()
lista = list(map(int,lista))#设置网格的大小
为lista中的i:
grid_size.append( i)

作为文件中的行:
line = line.replace(\ n,)
words.append(line)
file。 close()

def grid_generator(grid_size):
n,p = grid_size
在范围内(n):
matriz.append([])
为范围(p)中的j:
matriz [i] .append(。)

def sets_word_inside(grid_size,word,grid):
n,p = grid_size
word = random.choice([word,word [:: - 1]])
#horizo​​ntal,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

给我,列举的字母(word):
char = grid [y + d [1] * i] [x + d [0] * i]
如果char!=和char!=字母:
#如果达到已经填满的空间 - 重新启动进程。
#第二个条件允许创建与重复单词交叉的单词。

返回False
grid [y + d [1] * i] [x + d [0] * i] =字母[i]
返回True

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

  9 
白色
黑色
蓝色
绿色
粉红色
黄色
红色
灰色
紫色
pwbityldi
pvwoleeyt
xgaxjrimg
qicjbgjex
sskgqlgrr
pinkioutr
elprupgeo
lablsrpgy
coryeufrx


第一个问题是,在添加新单词之前,您并没有打算查找前面的单词。
第二个问题是因为你不能从输入中去除空白。



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

 对于范围内的j(p ):
matrix [i] .append(。)

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

 对于范围内的(0,len(word)):
grid [ y + d [1] * i] [x + d [0] * i] = word [i]

打印\ n已更新的网格,带\ t,单词\\\
(n):
print.join(matrix [row])

返回栅格



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

 用白色
更新网格。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。我希望。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。

用kcalb
更新了网格。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。
。 w h k t e。
。 。 。 。 C 。 。 。 。
。 。 。 。 。一个 。 。 。
。 。 。 。 。 。 l。 。
。 。 。 。 。 。 。 b。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。

用蓝色
更新了网格。 。 。 。 。 。 。 。
。 。 b。 。 。 。 。 。
。 w h l t e。
。 。 。 。你好。 。 。
。 。 。 。 。 e。 。 。
。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。

使用neerg
更新网格。 。 。 。 。 。 。
。 b。 。 。 。 。 。
。 w n l t e。
。 。 。欧洲联盟 。 。 。 。
。 。 。 。 e。 。 。
。 。 。 。 。 r。 。
。 。 。 。 。 。 G 。
。 。 。 。 。 。 。 。 。
。 。 。 。 。 。 。 。 。

您会看到发生了什么:您写得太过分了,而且会撞击现有条目。



解决方案

我建议您遵循我迄今所做的工作:初始化你发现容易阅读的非字母。现在,在向网格输入一个单词之前,请检查路径是否清晰。更好的是,如果相交字母匹配,则允许在以前的单词中进行书写。



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


I'm trying to make a word-search grid with a list of given words. My problem is that some of the words don't appear properly.

I have updated the code, see below

I have tried to fix the previous error and now I am getting an out of range error that i cant fix, if anyone can help I would appreciate

Note 1: I didnt include all the program functions, if required I will include them later

The program takes in a text file like this:

9 9  
white  
black  
blue  
green  
pink  
yellow  
red  
grey  
purple  

where the first 2 numbers in the file are the dimensions of the grid, and the rest is the words to place in the grid.

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

For now the output of the code is something like this:

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  

解决方案

I see two basic problems: you overwrite existing words with new ones, and you write beyond the end of the actual word.

The first problem is that you haven't bothered to look for prior words before you add new ones. The second problem is because you fail to strip white-space out of the input.

I added some simple debugging instrumentation to track this. First, I initialised the grid to dots instead of letters, so I could see what's going on:

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

Then, I printed the matrix after adding each word:

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

Then I ran it and got this output:

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   .
. . . . . . . . .
. . . . . . . . .

You see what's happening: you write too far, and you crash over existing entries.

SOLUTION

I recommend that you follow what I've done so far: initialize to some non-letter that you find easy to read. Now, before you enter a word into the grid check that the path is clear. Even better, allow for writing across previous words if the intersecting letters match.

After you've placed all the words from the list, then fill in the rest of the grid.

这篇关于如何生成使用相同字母启用单词的单词搜索网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆