How To Generate A Word Search Grid That Enables Words Using The Same Letter
Solution 1:
I see two basic problems: you overwrite existing words with new ones, and you write beyond the end of the actual word.
The first problem is that you haven't bothered to look for prior words before you add new ones. The second problem is because you fail to strip white-space out of the input.
I added some simple debugging instrumentation to track this. First, I initialised the grid to dots instead of letters, so I could see what's going on:
for j in range(p):
matrix[i].append(".")
Then, I printed the matrix after adding each word:
for i in range(0,len(word)):
grid[y+d[1]*i][x+d[0]*i]=word[i]
print"\nUpdated grid with\t", word, "\n",
for row in range(n):
print" ".join(matrix[row])
return grid
Then I ran it and got this output:
Updated grid with white
. . . . . . . . .
. . . . . . . . .
. w h i t e .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Updated grid with kcalb
. . . . . . . .
. . . . . . . .
. w h k t e .
. . . . c . . . .
. . . . . a . . .
. . . . . . l . .
. . . . . . . b .
. . . . . . . . .
. . . . . . . . .
Updated grid with blue
. . . . . . . .
. . b . . . . . .
. w h l t e .
. . . . u . . . .
. . . . . e . . .
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. . . . . . . . .
Updated grid with neerg
. . . . . . .
. b . . . . . .
. w n l t e .
. . . e u . . . .
. . . . e e . . .
. . . . . r . .
. . . . . . g .
. . . . . . . . .
. . . . . . . . .
You see what's happening: you write too far, and you crash over existing entries.
SOLUTION
I recommend that you follow what I've done so far: initialize to some non-letter that you find easy to read. Now, before you enter a word into the grid check that the path is clear. Even better, allow for writing across previous words if the intersecting letters match.
After you've placed all the words from the list, then fill in the rest of the grid.
Post a Comment for "How To Generate A Word Search Grid That Enables Words Using The Same Letter"