Advertisement
oaktree

[Ruby] Guessing Game

Jun 16th, 2016
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.25 KB | None | 0 0
  1. def check_for_answer(ans)
  2.     guess = ans+1
  3.     while true
  4.         print "[*] Enter a guess... "
  5.         guess = gets.chomp.to_i
  6.  
  7.         if guess < ans
  8.             puts "  [*] Higher..."
  9.         elsif guess > ans
  10.             puts "  [*] Lower..."
  11.         else
  12.             puts "  [*] Correct!"
  13.             break
  14.         end
  15.     end
  16. end
  17.  
  18. def singleplayer
  19.     puts "[*] You have entered Single Player"
  20.     puts "\n[*] Now please enter the guessing range (i.e., from x to y [inclusive])"
  21.     print "separated by a comma. "
  22.     x,y = gets.chomp.split(',').map(&:to_i)
  23.    
  24.     check_for_answer(rand(x..y))   
  25. end
  26.  
  27. def multiplayer
  28.     puts "[*] You have entered Multiplayer."
  29.     puts "[*] One player should enter a number while the other looks away."
  30.     puts "[*] Enter answer: "
  31.    
  32.     answer = gets.chomp.to_i
  33.    
  34.     system("clear")
  35.    
  36.     puts "[*] Now bring in the guesser..."
  37.     puts "[*] Hey, guesser. The range is #{answer - rand(0..answer)} to #{answer + rand(1..answer)}"
  38.  
  39.     check_for_answer(answer)
  40. end
  41.  
  42. def main
  43.     puts "#----------------#"
  44.     puts "# Number Guesser #"
  45.     puts "#   by oaktree   #"
  46.     puts "#----------------#"
  47.     print "\n"*2
  48.  
  49.     puts "[*] Here are your options:"
  50.     puts "1. Single Player"
  51.     puts "2. Two Players"
  52.  
  53.     print "[*] Choice? [1/2] "
  54.     input = gets.chomp.to_i
  55.  
  56.     if input == 1
  57.         singleplayer
  58.     else
  59.         multiplayer
  60.     end
  61. end
  62.  
  63. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement