[HJG] '''HiLo''' is a simple number-guessing game: for each guess, you get the answer "too high" or "too low", until you guess right. With this game, you can explain how binary seaching works. ---- # HiLo.tcl - HaJo Gurt - 2005-06-19 # Simple Guess-the-number - game # # !! Run this in tclsh84 - wish has a bug with gets !! # See also: http://wiki.tcl.tk/10794 - [gets workaround] proc input {prompt} { puts -nonewline $prompt set n [gets stdin] } # "Automatic" input proc inputA {prompt} { puts -nonewline $prompt incr ::x } # Return a number in the range 0 .. $range-1 proc random { {range 100} } { return [expr {int(rand()*$range)}] } #########.#########:#########^#########+#########-#########*#########$##### proc HiLo { {Max 100} } { #set Secret 11 set Secret [expr {[random $Max] +1 }] puts "Guess my number (1 .. $Max)" set Try 0 while 1 { incr Try set Num [ input "Your guess #$Try: " ] if {$Num < $Secret} { puts "$Num is too low" } if {$Num > $Secret} { puts "$Num is too high" } if {$Num == $Secret} { puts "$Num is correct - You needed $Try guesses."; break } if {$Try >= 12} { puts "You won't guess it..."; break } } puts "Bye!" } ### set x 0 ;# value for inputA catch {console show} #HiLo 1024 HiLo ;# default: 100 ---- It looks like the biggest problem with this program is the simple [gets] for reading from the user. Even the FAQ at http://www.tcl.tk/man/tcl8.4/TclCmd/gets.htm and the tutorial at http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html don't have useful information about this... ----