Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created April 15, 2019 11:26
Show Gist options
  • Save krokrob/df8e8c0cf1b0950266f312c74bc4731d to your computer and use it in GitHub Desktop.
Save krokrob/df8e8c0cf1b0950266f312c74bc4731d to your computer and use it in GitHub Desktop.
# interface.rb
# Pseudo-code:
# 1. Print welcome and the horses names
# display a welcome message
puts "Welcome to the horse race!"
# store horses names in an array
horses = [
'Grand Cardinal',
'Jolly Jumper',
'As de Trèfle',
'Bourriquet',
'Rockstar'
]
# Iterate on array and display each horse name:
horses.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
# 2. Get user's bet
puts "Who's the winner?"
# store the bet
number = gets.chomp.to_i # get horse number
index = number - 1 # get horse index
bet = horses[index] # get horse name
# check if horse exists
if (1..5).include?(number)
puts "#{bet} is ready!"
puts "Ok the race is going to start!"
# 3. Run the race 🐴
# initiate a loop of 4 iterations (laps)
lap_winner = ''
4.times do |lap|
shuffled_horses = horses.shuffle
# store lap winner
lap_winner = shuffled_horses.first
# Display first horse name
if lap == 3
puts "#{lap_winner} is the champion!"
else
puts "#{lap_winner} is beyond on lap #{lap + 1}"
end
sleep 2
end
# lap = 0
# while lap < 4
# # For each lap:
# lap += 1
# # Reorder horses names
# shuffled_horses = horses.shuffle
# # store lap winner
# lap_winner = shuffled_horses.first
# # Display first horse name
# if lap == 4
# puts "#{lap_winner} is the champion!"
# else
# puts "#{lap_winner} is beyond on lap #{lap}"
# end
# sleep 2
# end
# 4. Print results
# Compare user bet with last lap winner
if bet == lap_winner
puts "Congrats! You win!"
else
puts "Too bad..."
end
# Display the user result (win/loose)
else
puts "This horse is not running today"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment