0hc.net Menu
Home Overview
About Menu
Impressum

Tri

Challenge: Sort the numbers! require 'tk' class Gtri def fcol(x) return "#0055FF" if x==0 y=x%5 if y==0 then "#FF0000" elsif y==1 "#FF6A00" elsif y==2 "#FFA000" elsif y==3 "#FFCC00" elsif y==4 "#FFFF00" end end def fmove(mov) i=@pos if mov==0 then pos=(@pos-1*$grea[0])% ($grea[0]*$grea[1]) end if mov==1 then i+=1 i-=$grea[0] if (i% $grea[0]) == 0 return if ((i% $grea[0] +(i/$grea[0]+1)% 2 )%2)==1 pos=@pos+1 pos-=$grea[0] if (pos% $grea[0]) == 0 end if mov==2 then pos=(@pos+1*$grea[0])% ($grea[0]*$grea[1]) end if mov==3 then return if ((i% $grea[0] +(i/$grea[0]+1)% 2 )%2)==1 pos=@pos-1 pos+=$grea[0] if (pos% $grea[0]) == $grea[0]-1 end @field[@pos], @field[pos] = @field[pos], @field[@pos] @glif[@pos].text, @glif[pos].text = @field[@pos].to_s, @field[pos].to_s @glif[@pos].background, @glif[pos].background = fcol(@field[@pos]), fcol(@field[pos]) @pos=pos end def initialize @bor = 10 @sizw = 21 @sizh = 20 @wid = 2 root = TkRoot.new {title "Tri - Game"} root.width(@bor*2 + @sizw*$grea[0] - 1).height(@bor*2 + @sizh*$grea[1] - 1) @canvas = TkCanvas.new(root) @canvas.width(@sizw*$grea[0]-1).height(@sizh*$grea[1]-1) @canvas.background("#222222") @canvas.place('x' => 8, 'y' => 8) @pos=0 @glif=[] @field=[] (0...($grea[0]*$grea[1])).each {|i| @field.push(i)} (0...($grea[0]*$grea[1])).each {|i| a=fcol(i); @glif.push(TkLabel.new(root){text i.to_s; background a; width 2})} (0...($grea[0]*$grea[1])).each {|i| @glif[i].place('x' => @bor+((i% $grea[0] )*@sizw)+((i% $grea[0] +(i/$grea[0]+1)% 2 )%2)*@wid, 'y' => @bor+(((i/$grea[0] )% $grea[1] )*@sizh))} (($grea[0]*$grea[1])**2).times{fmove(rand(3))} root.bind("KeyPress-Up") {fmove(0)} root.bind("KeyPress-Down") {fmove(2)} root.bind("KeyPress-Left") {fmove(3)} root.bind("KeyPress-Right") {fmove(1)} end end $grea = [2,2] ($grea[0] = 2*ARGV[0].to_i; $grea[1] = 2*ARGV[1].to_i) if ARGV.length == 2 Gtri.new Tk.mainloop
0hc.net    © 2001-2014 Harald Wolfsgruber