Skip to content

Instantly share code, notes, and snippets.

@worthlesscog
Created May 7, 2012 17:51
Show Gist options
  • Save worthlesscog/2629297 to your computer and use it in GitHub Desktop.
Save worthlesscog/2629297 to your computer and use it in GitHub Desktop.
Chute and revised deck with Shuffle
trait Shuffle {
var cards: List[Card]
def next = {
if (!cards.isEmpty) {
val card = cards.head
cards = cards.tail
Some(card)
} else None
}
lazy val random = new scala.util.Random
def shuffle {
def shuffle(cards: List[Card], size: Int, pile: List[Card] = Nil): List[Card] = {
if (size > 0) {
val card = cards(random.nextInt(size))
val (bef, aft) = cards.span(_ != card)
shuffle(bef ::: aft.tail, size - 1, card :: pile)
} else pile
}
cards = shuffle(cards, cards.size)
}
}
class Deck extends Shuffle {
var cards = for (r ← Rank.values.toList; s ← Suit.values) yield r of s
}
class Chute(decks: Deck*) extends Shuffle {
var cards = decks.foldLeft(List[Card]())((l, d) ⇒ d.cards ::: l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment