Version 2 of HiLo2

Updated 2005-06-20 18:00:30

This is the GUI-version of HiLo:


 # HiLo2.tcl - HaJo Gurt - 2005-06-20
 # Simple number-guessing-game with GUI

 #########.#########:#########^#########+#########-#########*#########$#####

  proc random { {range 100} } {
  # Return a number in the range 0 .. $range-1
    return [expr {int(rand()*$range)}]
  }

  proc Disp { {w .res1} {str ""} } {
  # Output string to one of the text-widgets
    $w insert end "\n"
    $w insert end $str
    $w see end          ;# scroll to bottom
  }

  proc Disable {} {
  # Disable entry-widget, highlight Newgame-Button
    .inp1 config -state readonly
    .but1 config -bg green
  }

  proc NewGame {} {
  # Start a new game
    set ::nGuess  0
   #set ::Secret 13
    set ::Secret [expr {[random $::Max] +1 }]

    .res1 delete 0.0 end
    .res2 delete 0.0 end
    Disp .res1 "  Guess my number:\n"
    Disp .res2 "   (1 .. $::Max)  \n"

    .inp1 config -state normal
    .but1 config -bg grey
  }

  proc TestNum {} {  ;# ??
  # Test if input is numeric
    set ok [string is integer $::Guess]
    if {$ok==0} {bell}
    return $ok
  }

  proc Update {} {
  # Process input, update display
    global Secret Guess nGuess

    set ok [string is integer $::Guess]
    if {$ok==0} {
      bell         ;# alert: input-error
    } else {
      incr ::nGuess
      if {$Guess <  $::Secret} { Disp .res1 "$Guess is too low"  }
      if {$Guess >  $::Secret} { Disp .res2 "$Guess is too high" }
      if {$Guess == $::Secret} { Disable
                                 Disp .res1 "$Guess is correct.\nYou needed $nGuess guesses.";
                               }
    }
    set Guess ""     ;# clear input-field
  }

  proc Init {} {
  # Initialize values, build GUI
    global Max Secret Guess nGuess
    set Max 100

    frame  .f1
    frame  .f2              ;# -bg blue -relief ridge
    pack   .f1 .f2 -pady 2

    text   .res1 -width 20 -height 10
    text   .res2 -width 20 -height 10
    pack   .res1 .res2  -in .f1  -side left

    tk_optionMenu .opt1 Max 16 64 100 128 256 512 1000 1024 2048 4096 8192
    button        .but1 -text "New game" -command {NewGame}
    label         .lx   -text ""         -anchor c -padx 12          ;# Spacer
    label         .lab1 -text "Guess:"
    entry         .inp1 -width 5         -textvariable Guess  ;#-validatecommand TestNum -validate key
    pack          .opt1 .but1 .lx .lab1 .inp1  -in .f2  -side left -padx 2 -pady 2

    bind          .inp1 <Return> {Update}

    wm title . "HiLo 2"
  }

  Init
  NewGame
  focus -force .inp1

HJG The options -relief and -validatecommand did not work as expected, but otherwise everything works fine.


Category Games