View Javadoc

1   // START LICENSE
2   // JSet - a Java JSet card board game implementation
3   // Copyright (C) 2004 Jerome Lacoste
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or (at
8   // your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful, but
11  // WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  // END LICENSE
19  
20  package org.cb.jset;
21  
22  import java.util.Stack;
23  
24  /***
25   * FIXME document
26   * Maybe should be called CardDeck.
27   * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
28   * @version $Id: CardStack.java 78 2004-04-13 04:25:57Z jerome $
29   */
30  public class CardStack
31  {
32    private final Stack _cardStack = new Stack();
33  
34    public CardStack()
35    {
36      _cardStack.setSize(CardProperties.CARDSNB);
37    }
38  
39    public void shuffle()
40    {
41      // create a stack of shuffle cards
42      final boolean[] usedIndexes = new boolean[CardProperties.CARDSNB];
43      for (int i = 0; i < CardProperties.CARDSNB; i++)
44      {
45        int randIdx = (int) (Math.random() * CardProperties.CARDSNB);
46        // skip used cards
47        while (usedIndexes[randIdx] == true)
48        {
49          randIdx++;
50          if (randIdx == CardProperties.CARDSNB)
51          {
52            randIdx = 0;
53          }
54        }
55        usedIndexes[randIdx] = true;
56        final CardProperties prop = CardProperties.getCard(i);
57        org.cb.util.Assert.assertTrue(randIdx < CardProperties.CARDSNB && randIdx >= 0, "Wrong Idx=" + randIdx);
58        _cardStack.set(randIdx, prop);
59      }
60    }
61  
62    public CardProperties[] pop(final int nbCardsToPop)
63    {
64      final CardProperties[] cardsToPop = new CardProperties[nbCardsToPop];
65      for (int i = 0; i < nbCardsToPop; i++)
66      {
67        cardsToPop[i] = (CardProperties) _cardStack.pop();
68      }
69      return cardsToPop;
70    }
71  
72    public boolean empty()
73    {
74      return _cardStack.empty();
75    }
76  
77    public int size()
78    {
79      return _cardStack.size();
80    }
81  }