Home ]Software ]Curriculum ]Hardware ]Community ]News ]Publications ]Search ]

# Put this in the directory with the 1-8.gifs and run like:
#   python PuzzleDisplayer.py 13s824765 7128s3654

# Assumes that you have a file called EightPuzzle
# which either imports or defines your search function.
# In addition, you should alias your search function:
# SolveIt = A_star

from Search import *
from EightPuzzleState import *
import Tkinter, sys, time, posixpath

class Display(Tkinter.Toplevel):
    def __init__(self, root, start, goal, node):
        Tkinter.Toplevel.__init__(self, root)
        self.root = root
        self.start = start
        self.goal = goal
        self.node = node
        self.tileImage = {}
        self.tileButton = {}
        for i in range(1, 9):
            file = "%d.gif" % i
            if posixpath.exists(file):
                self.tileImage[i] = Tkinter.PhotoImage(file=file)
                self.tileButton[i] = Tkinter.Button(self, text="%d" % i, image = self.tileImage[i])
            else:
                self.tileImage[i] = None
        self.protocol('WM_DELETE_WINDOW',self.destroy)
        self.wm_title("Eight Puzzle")
        path = []
        while node != None:
            path.insert(0, node)
            node = node.parent
        for node in path:
            self.drawBoard(node.state)
            self.root.update_idletasks()
            time.sleep(1)

    def drawBoard(self, state):
        for y in range(3):
            for x in range(3):
                value = state.getTile(x, y)
                if value != ' ':
                    self.tileButton[int(value)].grid_configure(row=y,column=x,
                          sticky=Tkinter.E+Tkinter.W+Tkinter.N+Tkinter.S)

    def destroy(self):
        sys.exit(1)

if __name__ == '__main__':
    # if you don't pass in args, show proper usage:
    if len(sys.argv) != 3:
        print """
usage: python PuzzleDisplayer.py START GOAL
       where START #########
             GOAL  #########
       examples: python PuzzleDisplayer.py 13s824765 1238s4765
                 python PuzzleDisplayer.py 75648s123 12345678s
                 python PuzzleDisplayer.py 13s824765 7128s3654
"""
        sys.exit(1)
    else:
        # parse command line into representation
        s = []
        for c in sys.argv[1]:
            if c == 'S' or c == 's':
                s.append( ' ' )
            else:
                s.append( int(c) )
        g = []
        for c in sys.argv[2]:
            if c == 'S' or c == 's':
                g.append( ' ' )
            else:
                g.append( int(c) )
    start = EightPuzzleState(*s) # pass in list as args
    goal = EightPuzzleState(*g) # pass in list as args
    node = SolveIt(start, goal) # solve it!
    root = Tkinter.Tk() # make a root window
    root.withdraw() # hide it
    display = Display(root, start, goal, node) # show solution
    display.mainloop() # leave it on screen


Home ]Software ]Curriculum ]Hardware ]Community ]News ]Publications ]Search ]

CreativeCommons View Wiki Source | Edit Wiki Source | Mail Webmaster