/* @(#)CardDeck.java 1.0 7 February 2005 */ /* Useful imports */ package FinalProject; import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.codec.*; import edu.neu.ccs.console.*; import edu.neu.ccs.filter.*; import edu.neu.ccs.jpf.*; import edu.neu.ccs.parser.*; import edu.neu.ccs.pedagogy.*; import edu.neu.ccs.quick.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import java.util.*; import java.math.*; import java.beans.*; import java.lang.reflect.*; import java.net.*; import java.util.regex.*; public class CardDeck { // size of deck public static final int decksize = CardImages.decksize; // array of cards in deck private Card[] card = new Card[decksize]; private int[] permutation = null; /** Construct a shuffled card deck. */ public CardDeck() { this(true); } /** * Construct a card deck. * * If shuffle is true then shuffle the deck * otherwise return the deck with the cards in order. */ public CardDeck(boolean shuffle) { int index = 0; for (int suit = Card.HEARTS; suit <= Card.CLUBS; suit++) for (int rank = Card.ACE; rank <= Card.KING; rank++) { card[index] = new Card(rank, suit); index++; } if (shuffle) shuffle(); else placeInOrder(); } /** * Place the cards in the deck in order by suit * hearts, diamonds, spades, clubs; * within each suit, rank the cards from ace to * king. * * Resets the iterator cursor to the beginning * of the deck. */ public void placeInOrder() { iteratorIndex = 0; permutation = ProbStatTools.integerSequence(decksize); } /** * Shuffle the deck. * * Resets the iterator cursor to the beginning * of the deck. */ public void shuffle() { iteratorIndex = 0; permutation = ProbStatTools.randomPermutation(decksize); } /** * Return the card at the given index. * * If necessary, the index is forced into the range * from 0 to (decksize - 1). * * @param index the card index * @return the card at the given index in the current order */ public Card getCard(int index) { // error check index %= decksize; if (index < 0) index += decksize; return card[permutation[index]]; } // member data private int iteratorIndex = 0; /* * Moves the iterator cursor one step forward * unless it is beyond the end of the deck. * * After this call, the interator cursor may * be positioned beyond the end of the deck * and there may be no more cards to "deal". */ public void step() { if (hasCard()) iteratorIndex++; } /* * Returns the current Card if hasCard() is true * or else returns null. * * Does not move the iterator cursor. */ public Card currentCard() { if (hasCard()) return card[permutation[iteratorIndex]]; else return null; } /* * Tests whether there is a current Card to * return, that is, whether the iterator cursor * is still within the bounds of the deck. */ public boolean hasCard() { return cardsToDeal() > 0; } /** * Returns the number of cards still left to "deal" * in the deck by reading the index of the current * card and computing how many cards remain. */ public int cardsToDeal() { return decksize - iteratorIndex; } /* Reset the iterator cursor but do not shuffle. */ public void resetNoShuffle() { iteratorIndex = 0; } /** * Using the iterator, return the given number of * cards from the deck and advance the iterator * beyond these cards. * * Throw a run time exception if the given n is * less than 0 or greater than cardsToDeal(). */ public Card[] dealCards(int n) { if ((n < 0) || (n > cardsToDeal())) throw new RuntimeException("Cannot deal " + n + " cards"); Card[] cards = new Card[n]; for (int i = 0; i < n; i++) { cards[i] = currentCard(); step(); } return cards; } }