OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tk8.6.12 / library / demos / puzzle.tcl
1 # puzzle.tcl --
2 #
3 # This demonstration script creates a 15-puzzle game using a collection
4 # of buttons.
5
6 if {![info exists widgetDemo]} {
7     error "This script should be run from the \"widget\" demo."
8 }
9
10 package require Tk
11
12 # puzzleSwitch --
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
15 # empty space.
16
17 proc puzzleSwitch {w num} {
18     global xpos ypos
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)))} {
27         set tmp $xpos(space)
28         set xpos(space) $xpos($num)
29         set xpos($num) $tmp
30         set tmp $ypos(space)
31         set ypos(space) $ypos($num)
32         set ypos($num) $tmp
33         place $w.frame.$num -relx $xpos($num) -rely $ypos($num)
34     }
35 }
36
37 set w .puzzle
38 catch {destroy $w}
39 toplevel $w
40 wm title $w "15-Puzzle Demonstration"
41 wm iconname $w "15-Puzzle"
42 positionWindow $w
43
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."
45 pack $w.msg -side top
46
47 ## See Code / Dismiss buttons
48 set btns [addSeeDismiss $w.buttons $w]
49 pack $btns -side bottom -fill x
50
51 # Special trick: select a darker color for the space by creating a
52 # scrollbar widget and using its trough color.
53
54 scrollbar $w.s
55
56 # The button metrics are a bit bigger in Aqua, and since we are
57 # using place which doesn't autosize, then we need to have a
58 # slightly larger frame here...
59
60 if {[tk windowingsystem] eq "aqua"} {
61     set frameSize 168
62 } else {
63     set frameSize 120
64 }
65
66 frame $w.frame -width $frameSize -height $frameSize -borderwidth 2\
67         -relief sunken -bg [$w.s cget -troughcolor]
68 pack $w.frame -side top -pady 1c -padx 1c
69 destroy $w.s
70
71 set order {3 1 6 2 5 7 15 13 4 11 8 9 14 10 12}
72 for {set i 0} {$i < 15} {set i [expr {$i+1}]} {
73     set num [lindex $order $i]
74     set xpos($num) [expr {($i%4)*.25}]
75     set ypos($num) [expr {($i/4)*.25}]
76     button $w.frame.$num -relief raised -text $num -bd 0 -highlightthickness 0 \
77             -command "puzzleSwitch $w $num"
78     place $w.frame.$num -relx $xpos($num) -rely $ypos($num) \
79         -relwidth .25 -relheight .25
80 }
81 set xpos(space) .75
82 set ypos(space) .75