Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # Gavin Stark Psuedo Code
  2. # 1. We remember that a is 1
  3. # 2. We remember that b is 100
  4. # 3. We remember that z is 0
  5. # 4. We tell the user we are going to
  6. # guess a number between "a" and "b"
  7. # 5. User chooses number
  8. # 6. We remember our guess is (a+b) / 2
  9. # 7. Tell the user that we are guessing the number "guess"
  10. # 8. User chooses yes, no
  11. # 9. If user chooses yes
  12. # then say we got it in z guesses
  13. # game is over
  14.  
  15. # Loop until guess is correct
  16. # 10. If user chooses no
  17. # 11. Ask if number is higher or lower than the number "guess"
  18. # 12. If user chooses lower
  19. # 13. Make b equal to guess - 1
  20. # 14. go back to step 6
  21. # 15. If user chooses higher
  22. # 16. Make a equal to guess + 1
  23. # 17. go back to step 6
  24.  
  25. min = 1
  26. max = 100
  27. tries = 0
  28.  
  29. puts "Welcome. I will play a number game with you. Think of a number between one and one hundered. I will try to guess that number. If you are ready to play, press the 'enter' button."
  30. gets
  31.  
  32. loop do
  33. numberGuess = (min+max) / 2
  34.  
  35. puts "Is the number #{numberGuess}? If #{numberGuess} is the number you were thinking of, enter y for 'yes'. If it was not, enter 'l' for me to guess lower, 'h' for me to guess higher."
  36. userAnswer = gets.chomp
  37.  
  38. if userAnswer == "y"
  39. puts "I guessed right!"
  40. break
  41.  
  42. elsif userAnswer == "l"
  43. max = numberGuess - 1
  44.  
  45. elsif userAnswer == "h"
  46. min = numberGuess + 1
  47. end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement