The guy who had the post about the "vowel search" exercise got me to thinking 
about matrices of letters, and thence to word search games, which I have made a 
time or two by hand and they are a pain.

So I decided to try making a program that would put words into a word search.

This is very basic, there's no checking to see if your word is going to go out 
of bounds (yet), but it works.

So, just for the heck of it, I thought, what would happen if I put a word into 
the matrix, such that its first letter is in the center, and then it rotate 
around its insertion point.

This sets up a Tkinter text widget and inserts the matrix into it.

I figured I could just clear the widget by deleting everything in it, then 
putting a matrix, modified with the next rotation into it.

No dice.  The rotations are happening, but the text widget only materializes 
after the last rotation, and the final "frame" in the rotation is the only one 
it shows.

I've run into this same sort of issue with Tcl/Tk, and never solved it there, 
either.

Any ideas?  The last 10-12 lines are the ones that do the rotation.  I've tried 
moving the root.mainloop() statement up higher in the code.  That causes the 
text widget to show up, but no matrix inside.

from Tkinter import *
from tkFont import *

import random

root = Tk()

fixed_width = Font(family = 'Courier', size = 10)
textbox = Text(root, font = fixed_width, width = 40, height = 20)
textbox.pack()

def blank_matrix(sizeX, sizeY, fillchar):

    sizeX += 1
    sizeY += 1

    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    letter_matrix = []

    for row in range(1,sizeY):
        line = []
        for _column in range(1, sizeX):
            if fillchar == 'letters':
                letter = random.randrange(0, 26)
                line.append(letters[letter])
            else:
                line.append(fillchar)
        letter_matrix.append(line)

    return letter_matrix
    
def print_matrix(matrix):
    textbox.delete(1.0, END)
    for line in matrix:
        #print ' '.join(line)
        line = ' '.join(line) + '\n'
        textbox.insert(END, line)

def insert_word(word, print_dir, x, y, matrix):

    word = word.upper()
    word = list(word)
    
    print_dirs = dict()
    print_dirs['e'] = (1,0)
    print_dirs['ne'] = (1,-1)
    print_dirs['n'] = (0,-1)
    print_dirs['nw'] = (-1,-1)
    print_dirs['w'] = (-1, 0)
    print_dirs['sw'] = (-1, 1)
    print_dirs['s'] = (0, 1)
    print_dirs['se'] = (1,1)
         
    x_plus, y_plus = print_dirs[print_dir]
    
    for i in range(0, len(word)):
        matrix[y + (i * y_plus)][x + (i * x_plus)] = word[i]
       
    return matrix

directions = ['e', 'ne', 'n', 'nw', 'w', 'sw', 's', 'se']
for direction in directions:
    print direction
    matrix = blank_matrix(20, 20, '.')
    matrix = insert_word('test_word', direction, 10, 10, matrix)
    print_matrix(matrix)
    
root.mainloop()

I've also doctored the code to get a final matrix with all rotations 
included...so the rotations are for sure happening, but the text widget just 
isn't updating.

This is what it looks like with all possible rotations, in case I'm not making 
sense.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . D . . . . . . . D . . . . . . . D .
. . . R . . . . . . R . . . . . . R . .
. . . . O . . . . . O . . . . . O . . .
. . . . . W . . . . W . . . . W . . . .
. . . . . . _ . . . _ . . . _ . . . . .
. . . . . . . T . . T . . T . . . . . .
. . . . . . . . S . S . S . . . . . . .
. . . . . . . . . E E E . . . . . . . .
. . D R O W _ T S E T E S T _ W O R D .
. . . . . . . . . E E E . . . . . . . .
. . . . . . . . S . S . S . . . . . . .
. . . . . . . T . . T . . T . . . . . .
. . . . . . _ . . . _ . . . _ . . . . .
. . . . . W . . . . W . . . . W . . . .
. . . . O . . . . . O . . . . . O . . .
. . . R . . . . . . R . . . . . . R . .
. . D . . . . . . . D . . . . . . . D .
. . . . . . . . . . . . . . . . . . . .







      
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to