Guest User

Untitled

a guest
Mar 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. ## Fichier de classes
  2. def gets_answer_of(type, genre = '')
  3. "Quel#{genre} #{type} voulez-vous ?"
  4. end
  5.  
  6. class Calcul
  7. def initialize # niveau de difficulté
  8. @num1, @num2 = rand(9*$ndd).succ,rand(9*$ndd).succ
  9. eval "@resultat = @num1 #{$operation.to_s} @num2"
  10. end
  11. def show
  12. "#{@num1} #{$operation.to_s} #{@num2} ?"
  13. end
  14. def juste?(rendu)
  15. rendu.to_i == @resultat ? true : juste?(rendu)
  16. end
  17. end
  18.  
  19. class Operation
  20.  
  21. def initialize(operation)
  22. $operation = gets.chomp.to_sym
  23. [:*,:/,:-,:+,:**].include?($operation) ? true : ask_operation
  24. end
  25. end
  26.  
  27. class Difficulte
  28. def initialize
  29. $ndd = gets_answer_of("niveau de difficulté").to_i
  30. end
  31. end
  32.  
  33. ## Fichier de GUI
  34. require 'gtk2'
  35. Gtk.init
  36.  
  37. $operation, $ndd = :*,1
  38. calcul = Calcul.new
  39.  
  40.  
  41. box = _box :v
  42. label = _label calcul.show
  43. entry = _entry do |e|
  44. calcul = Calcul.new if calcul.juste?(e)
  45. end
  46.  
  47. box.pack_start label
  48. box.pack_start entry
  49.  
  50. @window = _window
  51.  
  52. @window.add box
  53. @window.show_all
  54.  
  55. Gtk.main
  56.  
  57. ## Fichier d'aide
  58. def _window
  59. window = Gtk::Window.new
  60. window.signal_connect('destroy') {Gtk.main_quit}
  61. window
  62. end
  63.  
  64. def _box(type, bool = true, number = 6)
  65. eval "Gtk::#{type.to_s.capitalize}Box.new(bool,number)"
  66. end
  67.  
  68. def _button(params = {:text => "", :type => :click})
  69. # Assignation des différentes valeurs
  70.  
  71. signal_type = params[:type] == :check ? :toggle : params[:type]
  72. signal = "#{signal_type.to_s}#{'e' if signal_type.to_s[-1,1] != 'e'}d"
  73. text = if params[:text] == :cancel then Gtk::Stock::CANCEL
  74. elsif params[:text] == :ok then Gtk::Stock::OK
  75. else params[:text]
  76. end
  77. button, type = "", (params[:type] == :click ? "" : params[:type])
  78. # Création du bouton
  79. eval "button = Gtk::#{type.to_s.capitalize}Button.new text"
  80. eval "button.#{params[:statut]} = true" if params[:statut]
  81. # Action s'il est pris en compte (voir section 1 pour voir quel section)
  82. button.signal_connect(signal) do
  83. yield
  84. end
  85. button
  86. end
  87.  
  88. def _label(text)
  89. Gtk::Label.new text
  90. end
  91.  
  92. def _table(x,y,*rows)
  93. table = Gtk::Table.new x,y
  94. rows.each do |r| # row
  95. r[:o] = (
  96. :shrink ? Gtk::SHRINK : (
  97. :expand ? Gtk::EXPAND : (
  98. :fill ? Gtk::FILL : Gtk::FILL)))
  99. # Objet, Positions, Options de mise en forme
  100. table.attach r[:r], r[:g], r[:d], r[:h], r[:b], r[:o], r[:o]
  101. end
  102. table
  103. end
  104.  
  105. def _message_dialog(text, window, params = {:type => :info, :button => :close})
  106. message_dialog = true
  107. eval "message_dialog = Gtk::MessageDialog.new(window, Gtk::Dialog::DESTROY_WITH_PARENT,
  108. Gtk::MessageDialog::#{params[:type].to_s.upcase},
  109. Gtk::MessageDialog::BUTTONS_#{params[:button].to_s.upcase}, text)"
  110. message_dialog.run
  111. message_dialog.destroy
  112. end
  113.  
  114. def _entry(params = {})
  115. entry = Gtk::Entry.new
  116. params.each do |k,v|
  117. eval "entry.#{k.to_s} = #{v.int? ? v : v.to_s}"
  118. end
  119. entry.signal_connect('activate') do
  120. yield entry.text
  121. end
  122. entry
  123. end
Add Comment
Please, Sign In to add comment