Last active
August 29, 2015 14:11
-
-
Save falonofthetower/92d0b4cbe717d52da6dd to your computer and use it in GitHub Desktop.
Extended Shuffle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Card | |
attr_accessor :value, :suit | |
def initialize(v, s) | |
@value = v | |
@suit = s | |
end | |
end | |
class Deck | |
def initialize | |
suits = 'Clubs', 'Diamonds', 'Hearts', 'Spades' | |
values = 'Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King' | |
@cards = suits.product(values) | |
20.times { shuffle } | |
end | |
def shuffle | |
riffle_shuffle | |
over_hand_shuffle | |
riffle_shuffle | |
riffle_shuffle | |
riffle_shuffle | |
over_hand_shuffle | |
cut | |
end | |
def cut | |
split_at = (((45..55).to_a.sample / 100.0) * @cards.count).to_i | |
left_hand = @cards.shift(split_at) | |
right_hand = @cards.shift(@cards.count) | |
@cards += right_hand | |
@cards += left_hand | |
end | |
def over_hand_shuffle | |
split_at = (((10..15).to_a.sample / 100.00) * @cards.count).to_i | |
left_hand = @cards.shift(split_at) | |
right_hand = @cards.shift(@cards.count) | |
while right_hand.count > 0 | |
left_hand.unshift(right_hand.shift([8,9,10,11,12,13,14,15].sample)) | |
end | |
@cards += left_hand | |
end | |
def riffle_shuffle | |
split_at = (((40..60).to_a.sample / 100.0) * @cards.count).to_i # split the deck somewhere between 40% and 60% | |
left_hand = @cards.shift(split_at) | |
right_hand = @cards.shift(@cards.count) | |
while left_hand.count > 0 || right_hand.count > 0 | |
if left_hand.count > 0 | |
case | |
when left_hand.count >= 3 | |
@cards += left_hand.shift([1, 2, 3].sample) | |
when left_hand.count == 2 | |
@cards += left_hand.shift([1, 2].sample) | |
when left_hand.count == 1 | |
@cards += left_hand.shift(1) | |
end | |
end | |
if right_hand.count > 0 | |
case | |
when right_hand.count >= 3 | |
@cards += right_hand.shift([1, 2, 3].sample) | |
when right_hand.count == 2 | |
@cards += right_hand.shift([1, 2].sample) | |
when right_hand.count == 1 | |
@cards += right_hand.shift(1) | |
end | |
end | |
end | |
end | |
end | |
deck = Deck.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment