From 8b737a10fdc8a9764541d232038a9037be1d94b2 Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Fri, 17 Oct 2008 23:33:58 +0000 Subject: [PATCH] Allow a particular random object to be specified when shuffling the deck. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@760 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../com/threerings/parlor/card/data/Deck.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/java/com/threerings/parlor/card/data/Deck.java b/src/java/com/threerings/parlor/card/data/Deck.java index 16faf840..60fd618d 100644 --- a/src/java/com/threerings/parlor/card/data/Deck.java +++ b/src/java/com/threerings/parlor/card/data/Deck.java @@ -23,6 +23,7 @@ package com.threerings.parlor.card.data; import java.util.Collections; import java.util.List; +import java.util.Random; import com.threerings.util.StreamableArrayList; @@ -40,7 +41,7 @@ public class Deck extends StreamableArrayList { reset(false); } - + /** * Constructor. * @@ -51,7 +52,7 @@ public class Deck extends StreamableArrayList { reset(includeJokers); } - + /** * Resets the deck to its initial state: an unshuffled deck of * 52 or 54 cards, depending on whether the jokers are included. @@ -62,19 +63,19 @@ public class Deck extends StreamableArrayList public void reset (boolean includeJokers) { clear(); - + for (int i = SPADES; i <= DIAMONDS; i++) { for (int j = 2; j <= ACE; j++) { add(new Card(j, i)); } } - + if (includeJokers) { add(new Card(RED_JOKER, 3)); add(new Card(BLACK_JOKER, 3)); } } - + /** * Shuffles the deck. */ @@ -82,7 +83,15 @@ public class Deck extends StreamableArrayList { Collections.shuffle(this); } - + + /** + * Shuffles the deck. + */ + public void shuffle (Random r) + { + Collections.shuffle(this, r); + } + /** * Deals a hand of cards from the deck. * @@ -95,19 +104,19 @@ public class Deck extends StreamableArrayList int dsize = size(); if (dsize < size) { return null; - + } else { Hand hand = new Hand(); - + // use a sublist view to manipulate the top of the deck List sublist = subList(dsize - size, dsize); hand.addAll(sublist); sublist.clear(); - + return hand; } } - + /** * Returns a hand of cards to the deck. *