Reputation: 15
The image shows the output. You can clearly see how the letters are not perfectly aligned. We are trying to make a game for a school project and chose Word Search. We researched and have found a piece of coding that helps us make the grid as well as place our word into the grid. But the problem we are facing now is that the grid is not properly aligned. (ie. the 3rd letter from first row is not exactly above the third letter in second row.). This makes it difficult to find the word.But unfortunately we have not studied how to use random.choice
and cant seem to understand how to align the letters properly. All thoughts and opinions are appreciated.
import string
import random
print '''Welcome to WORD SEARCH. This game has five stages. You will have a crossword in each stage with one element hidden in each puzzle.
However, do remember that the game is over once you make a mistake. So, think carefully and play.
Good luck!!!!'''
width = 12
height = 12
def put_word(word,grid):
word = random.choice([word,word[::-1]])
d = random.choice([[1,0],[0,1],[1,1]])
xsize = width if d[0] == 0 else width - len(word)
ysize = height if d[1] == 0 else height - len(word)
x = random.randrange(0,xsize)
y = random.randrange(0,ysize)
for i in range(0,len(word)):
grid[y + d[1]*i][x + d[0]*i] = word[i]
return grid
Thank You
Upvotes: 0
Views: 154
Reputation: 191728
Seems to be printing fine to me... Maybe you should use a Monospaced font in your terminal / wherever you are printing out the grid.
grid = [['_' for _ in range(width)] for _ in range(height)]
def print_grid():
global grid
for row in grid:
print " ".join(row)
put_word("hello", grid)
put_word("world", grid)
print_grid()
You may want to fix the algorithm, though, because words are overlapping and you see I added "hello"
, but "herlo"
is there instead...
Sample Output
_ _ _ h _ _ _ _ _ _ _ _
_ _ _ _ e _ _ _ _ _ _ _
_ _ _ d l r o w _ _ _ _
_ _ _ _ _ _ l _ _ _ _ _
_ _ _ _ _ _ _ o _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
Upvotes: 2