require 'tk' $w, $h = 400, 400 $x0, $y0 = 50, 20 $n, $dim = 4, 75 $blank = 16 $puzzle = [nil,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,"blank"] $goal = [nil,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,"blank"] $tile = [nil] $text = [nil] def linearconflict count = 0 return count end def manhattan count = 0 return count end def diffeval count = 0 return count end def move(index) temp = $puzzle[index] $puzzle[index]="blank" $puzzle[$blank] = temp $tile[$blank].fill='black' puts "text at index=#{$text[index].text}" $text[$blank].text="#{temp}" $tile[index].fill= 'white' $blank=index end def click(c, e) adjacent = false index= ((e.x-$x0)/$dim + 1) + (e.y-$y0)/$dim * 4 row = (index-1)/4+1 col = index-(row-1)*4 puts "#{e.x} #{e.y} Index: #{index} at row:#{row} col:#{col}" move(index) end root = TkRoot.new(:title => 'Sliding Tile Puzzle') canvas = TkCanvas.new(root, :width => $w, :height => $h, :background => 'black') canvas.pack k = 0 while k < $n j = 0 while j < $n x, y = $x0 + $dim*j, $y0 + $dim*k num = k*$n + j + 1 puts "drawing tile # #{num}: x=#{x}, y=#{y}, to #{x+$dim}, #{y+$dim}, $dim=#{$dim}" tile = TkcRectangle.new(canvas, x, y, x+$dim, y + $dim, :outline => 'white') text = TkcText.new(canvas, x+$dim/2, y+$dim/2, :text => "#{num}", :fill =>'white') $tile << tile $text << text j += 1 end k += 1 end $headings1 = TkcText.new(canvas, 120, $h-70, :text=>"# misplaced manhattan linear", :fill => 'white') $headings2 = TkcText.new(canvas, 130, $h-55, :text=>" tiles distance conflict", :fill => 'white') $evaltext = TkcText.new(canvas, 120, $h-$dim/2, :text => "#{diffeval} #{manhattan} #{linearconflict}", :fill => 'white') #puts "Array of tiles: #{$tile.inspect}" puts "for white tile: x=#{x}, y=#{y}, to #{x+$dim}, #{y+$dim}, $dim=#{$dim}" tile.fill = 'white' text.text = "" root.bind('1') { |event| click(canvas,event) } root.bind('q') { exit } Tk.mainloop