1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }