Skip to content

Instantly share code, notes, and snippets.

@amasses
Created December 10, 2010 05:46
Show Gist options
  • Save amasses/735838 to your computer and use it in GitHub Desktop.
Save amasses/735838 to your computer and use it in GitHub Desktop.
For http://www.engineyard.com/blog/2010/engine-yard-lucky-13-contest/ - I know it said as fewer lines as possible, but I'm just bored and procrastinating so... enjoy :)
require 'httparty'
require 'json'
WHEEL_URL = "http://roulette.engineyard.com"
LUCKY_NUMBER = 13 #or not so lucky
PAYOUT_FACTOR = 35
def spin_the_wheel(bet)
spin = HTTParty.get(WHEEL_URL)
begin #lazyness
result = JSON.parse(spin.body)
spin_result = result["winning_number"].to_i
if spin_result and spin_result == LUCKY_NUMBER
winnings = bet * PAYOUT_FACTOR
puts "You scored! Winnings: $#{winnings}"
return winnings
else
puts "Winning number was #{spin_result}, you win $0"
return 0
end
rescue
puts "The roulette wheel jammed up, everyone gets a refund :("
return bet
end
end
wallet = 10000
spend = 0
bet = 100
winnings = []
(wallet / bet).times do
spend += bet
winnings << spin_the_wheel(bet)
end
total_winnings = winnings.reduce(:+)
puts "In total you won $#{total_winnings} out of the $#{wallet} you bet... you now have $#{(wallet - spend) + total_winnings}"
@amasses
Copy link
Author

amasses commented Dec 10, 2010

Ah.... I need to name my methods properly.... odds(bet) should read spin(bet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment