Created
May 7, 2012 17:51
-
-
Save worthlesscog/2629297 to your computer and use it in GitHub Desktop.
Chute and revised deck with Shuffle
This file contains 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
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