# This demonstration script creates a 15-puzzle game using a collection
# of buttons.
#
# SCCS: @(#) puzzle.tcl 1.4 96/02/16 10:49:48
# puzzleSwitch --
# This procedure is invoked when the user clicks on a particular button;
# if the button is next to the empty space, it moves the button into th
# empty space.
proc puzzleSwitch {w num} {
global xpos ypos
if {(($ypos($num) >= ($ypos(space) - .01))
&& ($ypos($num) <= ($ypos(space) + .01))
&& ($xpos($num) >= ($xpos(space) - .26))
&& ($xpos($num) <= ($xpos(space) + .26)))
|| (($xpos($num) >= ($xpos(space) - .01))
&& ($xpos($num) <= ($xpos(space) + .01))
&& ($ypos($num) >= ($ypos(space) - .26))
&& ($ypos($num) <= ($ypos(space) + .26)))} {
set tmp $xpos(space)
set xpos(space) $xpos($num)
set xpos($num) $tmp
set tmp $ypos(space)
set ypos(space) $ypos($num)
set ypos($num) $tmp
place $w.frame.$num -relx $xpos($num) -rely $ypos($num)
}
}
set w .puzzle
catch {destroy $w}
toplevel $w
wm title $w "15-Puzzle Demonstration"
wm iconname $w "15-Puzzle"
positionWindow $w
label $w.msg -font $font -wraplength 4i -justify left -text "A 15-puzzle appears below as a collection of buttons. Click on any of the pieces next to the space, and that piece will slide over the space. Continue this until the pieces are arranged in numerical order from upper-left to lower-right."