Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created April 23, 2018 15:11
Show Gist options
  • Select an option

  • Save rodloboz/f2f04e17ebd4fcfe16dab2d91e9567ef to your computer and use it in GitHub Desktop.

Select an option

Save rodloboz/f2f04e17ebd4fcfe16dab2d91e9567ef to your computer and use it in GitHub Desktop.
# USER STORIES
# - see a list of horses
# - place a bet
# - see the winning horse
# pseudo-code
# - initialize user waller with 100€
# - show user wallet
# - show/display a list of horses => [array]
# - as the user to pick a horse
# - store user choice
# (validate user choice)
# - place a bet
# select a random winner => shuffle // sample
# show winner to user (win or loose)
# if user won: wallet + 50
# if lost: - 10
# if wallet is 0 or less GAME OVER
# ask if user wants to continue
# user starts with 100€
# if he wins he gets 50€
# if he looses he loses 10€
require 'pry-byebug'
HORSES = [
"Paul", # 0
"Jack",
"Clover",
"Jumpy",
"Michael Bolton", # 4
"Jack Sparrow"
]
def welcome(message)
puts "#################################"
puts "### ###"
puts "### H O R S E S ###"
puts "### ###"
puts "#################################"
puts "\n"
puts "#{message}"
end
def show_horses
puts "There are the horses for this race."
puts "Please pick a horse:"
HORSES.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
gets.chomp.to_i - 1
end
# checking if user horse matches winning horse
# def race_outcome(user_choice, winning_horse)
# # winning_horse == user_choice ? puts "Congratulations! You won!" : puts "Sorry, you lost. Game over!"
# if winning_horse == user_choice
# puts "Congratulations! You won!"
# else
# puts "Sorry, you lost. Game over!"
# end
# end
def did_user_win?(user_choice, winning_horse)
winning_horse == user_choice
end
def race
puts "#{HORSES.sample} is in the lead..."
sleep(1)
puts "#{HORSES.sample} is in the lead..."
sleep(1)
puts "#{HORSES.sample} is in the lead..."
sleep(1)
end
# Game starts
balance = 100
welcome("Welcome to the race track!")
loop do
horse_index = show_horses
# get user choise and convert to an index (integer)
until (0...HORSES.length).include?(horse_index)
puts "Please pick a valid horse: (1 to #{HORSES.length})"
horse_index = gets.chomp.to_i - 1
end
puts "You picked: #{HORSES[horse_index]}"
race
winner = HORSES.sample
puts "#{winner} won the race!!!"
# user_choice winning_horse
# => "Clover" "Jumpy"
if did_user_win?(HORSES[horse_index], winner)
puts "Congratulations! You won!"
balance += 50
print "You won 50€."
else
puts "Sorry, you lost. Game over!"
balance += -10
print "You lost 10€."
end
puts "You now have #{balance}€"
break if balance <= 0
puts "Do you want to race again? [y][n]"
choice = gets.chomp
break unless choice == "y" || choice == "yes"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment