【问题标题】:Problem with Word Search Generator with python带有python的单词搜索生成器的问题
【发布时间】:2019-03-05 12:25:12
【问题描述】:

我正在尝试实现一些代码来运行搜索游戏生成器,并在数组上包含一些单词,所以我决定使用一些在视频中测试过​​的代码,在视频中似乎运行正常,但是当我运行时它冻结的代码,所以我想知道发生了什么:

这是我正在谈论的代码:

import random
import string

words = ['PYTHON', 'ROBBIE', 'GITHUB', 'BEEF']
grid_size=15
grid = [['_' for _ in range(grid_size)] for _ in range(grid_size)]
orientations = ['leftright','updown','diagonalup','diagonaldown']
#Prints the grid
def print_grid():
    for x in range(grid_size):
        print('\t'*5+' '.join(grid[x]))
#Generates grid
def generategrid(words):
    for word in words:
        word_length = len(word)
        placed = False
        while not placed:
            orientation = random.choice(orientations)
            #Sets orientation given by a random number
            if orientation == 'leftright':
                step_x=1
                step_y=0
            if orientation == 'updown':
                step_x = 0
                step_y = 1
            if orientation == 'diagonalup':
                step_x = 1
                step_y = 1
            if orientation == 'diagonaldown':
                step_x = 1
                step_y = -1
        #We generate a random starting point, then we calculate the ending point and if it exceeds the limit, we calculate the number again
        x_position = random.randint(0,grid_size)
        y_position = random.randint(0,grid_size)
        ending_x = x_position + word_length*step_x
        ending_y = y_position + word_length*step_y
        if ending_x < 0 or ending_x >= grid_size: continue
        if ending_y < 0 or ending_y >= grid_size: continue
        failed=False
        #we set the word on the previously given position
        for i in range(word_length):
            character = word[i]
            new_position_x = x_position + i*step_x
            new_position_y = y_position + i*step_y
            character_at_new_position = grid[new_position_x][new_position_y]
            #if there is some character that could be used to form the word
            if character_at_new_position != '_':
                if character_at_new_position == character:
                        continue
                else:
                        failed = True
                        break      
        if failed:#We do the process from above again until we can put the word on the grid 
            continue
        else:
            #Everything worked perfectly and the word was placed without problems
            for i in range(word_length):
                character = word[i]
                new_position_x = x_position + i*step_x
                new_position_y = y_position + i*step_y
            grid[new_position_x][new_position_y] = character
            placed = True
generategrid(words)
print_grid()

当我运行程序时,它会冻结,但是在视频中,此代码似乎运行良好。任何建议或观察将不胜感激!

【问题讨论】:

    标签: python arrays search generator word


    【解决方案1】:

    while not placed: 之后有一个无限循环。我不完全确定代码的意图是什么,但我认为您的意思是在一定时间后设置placed = True

    在代码的更下方,它正是这样做的,所以我的猜测是缩进错误。在 Python 中,缩进定义了循环的范围,因此请仔细检查您正在查看的示例中的缩进。

    【讨论】:

    • 好吧,placed = True 在 for 中,因此暂时无法访问,我还发现由于我的懒惰,我 复制和粘贴'd 一些计算最终位置的代码。顺便说一句,这段代码在给定一个数组的情况下生成一个单词搜索游戏,例如,在这种情况下,一个可能的输出是:imgur.com/a/SobI38C,我对 python 有点陌生,所以这是一个像我这样的新手很常见的细节。
    • 哦,嘿,看起来很酷!在 Python 中复制和粘贴是一个真正的问题,因为您最终可能会复制制表符而不是空格,并且如果将它们组合在一起,该语言将不会运行代码。我不确定您使用的是什么编辑器,但许多编辑器都具有将制表符转换为空格和将空格转换为制表符的功能。只是半相关的,但如果你还不知道它会有所帮助。
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多