| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.cb.jset.CardStack |
|
|
| 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 | 2 | private final Stack _cardStack = new Stack(); |
| 33 | ||
| 34 | public CardStack() |
|
| 35 | 2 | { |
| 36 | 2 | _cardStack.setSize(CardProperties.CARDSNB); |
| 37 | 2 | } |
| 38 | ||
| 39 | public void shuffle() |
|
| 40 | { |
|
| 41 | // create a stack of shuffle cards |
|
| 42 | 2 | final boolean[] usedIndexes = new class="keyword">boolean[CardProperties.CARDSNB]; |
| 43 | 164 | for (int i = 0; i < CardProperties.CARDSNB; i++) |
| 44 | { |
|
| 45 | 162 | int randIdx = (class="keyword">int) (Math.random() * CardProperties.CARDSNB); |
| 46 | // skip used cards |
|
| 47 | 867 | while (usedIndexes[randIdx] == true) |
| 48 | { |
|
| 49 | 705 | randIdx++; |
| 50 | 705 | if (randIdx == CardProperties.CARDSNB) |
| 51 | { |
|
| 52 | 12 | randIdx = 0; |
| 53 | } |
|
| 54 | } |
|
| 55 | 162 | usedIndexes[randIdx] = true; |
| 56 | 162 | final CardProperties prop = CardProperties.getCard(i); |
| 57 | 162 | org.cb.util.Assert.assertTrue(randIdx < CardProperties.CARDSNB && randIdx >= 0, "Wrong Idx=" + randIdx); |
| 58 | 162 | _cardStack.set(randIdx, prop); |
| 59 | } |
|
| 60 | 2 | } |
| 61 | ||
| 62 | public CardProperties[] pop(final int nbCardsToPop) |
|
| 63 | { |
|
| 64 | 45 | final CardProperties[] cardsToPop = new CardProperties[nbCardsToPop]; |
| 65 | 207 | for (int i = 0; i < nbCardsToPop; i++) |
| 66 | { |
|
| 67 | 162 | cardsToPop[i] = (CardProperties) _cardStack.pop(); |
| 68 | } |
|
| 69 | 45 | return cardsToPop; |
| 70 | } |
|
| 71 | ||
| 72 | public boolean empty() |
|
| 73 | { |
|
| 74 | 0 | return _cardStack.empty(); |
| 75 | } |
|
| 76 | ||
| 77 | public int size() |
|
| 78 | { |
|
| 79 | 46 | return _cardStack.size(); |
| 80 | } |
|
| 81 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |