Mrowqa

"Geuss what number I mean" in Ruby

Apr 23rd, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.71 KB | None | 0 0
  1. # Simple game "Guess what number I mean" in Ruby written by Mrowqa
  2.  
  3. class String
  4.     def is_integer? # add our test
  5.         self =~ /^[-+]?([0-9]*)?$/
  6.     end
  7. end
  8.  
  9. times = 0
  10. Random::srand Integer(Time.now)
  11. number = (Random::rand*100).to_i % 100 + 1 # 1..100
  12. puts "Type number from 1 to 100 range: "
  13.  
  14. begin
  15.     times+=1
  16.     input = gets[0..-2]
  17.     if not input.is_integer?
  18.         puts "Wrong! This is *NOT* number. Try one more time:"
  19.         times-=1
  20.         next
  21.     end
  22.     no=Integer(input)
  23.  
  24.     case
  25.         when no<1 || no>100
  26.             puts "#{no} isn't from given range!"
  27.             times-=1
  28.         when no<number
  29.             puts "#{no} is too small!"
  30.         when no>number
  31.             puts "#{no} is too big!"
  32.     end
  33. end while no!=number
  34.  
  35. puts "You guessed #{number} by #{times} time!"
Advertisement
Add Comment
Please, Sign In to add comment