Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Fichier de classes
- def gets_answer_of(type, genre = '')
- "Quel#{genre} #{type} voulez-vous ?"
- end
- class Calcul
- def initialize # niveau de difficulté
- @num1, @num2 = rand(9*$ndd).succ,rand(9*$ndd).succ
- eval "@resultat = @num1 #{$operation.to_s} @num2"
- end
- def show
- "#{@num1} #{$operation.to_s} #{@num2} ?"
- end
- def juste?(rendu)
- rendu.to_i == @resultat ? true : juste?(rendu)
- end
- end
- class Operation
- def initialize(operation)
- $operation = gets.chomp.to_sym
- [:*,:/,:-,:+,:**].include?($operation) ? true : ask_operation
- end
- end
- class Difficulte
- def initialize
- $ndd = gets_answer_of("niveau de difficulté").to_i
- end
- end
- ## Fichier de GUI
- require 'gtk2'
- Gtk.init
- $operation, $ndd = :*,1
- calcul = Calcul.new
- box = _box :v
- label = _label calcul.show
- entry = _entry do |e|
- calcul = Calcul.new if calcul.juste?(e)
- end
- box.pack_start label
- box.pack_start entry
- @window = _window
- @window.add box
- @window.show_all
- Gtk.main
- ## Fichier d'aide
- def _window
- window = Gtk::Window.new
- window.signal_connect('destroy') {Gtk.main_quit}
- window
- end
- def _box(type, bool = true, number = 6)
- eval "Gtk::#{type.to_s.capitalize}Box.new(bool,number)"
- end
- def _button(params = {:text => "", :type => :click})
- # Assignation des différentes valeurs
- signal_type = params[:type] == :check ? :toggle : params[:type]
- signal = "#{signal_type.to_s}#{'e' if signal_type.to_s[-1,1] != 'e'}d"
- text = if params[:text] == :cancel then Gtk::Stock::CANCEL
- elsif params[:text] == :ok then Gtk::Stock::OK
- else params[:text]
- end
- button, type = "", (params[:type] == :click ? "" : params[:type])
- # Création du bouton
- eval "button = Gtk::#{type.to_s.capitalize}Button.new text"
- eval "button.#{params[:statut]} = true" if params[:statut]
- # Action s'il est pris en compte (voir section 1 pour voir quel section)
- button.signal_connect(signal) do
- yield
- end
- button
- end
- def _label(text)
- Gtk::Label.new text
- end
- def _table(x,y,*rows)
- table = Gtk::Table.new x,y
- rows.each do |r| # row
- r[:o] = (
- :shrink ? Gtk::SHRINK : (
- :expand ? Gtk::EXPAND : (
- :fill ? Gtk::FILL : Gtk::FILL)))
- # Objet, Positions, Options de mise en forme
- table.attach r[:r], r[:g], r[:d], r[:h], r[:b], r[:o], r[:o]
- end
- table
- end
- def _message_dialog(text, window, params = {:type => :info, :button => :close})
- message_dialog = true
- eval "message_dialog = Gtk::MessageDialog.new(window, Gtk::Dialog::DESTROY_WITH_PARENT,
- Gtk::MessageDialog::#{params[:type].to_s.upcase},
- Gtk::MessageDialog::BUTTONS_#{params[:button].to_s.upcase}, text)"
- message_dialog.run
- message_dialog.destroy
- end
- def _entry(params = {})
- entry = Gtk::Entry.new
- params.each do |k,v|
- eval "entry.#{k.to_s} = #{v.int? ? v : v.to_s}"
- end
- entry.signal_connect('activate') do
- yield entry.text
- end
- entry
- end
Add Comment
Please, Sign In to add comment