Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created May 13, 2011 15:57
Show Gist options
  • Save brandon-beacher/970794 to your computer and use it in GitHub Desktop.
Save brandon-beacher/970794 to your computer and use it in GitHub Desktop.
require 'yaml'
record = []
play = true
output = File.new('records.yaml', 'r')
record = YAML.load(output.read)
output.close
puts "========= GuessIt 1.2 =========
Guess a number between 1 and 1000.
Current record is " + record.to_s + " guesses.
===============================
"
while play == true
x = rand(1000)
count = 1
print "What is your guess? "
guess = gets.to_i
while guess != x && play != false
if guess > x
print "That's too high. Guess again: "
count += 1
guess = gets.to_i
end
if guess < x
print "That's too low. Guess again: "
count += 1
guess = gets.to_i
else
break
end
end
record = [count]
output = File.new('records.yaml', 'a')
output.puts YAML.dump(record)
output.close
puts "Great! You guessed the number in " + count.to_s + " tries!"
print "Would you like to play again? (y/n)"
answer = gets.chomp!
if answer == 'y'
play = true
end
if answer == 'n'
play = false
break
end
end
puts "Goodnight."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment