From: dblack@... Date: 2006-03-18T02:40:00+09:00 Subject: Re: Card tricks with Ruby 2 - The Riffle Hi -- On Sat, 18 Mar 2006, grrr wrote: > First of all I wish to humbly send my thanks everyone taking their time to > share their pearls of wisdom! You have taught me alot! Hehe my honorable > mention goes to James for the cards.sort_by{ rand } which is nice and > snappy. Also, I thought the 'deck = deck[x..-1] + deck[0...x]' was clear > to me, even if I am a ruby newb. Now lets move on.. > > On Fri, 17 Mar 2006 22:14:47 +0900, dblack wrote: >> I have to say, it only dawned on me just now that it's kind of funny to >> cut the deck when the dealer is a computer. The idea of cutting the deck >> is to thwart attempts by the dealer to stack the deck. I actually kind of >> love the idea that one has to do this in the case of a computer :-) > > Yes, I want the whole deal to be as realistic as possible! Down to > last detail! In real life cards are not really shuffled into random > order! > > And thus theres need for realistic Riffle! 8) > > Around here, before any card games, after the deck has been > cut into two by the dealer it is of course 'recombined' by taking the two > decks, and mixing them back into one deck, in a manouver called 'the > riffle'. That's different from the person next to the dealer cutting the deck, though, which happens after all the shuffling is over. > "A riffle, in which half of the deck is held in each hand with the thumbs > inward, then cards are released by the thumbs so that they fall to the > table intertwined." Thus *almost* every card other card is from the other > half of the deck, and vice versa - with the odd double, triple or > quadruple cards sometimes falling on the table from either pack! > > You can find a photo and detailed description of the riffle here: > http://en.wikipedia.org/wiki/Shuffle > > This is done three times, and finally one of the players > gets to cut the deck and the dealer then recombines it and deals out > cards. Ie. dealer does 'cut + riffle' three times, then player 'cuts', > then dealer riffles, and game can then begin. > > How would you code riffle? class Deck def riffle sort_by { rand } # just don't tell anyone :-) end end But seriously... here's a first attempt, maybe useful at least as a starting point for better: class Deck < Array def initialize concat([*1..52]) end def split # an even cut; you'd want a real cut too [self[0...26],self[26..52]] end def pure_riffle s = split s[0].zip(s[1]).flatten end def riffle(max_consec = 3) s,t = split done = [] until s.empty? || t.empty? done.push(s.shift,t.shift) x = if rand(2).zero? then s else t end if time_for_glitch? rand(max_consec + 1).times { done.push(x.shift) } end end done + s + t end def time_for_glitch? rand(5).zero? end end -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black