Small change make script hang - don't understand why

3970 views python
-2

I've made a word searching game.It will print out a grid containing some words. The code goes like this:

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)

Then, I want to make the code print out random letters instead of dots(.) So I made some little changes.

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

But python stops functioning, it prints out nothing for a long time. I thought maybe python needed a lot of time to generate 15 x 50 random letters, so I changed the code to grid[row].append("a"), but python is still not functioning.

What had gone wrong?

answered question

Thanks for editing -- Exactly what I mean

1 Answer

1

You use '.' also to check that a cell is free in your grid (line 87), so if you fill cells with random letters, they won't be free anymore. You could instead fill empty spaces with random letters after putting in the words...

posted this

Have an answer?

JD

Please login first before posting an answer.