0

This is a wordhunt generator I'm building as an exercise. This function is supposed to place a new word on the matrix on a random position, without crossing the words that are already there.

So in this case the word "GAME" is supposed to be placed horizontally on the list, replacing the 'x' letters, without crossing any of the 'W' letters. Example:

Correct:

x G A M E x 
x x W x x x 
x x x W x x 
x x x x W x 
x x x x x W 
x x x x x x 

Incorrect

x x x x x x 
x x W x x x 
x x G W M E 
x x x x W x 
x x x x x W 
x x x x x x 

This is how I tried to buld the code:

from random import randint

word = 'GAME'

def place_word():

    wordhunt = [['x', 'x', 'x', 'x', 'x', 'x'],
                ['x', 'x', 'W', 'x', 'x', 'x'],
                ['x', 'x', 'x', 'W', 'x', 'x'],
                ['x', 'x', 'x', 'x', 'W', 'x'],
                ['x', 'x', 'x', 'x', 'x', 'W'],
                ['x', 'x', 'x', 'x', 'x', 'x']]


    #generate the initial position of the word on the matrix
    line = randint(0, 5)
    column = randint(0, 2)

    #iterator to place the new letters
    for i in range(0,4,1):

        #Check if the position is allowed
        if wordhunt[line][column+i] == 'x':

            wordhunt[line][column+i] = word[i]

            #if i reaches 3 the placement is complete
            if i == 3:
                return wordhunt

        #if it tries to place a letter on a forbidden position, it runs the function again
        else:
            place_word()

new_wordhunt = place_word()

for i in range(0, 6, 1):
    for j in range(0, 6, 1):
        print(new_wordhunt[i][j], end=' ')
    print()

In this case sometimes it generates expected results like the one bellow:

x x x x x x 
x x W x x x 
x x x W x x 
x x x x W x 
x x x x x W 
G A M E x x

But sometimes it generates incorrect results like this one:

x x x x x x 
x x W x x x 
x G A W E x 
x x x x W x 
x x x x x W 
x x x x x x

I've run a thousand testes but I just can't find out what is wrong with it.

Thank you for your help!

  • 1
    First, describe in words what your code does if the first and second spaces it wants to write to is free, but the third is blocked. – BadZen Aug 01 '23 at 01:15
  • Note that the line `else: place_word()` is exactly the same as `else: pass`. You're ignoring the result of the recursive function call – Brian61354270 Aug 01 '23 at 01:16
  • @BadZen I'm not sure what you're getting at. Their code doesn't respond to the first or second spaces being blocked either. `W A M E` and `G W M E` are both possible outputs of this code – Brian61354270 Aug 01 '23 at 01:17
  • Not related to the problem at hand: `range(0,4,1)` should simply be `range(len(word))` – jarmod Aug 01 '23 at 01:22

1 Answers1

0
from random import randint

word = 'GAME'

def place_word():

    wordhunt = [['x', 'x', 'x', 'x', 'x', 'x'],
            ['x', 'x', 'W', 'x', 'x', 'x'],
            ['x', 'x', 'x', 'W', 'x', 'x'],
            ['x', 'x', 'x', 'x', 'W', 'x'],
            ['x', 'x', 'x', 'x', 'x', 'W'],
            ['x', 'x', 'x', 'x', 'x', 'x']]


#generate the initial position of the word on the matrix
line = randint(0, 5)
column = randint(0, 2)

#iterator to place the new letters
for i in range(0,4,1):

    #Check if the position is allowed
    if 'W' not in  wordhunt[line]:

        wordhunt[line][column+i] = word[i]

        #if i reaches 3 the placement is complete
        if i == 3:
            return wordhunt

    #if it tries to place a letter on a forbidden position, it runs the function again
        else:
         place_word()
        
new_wordhunt = place_word()

for i in range(0, 6, 1):
   for j in range(0, 6, 1):
      print(new_wordhunt[i][j], end=' ')
   print()

Can you try this , but when I tried this course , many a times its taking too much time to generate the right random number , but the result are as you expected