Tcl HomeTcl Home Hosted by
ActiveState

Google SiteSearch

Image Based 15-Puzzle

Solve A 15-Puzzle Of A Cut-Up Image

Here's a scrambled image; see if you can put it right again..:

Source:

Here's the source for the puzzle:

proc puzzle {image} {
    set ind 0
    set w [expr [image width $image]/4]
    set h [expr [image height $image]/4]
    foreach i {3 1 6 2 5 7 0 13 4 11 8 9 14 10 12} {
	[image create photo $i] copy $image  -from [set x [expr $w*($i%4)]]  [set y [expr $h*($i/4)]]  [expr $x+$w] [expr $y+$h]
	button .$i -image $i -highlightthickness 0  -command "hit .$i" -bd 1
	grid .$i -row [expr $ind/4] -column [expr $ind % 4]
	incr ind
   }
}
proc hit {win} {
    global empty
    array set info [grid info $win]
    if {abs($empty(x)-$info(-column)) + abs($empty(y)-$info(-row)) == 1} {
        grid $win -row $empty(y) -column $empty(x)
	array set empty "x $info(-column) y $info(-row)"
    }
}
set empty(y) 3; set empty(x) 3
set data {....}
image create photo earth -data $data
unset data
puzzle earth