#!/usr/bin/env wish set usersize [lindex $argv 0] if {$usersize == ""} then { set size 5 } else { set size $usersize } set win 0 for {set i 0} {$i < $size} {incr i} { for {set j 0} {$j < $size} {incr j} { button .butr${i}c${j} -command "press $i $j" -background black -activebackground black grid .butr${i}c${j} -column $j -row $i } } proc rnd {n} { expr {int(rand()*$n)} } proc invert {row col} { global size win set l { {-1 0} {0 -1} {0 0} {1 0} {0 1} } foreach i $l { set r [expr $row + [lindex $i 0]] if {$r < 0 || $r >= $size} then {continue} set c [expr $col + [lindex $i 1]] if {$c < 0 || $c >= $size} then {continue} set colour [.butr${r}c${c} cget -background] if {$colour == "black"} then { .butr${r}c${c} configure -background white -activebackground white set win [expr $win + 1] } else { .butr${r}c${c} configure -background black -activebackground black set win [expr $win - 1] } } } proc press {row col} { global win invert $row $col if {$win == 0} then { exit } } for {set i 0} {$i < $size} {incr i} { for {set j 0} {$j < $size} {incr j} { if {[expr [rnd 1000] % 2] == 0} then { invert ${i} ${j} } } }