home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / lib / tk / demos / puzzle.tcl < prev    next >
Text File  |  1998-09-09  |  2KB  |  70 lines

  1. # puzzle.tcl --
  2. #
  3. # This demonstration script creates a 15-puzzle game using a collection
  4. # of buttons.
  5. #
  6. # SCCS: @(#) puzzle.tcl 1.4 96/02/16 10:49:48
  7.  
  8. # puzzleSwitch --
  9. # This procedure is invoked when the user clicks on a particular button;
  10. # if the button is next to the empty space, it moves the button into th
  11. # empty space.
  12.  
  13. proc puzzleSwitch {w num} {
  14.     global xpos ypos
  15.     if {(($ypos($num) >= ($ypos(space) - .01))
  16.         && ($ypos($num) <= ($ypos(space) + .01))
  17.         && ($xpos($num) >= ($xpos(space) - .26))
  18.         && ($xpos($num) <= ($xpos(space) + .26)))
  19.         || (($xpos($num) >= ($xpos(space) - .01))
  20.         && ($xpos($num) <= ($xpos(space) + .01))
  21.         && ($ypos($num) >= ($ypos(space) - .26))
  22.         && ($ypos($num) <= ($ypos(space) + .26)))} {
  23.     set tmp $xpos(space)
  24.     set xpos(space) $xpos($num)
  25.     set xpos($num) $tmp
  26.     set tmp $ypos(space)
  27.     set ypos(space) $ypos($num)
  28.     set ypos($num) $tmp
  29.     place $w.frame.$num -relx $xpos($num) -rely $ypos($num)
  30.     }
  31. }
  32.  
  33. set w .puzzle
  34. catch {destroy $w}
  35. toplevel $w
  36. wm title $w "15-Puzzle Demonstration"
  37. wm iconname $w "15-Puzzle"
  38. positionWindow $w
  39.  
  40. 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."
  41. pack $w.msg -side top
  42.  
  43. frame $w.buttons
  44. pack $w.buttons -side bottom -fill x -pady 2m
  45. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  46. button $w.buttons.code -text "See Code" -command "showCode $w"
  47. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  48.  
  49. # Special trick: select a darker color for the space by creating a
  50. # scrollbar widget and using its trough color.
  51.  
  52. scrollbar $w.s
  53. frame $w.frame -width 120 -height 120 -borderwidth 2 -relief sunken \
  54.     -bg [$w.s cget -troughcolor]
  55. pack $w.frame -side top -pady 1c -padx 1c
  56. destroy $w.s
  57.  
  58. set order {3 1 6 2 5 7 15 13 4 11 8 9 14 10 12}
  59. for {set i 0} {$i < 15} {set i [expr $i+1]} {
  60.     set num [lindex $order $i]
  61.     set xpos($num) [expr ($i%4)*.25]
  62.     set ypos($num) [expr ($i/4)*.25]
  63.     button $w.frame.$num -relief raised -text $num -highlightthickness 0 \
  64.         -command "puzzleSwitch $w $num"
  65.     place $w.frame.$num -relx $xpos($num) -rely $ypos($num) \
  66.     -relwidth .25 -relheight .25
  67. }
  68. set xpos(space) .75
  69. set ypos(space) .75
  70.