]>
git.zerfleddert.de Git - micropolis/blob - src/tk/library/demos/mkPuzzle.tcl
3 # Create a top-level window containing a 15-puzzle game.
6 # w - Name to use for new top-level window.
8 proc mkPuzzle
{{ w .p1
}} {
12 wm title
$w "15-Puzzle Demonstration"
13 wm iconname
$w "15-Puzzle"
14 message $w .msg
-font -Adobe - times
- medium
- r
- normal
--* -180 * -aspect 300 \
15 -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. Click the \" OK \" button when you've finished playing."
16 set order
{ 3 1 6 2 5 7 15 13 4 11 8 9 14 10 12 }
18 frame $w .
frame -geometry 120 x120
-borderwidth 2 -relief sunken
\
21 for { set i
0 } { $i < 15 } { set i
[ expr $i + 1 ]} {
22 set num
[ lindex $order $i ]
23 set xpos
( $num ) [ expr ( $i % 4 )* .25 ]
24 set ypos
( $num ) [ expr ( $i / 4 )* .25 ]
25 button $w .
frame .
$num -relief raised
-text $num \
26 -command "puzzle.switch $w $num "
27 place $w .
frame .
$num -relx $xpos ( $num ) -rely $ypos ( $num ) \
28 -relwidth .25 -relheight .25
33 button $w .ok
-text OK
-command "destroy $w "
35 pack append $w $w .msg
{ top fill
} $w .
frame { top expand padx
10 pady
10 } \
40 # Procedure invoked by buttons in the puzzle to resize the puzzle entries:
42 proc puzzle.
switch { w num
} {
44 if {(( $ypos ( $num ) >= ( $ypos ( space
) - .01 ))
45 && ( $ypos ( $num ) <= ( $ypos ( space
) + .01 ))
46 && ( $xpos ( $num ) >= ( $xpos ( space
) - .26 ))
47 && ( $xpos ( $num ) <= ( $xpos ( space
) + .26 )))
48 ||
(( $xpos ( $num ) >= ( $xpos ( space
) - .01 ))
49 && ( $xpos ( $num ) <= ( $xpos ( space
) + .01 ))
50 && ( $ypos ( $num ) >= ( $ypos ( space
) - .26 ))
51 && ( $ypos ( $num ) <= ( $ypos ( space
) + .26 )))} {
53 set xpos
( space
) $xpos ( $num )
56 set ypos
( space
) $ypos ( $num )
58 place $w .
frame .
$num -relx $xpos ( $num ) -rely $ypos ( $num )