3 # This demonstration script creates a 15-puzzle game using a collection
6 # RCS: @(#) $Id: puzzle.tcl,v 1.4 2002/08/31 06:12:28 das Exp $
8 if {![info exists widgetDemo
]} {
9 error "This script should be run from the \"widget\" demo."
13 # This procedure is invoked when the user clicks on a particular button;
14 # if the button is next to the empty space, it moves the button into th
17 proc puzzleSwitch
{w num
} {
19 if {(($ypos($num) >= ($ypos(space
) - .01))
20 && ($ypos($num) <= ($ypos(space
) + .01))
21 && ($xpos($num) >= ($xpos(space
) - .26))
22 && ($xpos($num) <= ($xpos(space
) + .26)))
23 ||
(($xpos($num) >= ($xpos(space
) - .01))
24 && ($xpos($num) <= ($xpos(space
) + .01))
25 && ($ypos($num) >= ($ypos(space
) - .26))
26 && ($ypos($num) <= ($ypos(space
) + .26)))} {
28 set xpos
(space
) $xpos($num)
31 set ypos
(space
) $ypos($num)
33 place $w.
frame.
$num -relx $xpos($num) -rely $ypos($num)
40 wm title
$w "15-Puzzle Demonstration"
41 wm iconname
$w "15-Puzzle"
44 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."
48 pack $w.buttons
-side bottom
-fill x
-pady 2m
49 button $w.buttons.dismiss
-text Dismiss
-command "destroy $w"
50 button $w.buttons.code
-text "See Code" -command "showCode $w"
51 pack $w.buttons.dismiss
$w.buttons.code
-side left
-expand 1
53 # Special trick: select a darker color for the space by creating a
54 # scrollbar widget and using its trough color.
58 # The button metrics are a bit bigger in Aqua, and since we are
59 # using place which doesn't autosize, then we need to have a
60 # slightly larger frame here...
62 if {[string equal
[tk windowingsystem
] aqua
]} {
68 frame $w.
frame -width $frameSize -height $frameSize -borderwidth 2\
69 -relief sunken
-bg [$w.s cget
-troughcolor]
70 pack $w.
frame -side top
-pady 1c
-padx 1c
73 set order
{3 1 6 2 5 7 15 13 4 11 8 9 14 10 12}
74 for {set i
0} {$i < 15} {set i
[expr {$i+1}]} {
75 set num
[lindex $order $i]
76 set xpos
($num) [expr {($i%4)*.25}]
77 set ypos
($num) [expr {($i/4)*.25}]
78 button $w.
frame.
$num -relief raised
-text $num -highlightthickness 0 \
79 -command "puzzleSwitch $w $num"
80 place $w.
frame.
$num -relx $xpos($num) -rely $ypos($num) \
81 -relwidth .25 -relheight .25