require 'tk' $w, $h = 300, 300 $x0, $y0 = 30, 30 $n, $dim = 8, 30 $playerdim = 24 $white = [29,36] $black = [28,37] $game = [nil,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,"b","w",30,31,32,33,34,35,"w","b",38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64] $tile = [nil] $text = [nil] $players = [nil] $movenum=0 def move(index) temp = $game[index] if $movenum % 2 == 1 $game[index]="b" $players[index].fill='black' $black << index else $game[index]="w" $players[index].fill='white' $white << index end # $game[$blank] = temp # $text[$blank].text="#{temp}" # $blank = index # $tile[index].fill= 'white' end def click_basic(c, e) puts "#{e.x} #{e.y}" end def click(c, e) adjacent = false index= ((e.x-$x0)/$dim + 1) + (e.y-$y0)/$dim * 8 row = (index-1)/4+1 col = index-(row-1)*4 puts "#{e.x} #{e.y} Index: #{index} at row:#{row} col:#{col}" $movenum += 1 move(index) =begin if ($blank-index).abs==1 and row==(($blank-1)/4+1) adjacent = true puts "adjacent row" elsif ($blank-index).abs==4 and col==($blank-((($blank-1)/4+1)-1)*4) adjacent = true puts "adjacent col" else adjacent = false puts "not adjacent" end if adjacent move(index) end =end # $evaltext.text = "#{diffeval} #{manhattan} #{linearconflict}" # puts "puzzle=#{$puzzle.inspect}" end root = TkRoot.new(:title => 'Othello') 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 tile = TkcRectangle.new(canvas, x, y, x+$dim, y + $dim, :outline => 'white', :fill => "green") text = TkcText.new(canvas, x+2, y+2, :text => "#{num}", :fill =>'black') player = TkcOval.new(canvas, x+($dim-$playerdim)/2, y+($dim-$playerdim)/2, x+$playerdim, y+$playerdim, :fill => "green") if $white.include?(num) player.fill = "white" elsif $black.include?(num) player.fill = "black" end $tile << tile $text << text $players << player j += 1 end k += 1 end #tile.fill = 'white' #text.text = "" root.bind('1') { |event| click(canvas,event) } root.bind('q') { exit } Tk.mainloop