[Tutor] 15 puzzle

Maritza Rodriguez maritza_rodz@hotmail.com
Tue, 08 May 2001 17:48:56 -0500


Here is what I have for my "15 Puzzle" program.  The "EXIT" button quits the 
game completely.  The "DONE" button prints the messages that I want 
(although I don't know if it will work once I figure out how to Scramble and 
play the game).  I tried to add a definition for "Scramble", but it is not 
working.  Can anyone help me figure out if I am close or just totally way 
off?  Also, I just cannot figure out how to move the buttons to the blank 
spot.

Maritza

#Sets up the matrix and "DONE", "SCRAMBLE" and "EXIT" buttons
#Quits with "EXIT" button
#DONE button prints "solution" or "nosolution"

import sys
from Tkinter import *
import string
import copy
import whrandom
#import msvcrt
#import array
#from Numeric import*

def die(event=0):
    sys.exit(0)

def solution(f):
    print "Great!  You found a solution!!"

def nosolution(f):
    print "Sorry!  No Solution!!"

names=["1","2","3","4","5","6","7","8","9","10","11","12","13","14",
      "15"," "]
blankrow=3
blankcol=3

def Scramble(f):
    pass
    acpy = list(names)
    for x in range(1,16):
        sizeofacpy = len(acpy)-1
        location = whrandom.randint(0,sizeofacpy)
        names[x] = acpy[location]
        del acpy[location]
    offs = names.index(" ")
    blankrow=offs/4
    blankcol=offs%4

def __init__(self,w):
    n = 0
    for i in range(4):
        for j in range(4):
            item1=Button(w,text=names[n])
            item1.grid(row=i,column=j,sticky=E+W+N+S)

            item2=Button(w,text=names[n])
            item2.grid(row=i,column=j,sticky=E+W+N+S)

            item3=Button(w,text=names[n])
            item3.grid(row=i,column=j,sticky=E+W+N+S)

            item4=Button(w,text=names[n])
            item4.grid(row=i,column=j,sticky=E+W+N+S)

            item5=Button(w,text=names[n])
            item5.grid(row=i,column=j,sticky=E+W+N+S)

            item6=Button(w,text=names[n])
            item6.grid(row=i,column=j,sticky=E+W+N+S)

            item7=Button(w,text=names[n])
            item7.grid(row=i,column=j,sticky=E+W+N+S)

            item8=Button(w,text=names[n])
            item8.grid(row=i,column=j,sticky=E+W+N+S)

            item9=Button(w,text=names[n])
            item9.grid(row=i,column=j,sticky=E+W+N+S)

            item10=Button(w,text=names[n])
            item10.grid(row=i,column=j,sticky=E+W+N+S)

            item11=Button(w,text=names[n])
            item11.grid(row=i,column=j,sticky=E+W+N+S)

            item12=Button(w,text=names[n])
            item12.grid(row=i,column=j,sticky=E+W+N+S)

            item13=Button(w,text=names[n])
            item13.grid(row=i,column=j,sticky=E+W+N+S)

            item14=Button(w,text=names[n])
            item14.grid(row=i,column=j,sticky=E+W+N+S)

            item15=Button(w,text=names[n])
            item15.grid(row=i,column=j,sticky=E+W+N+S)
            n = n+1

    f = Frame(w)
    b1=Button(f,text="SCRAMBLE")
    b1.bind("<Button>", Scramble)
    b2=Button(f,text="DONE")
    if names == 
["1","2","3","4","5","6","7","8","9","10","11","12","13","14",
    "15"," "]: b2.bind("<Button>", solution)
    else:  b2.bind("<Button>", nosolution)
    b1.pack(side=LEFT)
    b2.pack(side=RIGHT)
    f.grid(row=4,column=0,columnspan=4)

    g = Frame(w)
    b3=Button(g,text="EXIT")
    if "EXIT":
        b3["command"] = die
    b3.pack(side=LEFT)
    g.grid(row=5,column=0,columnspan=5)

root=Tk()


gr=Toplevel(root)
gr.title("15-PUZZLE")
grc = Puzzle (gr)

root.withdraw()
root.mainloop()


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com