0hc.net Menu
Home Overview
About Menu
Impressum

Colors

Challenge: Get the maximum possible points! require 'tk' class GColors def fand(f) res=0 (0...f.length).each {|i| res+=f[i]} return res end def gcol x case x when 0 return "white" when 1 return "orange" when 2 return "red" else return "black" end end def gcalc x if @afield[x] != -1 then @afield[x]-=1 @afield[x+1] = (@afield[x+1]+1)%3 if ((x+1) % @sfield[1] != 0 && @afield[x+1] != -1) @afield[x-1] = (@afield[x-1]+1)%3 if (x % @sfield[1] != 0 && @afield[x-1] != -1) @afield[x+@sfield[1]] = (@afield[x+@sfield[1]]+1)%3 if ((x+@sfield[1]) < @sfield[0]*@sfield[1] && @afield[x+@sfield[1]] != -1) @afield[x-@sfield[1]] = (@afield[x-@sfield[1]]+1)%3 if ((x-@sfield[1]) >= 0 && @afield[x-@sfield[1]] != -1) @gpoints.text = fand(@afield) (0...(@sfield[0]*@sfield[1])).each{|i| @bfield[i].text=@afield[i]; @bfield[i].background=gcol(@afield[i])} return 0 else return 0 end end def initialize @sfield = $grea @afield = [] @bfield = [] @ffield = [] root = TkRoot.new { title "Colors" } (0..@sfield[0]).each{|i| @ffield.push(TkFrame.new(root))} (0...(@sfield[0]*@sfield[1])).each{|i| @afield.push(rand(4)-1)} j=(-1) (0...(@sfield[0]*@sfield[1])).each do |i| j+=1 if i % @sfield[1]==0 t=@afield[i] c=gcol(t) @bfield.push(TkButton.new(@ffield[j]){text t; background c; width 3; pack 'side'=>'left'}) @bfield[i].command{gcalc i} end (0..@sfield[0]).each{|i| @ffield[i].pack} gp = fand(@afield) @gpoints = TkLabel.new(root){text gp; pack 'side'=>'right'} end end $grea = [5,6] ($grea[0] = ARGV[0].to_i; $grea[1] = ARGV[1].to_i) if ARGV.length == 2 GColors.new Tk.mainloop
0hc.net    © 2001-2014 Harald Wolfsgruber