[FF] 2008-04-23 - I was remembering having seen such a game somewhere... The purpose of this is not really the game but the challenge of implementing that functionality in Tk [Canvas]. Do you know a better way of doing it rather than such canvas [bind]s? About the game: It's about tidying up the lines so that no line crosses another line. ''You win additional 100 points if you can extend it with an algorithm that solves it, showing the progress step-by-step :-)'' [http://wiki.tcl.tk/_repo/images/FF/36859403rl0.gif] ---- ====== canvas .c -width 480 -height 480 -background white grid .c -row 0 -column 0 -sticky news grid columnconfigure . 0 -weight 1 grid rowconfigure . 0 -weight 1 set ndots 0 set r 8 set lastx 0 set lasty 0 proc add_dot {x y} { if {$::ndots > 0} { .c create line $::lastx $::lasty $x $y -tags [list line \ [format line%02d%02d [set ::ndots] [incr ::ndots]]] } {incr ::ndots} .c create oval \ [expr $x-$::r] [expr $y-$::r] [expr $x+$::r] [expr $y+$::r] \ -fill red -tags [list dot dot$::ndots] set ::dot$::ndots 0 .c bind dot$::ndots "set ::dot$::ndots 1" .c bind dot$::ndots "set ::dot$::ndots 0" .c bind dot$::ndots " if {!\$::dot$::ndots} break set l1 [format line%02d%02d [expr $::ndots-1] $::ndots] set l2 [format line%02d%02d $::ndots [expr $::ndots+1]] set c1 \[.c coords \$l1] set c2 \[.c coords \$l2] set nc1 \[list \[lindex \$c1 0] \[lindex \$c1 1] %x %y] set nc2 \[list %x %y \[lindex \$c2 2] \[lindex \$c2 3]] .c coords \$l1 \$nc1 .c coords \$l2 \$nc2 .c coords dot$::ndots \[expr %x-$::r] \[expr %y-$::r] \[expr %x+$::r] \[expr %y+$::r] " set ::lastx $x set ::lasty $y .c lower line } for {set i 0} {$i < 30} {incr i} { add_dot [expr int(rand()*400)] [expr int(rand()*400)] } ====== ---- [HJG] That looks like a simple version of gPlanarity, from http://web.mit.edu/xiphmont/Public/gPlanarity.html, or perhaps http://johntantalo.com/wiki/Planarity or http://www.planarity.net. See also http://www.jasondavies.com/planarity for this as a browser-game. ---- See also: [Canvas geometrical calculations] - [Polygon intersection] - [Triangle Madness] ---- '''[arjen] - 2012-04-05 07:37:48''' With respect to an automatic solver: the dots form a single chain, so by moving the first dot to a corner and then putting the next one along, say, the horizontal edge and then the next one if it fits etc, you can create a spiral or a zigzag line to solve the puzzle. While not entirely trivial, because things must fit, it seems not too complicated either - it would be a simple strategy for a human being anyway. ---- '''[swankguy] - 2012-04-05''' If you do this in Swank you can use the [Swank Canvas Connectors] to connect the ovals. Then you don't need to worry about repositioning the lines. <> canvas | Games | GUI