Updated 2007-07-11 11:24:12 by LV

if 0 {Richard Suchenwirth 2005-06-03 - Here's a tiny toy to simulate the game of Lotto, where six or seven numbers out of 1..49 are drawn.

Click on the canvas to start a drawing. Six numbers are randomly selected and marked with a red circle, a seventh one (MG called the "bonus ball" in the English lottery) is marked in yellow.}
 proc main {} {
    set dx 15
    set dy 15
    pack [canvas .c -width [expr {$dx*8}] -height [expr {$dy*9}]]
    set x $dx; set y $dy
    foreach i [iota1 49] {
        .c create text $x $y -text $i
        if {$i%7} {
            incr x $dx
        } else {set x $dx; incr y $dy}
    .c create text [expr {4*$dx}] $y -text "Click to draw"
    bind .c <1> {draw .c}
 proc draw w {
    $w delete marked
    set numbers [iota1 49]
    foreach i [iota1 6] {
       circle $w [$w bbox [ldraw numbers]] red
       update idletasks
       after 500
    circle $w [$w bbox [ldraw numbers]] yellow
    $w lower marked
 proc circle {w coords color} {
    $w create oval $coords -fill {} -outline $color -width 2 -tag marked

#------------------ Generally useful functions:
 proc iota1 n {
    set res {}
    for {set i 1} {$i<=$n} {incr i} {lappend res $i}
    set res
 proc ldraw _list {
    upvar 1 $_list list
    set pos [expr {int(rand()*[llength $list])}]
    K [lindex $list $pos] [set list [lreplace $list $pos $pos]]
 proc K {a b} {set a}


if 0 {

HJG Now it would be nice to see how many draws would be required to get a jackpot :-) but I have trouble to identify the numbers under the mouse-cursor... RS: I would prefer if people discussed changes, rather than just putting them in - this page was also meant as an example of simplicity, and isn't any more (restored now :^) So I moved your changes to Lotto 2.

MG commented out the line "pack .b" in main, as there doesn't seem to be a .b widget, and it was causing an error and preventing the bind command from running. Perhaps this is left over from HJG's changes by mistake? - RS: Yes, thanks - I had to restore manually, as there was no Wiki revision yet, and was too hurried.. and a bit frustrated, because several design decisions were reverted. For instance, why add a button if the canvas itself can act as one, just by a <1> binding? People of course have different requirements - mine was to deliver the lotto functionality as simple as possible.

HJG Sorry to upset you. I don't like it when the window-background/canvas itself has some functionality. This leads to surprises when such a window is in the background, you click on it to get it in front, and it instantly performs some action...

Category Toys - Arts and crafts of Tcl-Tk programming }