Advertisement
Guest User

guess the number

a guest
Feb 10th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.01 KB | None | 0 0
  1. package require Tk
  2.  
  3. namespace eval guess {
  4.     variable max 100
  5.     variable min 1
  6.     variable count -1
  7.     variable guess 0
  8.     namespace export *
  9. }
  10.  
  11. proc guess::gui {} {
  12.     pack [label .g]
  13.     pack [frame .c] -pady 5
  14.     pack [button .c.l -text "Lower" -command guess::lower] -side left -padx 3
  15.     pack [button .c.h -text "Higher" -command guess::higher] -side left -padx 3
  16.     pack [button .c.ok -text "Done" -command guess::done] -side left -padx 3
  17.     guess::next
  18. }
  19.  
  20. proc guess::next {} {
  21.     variable min
  22.     variable max
  23.     variable guess
  24.     variable count
  25.     incr count
  26.     set guess [expr {($max+$min)/2}]
  27.     .g conf -text "The number is: $guess ?"
  28. }
  29.  
  30. proc guess::higher {} {
  31.     variable guess
  32.     variable min $guess
  33.     guess::next
  34. }
  35.  
  36. proc guess::lower {} {
  37.     variable guess
  38.     variable max $guess
  39.     guess::next
  40. }
  41.  
  42. proc guess::done {} {
  43.     variable count
  44.     .g conf -text "Guessed it in $count tries."
  45.     .c.ok conf -text Exit -command exit
  46. }
  47.  
  48. guess::gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement