//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.parlor.card.data;

import java.util.Collections;
import java.util.List;

import com.threerings.util.StreamableArrayList;

/**
 * Instances of this class represent decks of cards.
 */
public class Deck extends StreamableArrayList<Card>
    implements CardCodes
{
    /**
     * Default constructor creates an unshuffled deck of cards without
     * jokers.
     */
    public Deck ()
    {
        reset(false);
    }
    
    /**
     * Constructor.
     *
     * @param includeJokers whether or not to include the two jokers
     * in the deck
     */
    public Deck (boolean includeJokers)
    {
        reset(includeJokers);
    }
    
    /**
     * Resets the deck to its initial state: an unshuffled deck of
     * 52 or 54 cards, depending on whether the jokers are included.
     *
     * @param includeJokers whether or not to include the two jokers
     * in the deck
     */
    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.
     */
    public void shuffle ()
    {
        Collections.shuffle(this);
    }
    
    /**
     * Deals a hand of cards from the deck.
     *
     * @param size the size of the hand to deal
     * @return the newly created and populated hand, or null
     * if there are not enough cards in the deck to deal the hand
     */
    public Hand dealHand (int size)
    {
        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<Card> sublist = subList(dsize - size, dsize);
            hand.addAll(sublist);
            sublist.clear();
            
            return hand;
        }
    }
    
    /**
     * Returns a hand of cards to the deck.
     *
     * @param hand the hand of cards to return
     */
    public void returnHand (Hand hand)
    {
        addAll(hand);
        hand.clear();
    }
}

@@ -1009,16 +1009,41 @@
 il.List;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -1372,37 +1372,33 @@
 t(false);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * C
@@ -1586,37 +1586,33 @@
 eJokers);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -1907,33 +1907,25 @@
    clear();%0A
-        
 %0A
+
         for 
@@ -2069,25 +2069,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -2191,37 +2191,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     /**%0A     * S
@@ -2312,28 +2312,152 @@
 his);%0A    %7D%0A
+%0A
     
+/**%0A     * Shuffles the deck.%0A     */%0A    public void shuffle (Random r)%0A    %7B%0A        Collections.shuffle(this, r);%0A    %7D%0A
 %0A    /**%0A   
@@ -2810,29 +2810,17 @@
 n null;%0A
-            
 %0A
+
         
@@ -2864,25 +2864,17 @@
 Hand();%0A
-        
 %0A
+
         
@@ -3063,21 +3063,9 @@
 ();%0A
-            
 %0A
+
     
@@ -3101,20 +3101,16 @@
 %7D%0A    %7D%0A
-    
 %0A    /**

Allow a particular random object to be specified when shuffling the deck.