Skip to content

Instantly share code, notes, and snippets.

@timlinquist
Created December 10, 2010 09:02
Show Gist options
  • Save timlinquist/735999 to your computer and use it in GitHub Desktop.
Save timlinquist/735999 to your computer and use it in GitHub Desktop.
Ruby Roulette wheel that places a bet on 13.
require 'rubygems'
require 'open-uri'
require 'json'
class Lucky13Payout
def initialize(location={:playing_in_usa=>true})
@slots_on_board= location[:playing_in_usa] ? 38 : 37
end
def bet_and_spin_wheel(wager)
spin_wheel == 13 ? wager * @slots_on_board : 0
end
def spin_wheel
JSON.parse(open('http://roulette.engineyard.com/')).read['winning_number'].to_i rescue 0
end
end
unless $test_run
puts 'Place your bet'
puts Lucky13Payout.new.bet_and_spin_wheel(gets.chomp.to_i)
end
require 'test/unit'
require 'rubygems'
require 'shoulda'
require 'mocha'
$test_run= true
require File.join(File.dirname(__FILE__), 'lucky_13.rb')
class Lucky13PayoutTest < Test::Unit::TestCase
context 'Rolling and calculating winnings' do
setup do
@payout= Lucky13Payout.new
@wager= 5
end
should 'not have any payout if 13 isn\'t landed on' do
@payout.stubs(:spin_wheel).returns(21)
assert_equal @payout.bet_and_spin_wheel(@wager), 0
end
should 'spin the wheel and return the winning number' do
assert_instance_of Fixnum, @payout.spin_wheel
end
context 'Winning spins on' do
context 'USA board' do
setup{ @payout.expects(:spin_wheel).returns(13) }
should 'payout 38 to 1 if we land on 13' do
payout= 190
assert_equal @payout.bet_and_spin_wheel(@wager), payout
end
end
context 'European or French board' do
setup do
@payout= Lucky13Payout.new(:playing_in_usa=>false)
@payout.expects(:spin_wheel).returns(13)
end
should 'payout 37 to 1 if we land on 13' do
payout=185
assert_equal @payout.bet_and_spin_wheel(@wager), payout
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment