Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python wordsearch生成器与单词重叠_Python_Python 3.x - Fatal编程技术网

Python wordsearch生成器与单词重叠

Python wordsearch生成器与单词重叠,python,python-3.x,Python,Python 3.x,所以我一直在尝试制作一个单词搜索网格生成器,但我发现这个代码有一个问题,我不知道如何解决它。该代码生成单词搜索网格并将列表中的单词放入其中,但有时该单词的某些字符被另一个单词覆盖(因为位置分配是随机的,不考虑先前生成的索引)。我想解决这个问题,但我不知道如何 import string import random height = 15 width = 15 def wordsearch(word,grid): word = random.choice([word]) word

所以我一直在尝试制作一个单词搜索网格生成器,但我发现这个代码有一个问题,我不知道如何解决它。该代码生成单词搜索网格并将列表中的单词放入其中,但有时该单词的某些字符被另一个单词覆盖(因为位置分配是随机的,不考虑先前生成的索引)。我想解决这个问题,但我不知道如何

import string
import random

height = 15
width = 15
def wordsearch(word,grid):
    word = random.choice([word])
    wordlength = len(word)

    direction = random.choice([[1,0],[0,1],[1,1]])
    xsize = width   if direction[0] == 0 else width  - wordlength
    ysize = height if direction[1] == 0 else height - wordlength

    x = random.randrange(0,xsize)
    y = random.randrange(0,ysize)

    print([x,y])

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

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

for word in ['hello','red','yellow','green','blue','father','mother','sister','brother','food','drinks', 'cheese']:
    grid = wordsearch(word, grid)

print("\n".join(map(lambda row: " ".join(row), grid)))
例如,在[1,0]处,向下生成“绿色”,但在[1,2]处生成的单词“蓝色”替换了第一个“e”

[6, 1]
[12, 7]
[2, 4]
[1, 0]
[1, 2]
[7, 2]
[7, 4]
[11, 6]
[3, 9]
[7, 10]
[5, 0]
[6, 0]
t g n v x d c f y r s m d z o
t r f v j h r h l l o r e v e
y b y v w t w i e y v f r r m
z e l b w c e a n e y z c w j
a n y u l l o m z k s h u z d
u s i c e i z h o a s e c j r
l c g n v y k e c t b s g q g
e f b c p s f r x e h i r t f
y q p r t h h a z e o s e z h
o l x b r o t h e r e t r z x
m f m m n u o f j d s e i e e
v z u d k x t k o v i r d o k
l s q o q w a l o o u n y y v
m q c u r v w i f d d d a p g
b a h c f o y g g n x e v z s

您应该检查随机选择的单词位置是否与已选择的任何位置“碰撞”(重叠)。如果是,则放弃该职位并生成新职位

对于一个示例实现,我用一个元组来表示单词的位置:
(开始行、开始列、方向、长度)
,然后可以这样检查冲突:


(这可能是一个效率极低的实现,但应该是直观的;此外,对于小矩阵也是如此(您没有任何地方可以阻止这种情况发生。事实上,由于您没有存储为以前的单词选择的空格,因此您无法阻止这种情况发生;就
wordsearch
函数而言,网格完全随机启动,只添加了一个单词。还有
random.choice([word]))
似乎有点不必要。这是一个重复的问题吗@muuud,我可以发誓今天早上确实有这个问题,所以?你必须跟踪已经存在的单词,并测试新单词是否会覆盖现有单词。最简单的方法是没有重叠。如果你想更有趣,可以让单词共享字母s、 例如,“hello”和“red”中的“e”。这更复杂,因为您必须检查哪些起始位置和方向是正确的。请告诉我您更喜欢哪个,以及是否需要进一步的帮助。
def get_points(word_placement):
    start_row, start_col, direction, length = word_placement 
    if direction == 'vertical': # column remains same
        return [(start_row+i, start_col) for i in range(length)]

    elif direction == 'horizontal': # row stays  same
        return [(start_row, start_col+i) for i in range(length)]

    elif direction == 'diagonal':
        return [(start_row+i, start_col+i) for i in range(length)]


def do_they_collide (wa, wb):
    points_a = get_points(wa)
    points_b = get_points(wb)
    if len(set(points_a).intersection(set(points_b))) > 0:
        return True
    else:
        return False

## Let's take an example
wa = (0, 1, 'vertical', 5)   # green
wb = (3, 0, 'horizontal', 4) # blue

do_they_collide(wa, wb)