Skip to content

Instantly share code, notes, and snippets.

@aarongreenlee
Created January 17, 2011 23:24
Show Gist options
  • Save aarongreenlee/783689 to your computer and use it in GitHub Desktop.
Save aarongreenlee/783689 to your computer and use it in GitHub Desktop.
Learning Ruby
# Aaron Greenlee (http://aarongreenlee.com)
# Learning Ruby exercise. It's fun.
MAX_NUMBER = 100
MIN_NUMBER = 1
ALLOWED_ATTEMPTS = 10
MESSAGE_BREAK_POINT = ALLOWED_ATTEMPTS / 2
CONTINUATION_INPUT_ANSWERS = ['yes','no','y','n','ok','sure','fo sizzle']
QUIT_ANSWERS = ['n','no']
CLUE_ANSWERS = ['clue']
timers = []
playCount = 0
guessCount = 0
# Boot it up!
print "
_______ __ __ _______ _______. _______.
/ _____|| | | | | ____| / | / |
| | __ | | | | | |__ | (----` | (----`
| | |_ | | | | | | __| \\ \\ \\ \\
| |__| | | `--' | | |____.----) | .----) |
\\______| \\______/ |_______|_______/ |_______/
_______ ___
| \\ / \\
| .--. | / ^ \\
| | | | / /_\\ \\
| '--' | / _____ \\
|_______/ /__/ \\__\\
.__ __. __ __ .___ ___. .______ ___
| \\ | | | | | | | \\/ | | _ \\ / \\
| \\| | | | | | | \\ / | | |_) | / ^ \\
| . ` | | | | | | |\\/| | | _ < / /_\\ \\
| |\\ | | `--' | | | | | | |_) | / _____ \\
|__| \\__| \\______/ |__| |__| |______/ /__/ \\__\\"
puts "\n\nWhat's up dog? What be yo name?"
print "Enter your name: "
name = gets.chomp
puts "\nHello #{name}! We be guessing at a numba b'tweet #{MIN_NUMBER} and #{MAX_NUMBER}."
puts "\n\nYouz canz guess dat numba. I tellz you if you be HIGH or LOW."
sleep(1)
puts "\n\nOh yeah. I forgot."
puts "You will only get #{ALLOWED_ATTEMPTS} guesses. Hope you got a brain!"
sleep(1)
puts "\n\nIf you wanna get weak, guess 'clue'."
#
# Game Loop
#
loop do
# --------------------------------------------------------------------------------------------------------------------
# Ask to play again if this is not the first time
if (playCount == 0)
puts "\n\nROUND 1\n\n"
else
# Sleep to get attention.
sleep(1)
# Ask to play...
loop do
puts "\n\nDo you want to play again?"
answer = gets.chomp.downcase
if (!CONTINUATION_INPUT_ANSWERS.include?(answer))
puts "\nCome on. You can't expect us to understand that."
puts "How about you try something like this: '" + CONTINUATION_INPUT_ANSWERS.join("' or '") + '"?'
else
if QUIT_ANSWERS.include?(answer)
puts "Thanks for playing..."
avgtime = timers.inject{ |sum, timer| sum + timer } / timers.size
puts "It took you an average of #{avgtime} seconds each round."
sleep(1)
# Quit
Process.exit!;
else
puts "\n\nROUND #{playCount + 1}\n\n"
break
end
end
end
end
# --------------------------------------------------------------------------------------------------------------------
# Prepare for a game!
beatTheGame = false
playCount += 1
guessCount = 0
computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
timer = Time.now
loop do
print "\nWhat be yo guess? "
guess = gets.chomp
guessCount+=1
if (guessCount == ALLOWED_ATTEMPTS)
break
end
if (guess.downcase == 'clue')
puts "Ok. Here comes a little help."
if (computers_number > 9)
puts "X" + computers_number.to_s.split(//).last
else
puts "The numba is less than ten."
end
else
guessint = guess.to_i
# Numeric answers
if (guessint > computers_number)
puts guessCount > MESSAGE_BREAK_POINT ? "To damn high! You only have #{ALLOWED_ATTEMPTS - guessCount} guess left!" : "To damn high."
elsif (guessint < computers_number)
puts guessCount > MESSAGE_BREAK_POINT ? "To freak'n low! You only have #{ALLOWED_ATTEMPTS - guessCount} guess left!" : "To low."
else
beatTheGame = true
sleep(1)
puts "\nThe number was #{computers_number}!"
break
end
end
end
# Save our time...
timers << (Time.now - timer).round
# Provide feedback to the user on their level of game
if (beatTheGame)
print "
____ ____ ______ __ __ ____ __ ____ __ .__ __. __
\\ \\ / / / __ \\ | | | | \\ \\ / \\ / / | | | \\ | | | |
\\ \\/ / | | | | | | | | \\ \\/ \\/ / | | | \\| | | |
\\_ _/ | | | | | | | | \\ / | | | . ` | | |
| | | `--' | | `--' | \\ /\\ / | | | |\\ | |__|
|__| \\______/ \\______/ \\__/ \\__/ |__| |__| \\__| (__)"
puts ""
puts ""
puts "Peace out. You are apart of the gang fo'eva."
else
puts "\nThe number was #{computers_number}!"
end
puts "\nIt took you #{timers.last} seconds that round!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment