puzzle.tcl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # puzzle.tcl --
  2. #
  3. # This demonstration script creates a 15-puzzle game using a collection
  4. # of buttons.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. # puzzleSwitch --
  10. # This procedure is invoked when the user clicks on a particular button;
  11. # if the button is next to the empty space, it moves the button into th
  12. # empty space.
  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. set w .puzzle
  33. catch {destroy $w}
  34. toplevel $w
  35. wm title $w "15-Puzzle Demonstration"
  36. wm iconname $w "15-Puzzle"
  37. positionWindow $w
  38. 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."
  39. pack $w.msg -side top
  40. ## See Code / Dismiss buttons
  41. set btns [addSeeDismiss $w.buttons $w]
  42. pack $btns -side bottom -fill x
  43. # Special trick: select a darker color for the space by creating a
  44. # scrollbar widget and using its trough color.
  45. scrollbar $w.s
  46. # The button metrics are a bit bigger in Aqua, and since we are
  47. # using place which doesn't autosize, then we need to have a
  48. # slightly larger frame here...
  49. if {[tk windowingsystem] eq "aqua"} {
  50. set frameSize 168
  51. } else {
  52. set frameSize 120
  53. }
  54. frame $w.frame -width $frameSize -height $frameSize -borderwidth 2\
  55. -relief sunken -bg [$w.s cget -troughcolor]
  56. pack $w.frame -side top -pady 1c -padx 1c
  57. destroy $w.s
  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 -bd 0 -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