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
This commit is contained in:
Mike Thomas
2008-10-17 23:33:58 +00:00
parent 57cefe5619
commit 8b737a10fd
@@ -23,6 +23,7 @@ package com.threerings.parlor.card.data;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random;
import com.threerings.util.StreamableArrayList; import com.threerings.util.StreamableArrayList;
@@ -40,7 +41,7 @@ public class Deck extends StreamableArrayList<Card>
{ {
reset(false); reset(false);
} }
/** /**
* Constructor. * Constructor.
* *
@@ -51,7 +52,7 @@ public class Deck extends StreamableArrayList<Card>
{ {
reset(includeJokers); reset(includeJokers);
} }
/** /**
* Resets the deck to its initial state: an unshuffled deck of * Resets the deck to its initial state: an unshuffled deck of
* 52 or 54 cards, depending on whether the jokers are included. * 52 or 54 cards, depending on whether the jokers are included.
@@ -62,19 +63,19 @@ public class Deck extends StreamableArrayList<Card>
public void reset (boolean includeJokers) public void reset (boolean includeJokers)
{ {
clear(); clear();
for (int i = SPADES; i <= DIAMONDS; i++) { for (int i = SPADES; i <= DIAMONDS; i++) {
for (int j = 2; j <= ACE; j++) { for (int j = 2; j <= ACE; j++) {
add(new Card(j, i)); add(new Card(j, i));
} }
} }
if (includeJokers) { if (includeJokers) {
add(new Card(RED_JOKER, 3)); add(new Card(RED_JOKER, 3));
add(new Card(BLACK_JOKER, 3)); add(new Card(BLACK_JOKER, 3));
} }
} }
/** /**
* Shuffles the deck. * Shuffles the deck.
*/ */
@@ -82,7 +83,15 @@ public class Deck extends StreamableArrayList<Card>
{ {
Collections.shuffle(this); Collections.shuffle(this);
} }
/**
* Shuffles the deck.
*/
public void shuffle (Random r)
{
Collections.shuffle(this, r);
}
/** /**
* Deals a hand of cards from the deck. * Deals a hand of cards from the deck.
* *
@@ -95,19 +104,19 @@ public class Deck extends StreamableArrayList<Card>
int dsize = size(); int dsize = size();
if (dsize < size) { if (dsize < size) {
return null; return null;
} else { } else {
Hand hand = new Hand(); Hand hand = new Hand();
// use a sublist view to manipulate the top of the deck // use a sublist view to manipulate the top of the deck
List<Card> sublist = subList(dsize - size, dsize); List<Card> sublist = subList(dsize - size, dsize);
hand.addAll(sublist); hand.addAll(sublist);
sublist.clear(); sublist.clear();
return hand; return hand;
} }
} }
/** /**
* Returns a hand of cards to the deck. * Returns a hand of cards to the deck.
* *