fork download
  1. # -*- encoding: UTF-8 -*-
  2.  
  3.  
  4. module Yakumo_yukari
  5. class Count_limit
  6. attr_accessor :start , :limit , :add
  7. attr_accessor :add_plus
  8. attr_accessor :start_init
  9. attr_accessor :type
  10. attr_accessor :loopwait
  11. attr_accessor :loopwait_wait
  12. attr_accessor :loopwait_ex
  13. attr_accessor :loopwait_ex_wait
  14. attr_accessor :c
  15. attr_accessor :hs
  16. attr_accessor :msg # mijissou
  17. def initialize hs = Hash.new
  18. @hs = hs
  19. @start = hs[:start] || 0
  20. @limit = hs[:limit] || 0
  21. @add = hs[:add] || 1
  22. @add_plus = hs[:add_plus] || 0
  23. @loopwait = 0
  24. @loopwait_wait = hs[:loopwait] || 0
  25. @loopwait_ex = hs[:loopwait_ex] || []
  26. @loopwait_ex_wait = hs[:loopwait_ex_wait] || 0
  27. @type = hs[:type] || nil
  28. @msg = []
  29. @c = hs[:start_init] || @start
  30.  
  31. case @type
  32. when :loop_rev
  33. if @add < 0
  34. @start , @limit = @limit , @start
  35. end
  36. @func =->{ count_loop_rev }
  37. when :loop
  38. @func =->{ count_loop }
  39. when :count_limit
  40. @func =->{ count_limit }
  41. else
  42. p :err
  43. exit
  44. end
  45. end
  46.  
  47. def call
  48. @func.call
  49. end
  50.  
  51. # 0...1...2...3...4...wait4...wait4...wait4...3...2...1...wait0...wait0
  52. def count_loop_rev
  53. if @loopwait > 1
  54. @loopwait -= 1
  55. return @c
  56. end
  57. @c += @add
  58. @add += @add_plus
  59. @add *= -1 if @c >= @limit || @c <= @start
  60. @c = @limit if @c >= @limit
  61. @c = @start if @c <= @start
  62. if (@c >= @limit) or (@c <= @start)
  63. @loopwait = @loopwait_wait
  64. end
  65. if @loopwait_ex.include? @c
  66. if @loopwait_ex.class == Hash
  67. @loopwait = @loopwait_ex[ @c ]
  68. else
  69. @loopwait = @loopwait_ex_wait
  70. end
  71. end
  72. return @c
  73. end
  74. end
  75.  
  76. class << self
  77.  
  78. def loop_rev_create hs = Hash.new
  79. Count_limit.new({ type: :loop_rev, }.merge( hs ))
  80. end
  81.  
  82. end
  83. end
  84.  
  85.  
  86.  
  87. a = Yakumo_yukari.loop_rev_create({
  88. start: 0 ,
  89. #start_init: 2 ,
  90. limit: 10 ,
  91. add: 1 ,
  92. #loopwait: 3 ,
  93. #loopwait_ex_wait: 3,
  94. loopwait_ex: ({ 5 => 2 , 7 => 4 }) ,
  95. #add_plus: 1 ,
  96. })
  97.  
  98. puts "----loop_rev loopwait_ex----"
  99. 40.times do |i|
  100. printf "%3d " % [ a.call ]
  101. end
  102.  
  103.  
Success #stdin #stdout 0s 4760KB
stdin
Standard input is empty
stdout
----loop_rev loopwait_ex----
  1    2    3    4    5    5    6    7    7    7    7    8    9   10    9    8    7    7    7    7    6    5    5    4    3    2    1    0    1    2    3    4    5    5    6    7    7    7    7    8