UserPreferences

PuzzleDisplayerProgram


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 

# 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