python 的词搜索生成器问题
Problem with Word Search Generator with python
我正在尝试实现一些代码,这些代码 运行 是一个搜索游戏生成器,在数组中包含一些单词,所以我决定使用一些在视频中测试过的代码,在视频中似乎是 运行 正常,但是当我 运行 代码冻结时,所以我想知道发生了什么:
这是我正在谈论的代码:
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()
当我 运行 程序冻结时,但是在视频中这段代码似乎运行良好。任何建议或观察将不胜感激!
您在 while not placed:
之后有一个无限循环。我不完全确定代码的意图是什么,但我认为你的意思是在一定时间后设置 placed = True
。
在代码的更下方,它正是这样做的,所以我猜是缩进错误。在 Python 中,缩进定义了循环的范围,因此请仔细检查您正在查看的示例中的缩进。
我正在尝试实现一些代码,这些代码 运行 是一个搜索游戏生成器,在数组中包含一些单词,所以我决定使用一些在视频中测试过的代码,在视频中似乎是 运行 正常,但是当我 运行 代码冻结时,所以我想知道发生了什么:
这是我正在谈论的代码:
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()
当我 运行 程序冻结时,但是在视频中这段代码似乎运行良好。任何建议或观察将不胜感激!
您在 while not placed:
之后有一个无限循环。我不完全确定代码的意图是什么,但我认为你的意思是在一定时间后设置 placed = True
。
在代码的更下方,它正是这样做的,所以我猜是缩进错误。在 Python 中,缩进定义了循环的范围,因此请仔细检查您正在查看的示例中的缩进。