[Tutor] 15 puzzle

Michael P. Reilly arcege@speakeasy.net
Wed, 9 May 2001 08:01:39 -0400 (EDT)


Maritza Rodriguez wrote
> 
> Thank you for responding.  I am confused, though.  I understand the first 
> part about adding a new method to check the table against the solution.  
> "Solution = ..." : this of,course goes under the class, right?  Now we 
> define solved(self); that makes sense.  OK when we define 
> is_it_really_solved(self), we are simply saying that if the puzzle is solved 
> it will return the dialog stated and if it is not solved it will return the 
> other dialog stated, right?  The next definition is where I got confused.  
> "makebuttons(self,w)"; what is this part of the program saying?  What do you 
> mean by blocks and keeping track of the buttons and making commands for 
> them?  Are you referring to the number buttons?  What does this do to them?  
> Is this the part that moves these buttons?
> Please excuse me for asking so many questions.  I am totally new to 
> programming, especially python.  Please help.
> 
> Maritza

Hi, I didn't want to spoil your fun by solving the problem for you, so
I didn't try to fill things out for you.

You are correct, the "is_it_really_solved" routine is to give some
feedback to the user: did I win or not.  It is better to put the actual
test in a separate method in case you need to change how you determine how
it is solved later (or change how self.names is used).  The "solution"
variable because what is called a "class member", it is shared by
all instances, so it is rarely modified in these types of programs.
All these here are defined within the class.

One common way of writing Tkinter programs is to have a method that
creates all the Tk widgets used in the class.  My makebuttons method is
doing that, passing the enclosing widget, w, to place the new ones in.

By keeping track of the "blocks" (buttons), you can modify their text
attributes as they get pressed instead of trying to move them.  But
either way, moving them or changing them, you need to keep track of
the button widgets created so you can do that.

Also, I added (incorrectly I noticed, sorry) a "callback" to the buttons.
A callback is a part of your program called by some library routine that
is not under you control.  In this case, when the button is pressed,
the lambda function is called.. not by any part of your code.

In essence, create buttons with the text from the "names" ("1",
"2", etc.).  Each button registers another method which gets called
when the user presses that button.  The method checks to see if it is
next to the blank space.  If it is, swaps the text in those buttons,
and in the "names" list.  This "simulates" the buttons moving without
going through all the coding.

  -Arcege


-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |