Skip to content

Instantly share code, notes, and snippets.

@TatyCat TatyCat/new_world.rb
Created Mar 13, 2019

Embed
What would you like to do?
ruby hw - New World
# Gavin Stark Psuedo Code
# 1. We remember that a is 1
# 2. We remember that b is 100
# 3. We remember that z is 0
# 4. We tell the user we are going to
# guess a number between "a" and "b"
# 5. User chooses number
# 6. We remember our guess is (a+b) / 2
# 7. Tell the user that we are guessing the number "guess"
# 8. User chooses yes, no
# 9. If user chooses yes
# then say we got it in z guesses
# game is over
# Loop until guess is correct
# 10. If user chooses no
# 11. Ask if number is higher or lower than the number "guess"
# 12. If user chooses lower
# 13. Make b equal to guess - 1
# 14. go back to step 6
# 15. If user chooses higher
# 16. Make a equal to guess + 1
# 17. go back to step 6
min = 1
max = 100
tries = 0
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."
gets
loop do
numberGuess = (min+max) / 2
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."
userAnswer = gets.chomp
if userAnswer == "y"
puts "I guessed right!"
break
elsif userAnswer == "l"
max = numberGuess - 1
elsif userAnswer == "h"
min = numberGuess + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.