小改动让脚本挂起 - 不明白为什么



我做了一个单词搜索游戏。它将打印出一个包含一些单词的网格。代码如下:

import random
DirectionList = []
# Create a grid filled with "." representing a blank
def createGrid():
grid=[]
for row in range(15):
grid.append([])
for column in range(50):
grid[row].append(".")
return grid
# Print the grid to the screen
def printGrid(grid):
for row in range(len(grid)):
for column in range(len(grid[row])):
print(grid[row][column],end="")
print()
# Try to place the word. Return True if successful
# False if it failed and we need to try again.
def tryToPlaceWord(grid,word):
# Figure out the direction of the work.
# Change the 8 to a 7 if you don't want backwards
# words.
status_check = []
direction=random.randrange(0,8)
DirectionList.append(direction)
if( direction == 0 ):
x_change=-1
y_change=-1
if( direction == 1 ):
x_change=0
y_change=1
if( direction == 2 ):
x_change=1
y_change=-1
if( direction == 3 ):
x_change=1
y_change=0
if( direction == 4 ):
x_change=1
y_change=1
if( direction == 5 ):
x_change=0
y_change=1
if( direction == 6 ):
x_change=-1
y_change=1
if( direction == 7 ):
x_change=-1
y_change=0
# Find the length and height of the grid
height=len(grid)
width=len(grid[0])
# Create a random start point
column=random.randrange(width)
row=random.randrange(height)
# Check to make sure  the word won't run off the edge of the grid.
# If it does, return False. We failed.
if( x_change < 0 and column < len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( x_change > 0 and column > width-len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( y_change < 0 and row < len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
if( y_change > 0 and row > height-len(word) ):
status_check.append(False)
status_check.append("None")
return status_check
# Now check to make sure there isn't another letter in our way
current_column=column
current_row=row
for letter in word:
# Make sure it is blank, or already the correct letter.
if grid[current_row][current_column]==letter or grid[current_row][current_column]=='.':
current_row += y_change
current_column += x_change
else:
# Oh! A different letter is already here. Fail.
status_check.append(False)
status_check.append("None")
return status_check
# Everything is good so far, actually place the letters.
current_column=column
current_row=row
for letter in word:
grid[current_row][current_column]=letter
current_row += y_change
current_column += x_change
if 7 in DirectionList:
status_check.append(True)
status_check.append("True")
return status_check
else:
status_check.append(True)
status_check.append("None")
return status_check
# This just calls tryToPlaceWord until we succeed. It could
# repeat forever if there is no possible place to put the word.
def placeWord(grid,words):
for word in words:
succeed=False
while not(succeed):
status_check=tryToPlaceWord(grid,word)
succeed=status_check[0]
backward = status_check[1]
return backward
# Create an empty grid
grid = createGrid()
# A list of words
words = ["pandabear","fish","snake","porcupine","dog","cat","tiger","bird","alligator","ant","camel","dolphin"]
# Place some words
placeWord(grid,words)
backward = placeWord(grid,words)
print("Current status:n"
"tGenerating a word searching diagram...n"
"tWords :",len(words),"n"
"tBackword :",backward)
# Print it out
printGrid(grid)

然后,我想让代码打印出随机的字母而不是点(。(所以我做了一些小的更改。

import random
import string
DirectionList = []
# Create a grid filled random letters representing a blank
def createGrid():
grid=[]
for row in range(15):
grid.append([])
for column in range(50):
grid[row].append(random.choice(string.ascii_uppercase))
return grid

但python停止了运行,它很长一段时间都不打印任何东西。我想python可能需要很多时间来生成15 x 50个随机字母,所以我把代码改为grid[row].append("a"),但python仍然无法运行。

出了什么问题?

编辑:

将代码更改为

for letter in word:
# Make sure it is blank, or already the correct letter.
if grid[current_row][current_column]==letter or grid[current_row][current_column]==' ':
current_row += y_change
current_column += x_change

但密码仍然挂着。。。

您使用"."还可以检查网格中的一个单元格是否空闲(第87行(,所以如果用随机字母填充单元格,它们将不再空闲。你可以在输入单词后用随机字母填充空白。。。

最新更新