Home

Archivos

Buscar

Categorías

Feeds:

RSS / Atom

Shoes - Generador de números· 12. February 2009, 01:50

Probando Shoes 2 y también Emacs

Shoes.app :title => "Numeros para la loteria",
  :width=>350, :height=>600 do
  background tan
  stack do
    para "Numero maximo"
    @max=edit_line()
    @max.text="55"
    para "Tamaño de grupo"
    @qty=edit_line()
    @qty.text="6"
    button("Go"){  
     calcula 
    }
  end
  @display=stack{ }
  def calcula
    max=@max.text.to_i # Numero maximo a elegir
    qty=@qty.text.to_i # Cantidad de numeros por combinacion
    #generamos los numeros con orden aleatorio
    x=Array.new(max) {|a| a+1}.sort_by{rand} 
    # hacemos grupos de QTY
    y=Array.new(max/qty+1) {|a| x[a*qty,qty]} 
    # rellenamos el ultimo grupo
    while y.last.length<qty do y.last.push(rand(max)+1).uniq! end 
    #Ordenamos cada grupo
    y.map!{|a| a.sort} 
    #Generamos resultado que se imprimira
    @result=""
    y.each {|a|
      a.each { |x| 
        @result+= "#{x}\t "
      } 
      @result+= "\n"
    }
    display_result
  end
  def display_result
    @display.clear do
      para "Juega estos numeros:"
      para @result
    end
  end
end