Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created January 22, 2018 15:05
Show Gist options
  • Save rodloboz/127176cdff8f9d2df01e195b74307f34 to your computer and use it in GitHub Desktop.
Save rodloboz/127176cdff8f9d2df01e195b74307f34 to your computer and use it in GitHub Desktop.
# Welcome user
# Display list of horses / roster [array]
# User picks a horse
# Randomly select winning horse (sample) / (shuffle)
# Determine if user won
# Display results
# Ask user if they want to play again
# 0 1 2 3
HORSES = ["Bronco", "Tweaker", "Michael Bolton", "Jack Sparrow", "John Wayne"]
puts "*****************************"
puts "*** ***"
puts "*** H O R S E S ***"
puts "*** ***"
puts "*****************************"
puts " Welcome to the racetrack!"
puts "\n\n"
loop do
puts "Pick a horse:"
HORSES.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
print "> "
choice = gets.chomp
# Need to ensure choice is between 0 and the last index of horses
until choice.match(/[1-#{HORSES.length}]/)
puts "Please pick a horse (1 to #{HORSES.length})"
print "> "
choice = gets.chomp
end
roster = HORSES.shuffle # returns a reshuffled array
# roster = HORSES.dup
3.times do
roster.shuffle!
puts "#{roster.first} is in the lead..."
sleep(1) # sleep stops program from running for 1 second
end
puts ""
puts "Race results:"
roster.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
if HORSES[choice.to_i - 1] == roster.first
puts "Congratulations! Your horse won!"
else
puts "Sorry, you lost!"
end
puts "DO you want to bet again?"
answer = gets.chomp
until answer.match(/y|n/i)
puts "Please pick (y) or (n)"
answer = gets.chomp
end
break if answer.downcase == "n" # || answer == "N"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment