Hand.java

/******   Hand  *********************************************************
*  A class that can be used by any game that requires hands of cards.   *
*  The cards each player holds can be simulated by an instance of Hand. *
*     Member functions: - Hand()                                        *
*                       - Hand(int)                                     *
*                       - setNext(int)                                  *
*                       - getNum()                                      *
*                       - getMax()                                      *
*                       - getCard(int)                                  *
*                       - removeCard(int)                               *
*                       - emptyHand()                                   *
*  Note: you might want to subclass this to add a sort method           *
************************************************************************/
                                                                             
public class Hand {

   /****   Constants  ***************************************************/

   static final int NO_CARD = -99999;

   /****   private member data  *****************************************
   *   cards - an array that holds the cards in the hand                *
   *   maxCards - the maximum number of cards a hand can hold           *
   *   numCards - the actual number of cards currently in the hand      *
   *********************************************************************/

   int cards[], maxCards, numCards;


   /****   Hand(int)  constructor  **************************************
   *  Allocates space for the number of cards you specify for the hand. *
   *  The hand is initially empty                                       *
   *********************************************************************/
   public Hand(int howMany) {

      maxCards = howMany;
      cards = new int[maxCards];
      numCards = 0;
   }


   /****  Hand  -  default constructor  *********************************
   *  Same as above except the default maximum is 5 cards               *
   *********************************************************************/
   public Hand() {

      maxCards = 5;
      cards = new int[maxCards];
      numCards = 0;
   }


   /****  setNext  ******************************************************
   *  Adds the card it is passed to the hand (if there is room)         *
   *********************************************************************/
   public void setNext(int card) {

      if (numCards < maxCards)
         cards[numCards++] = card;
   }


   /****  getNum  *******************************************************
   *  Returns the actual number of cards in the hand                    *
   *********************************************************************/
   public int getNum() {

      return numCards;
   }


   /****  getMax  *******************************************************
   *  Returns the maximum number of cards that the hand can hold        *
   *********************************************************************/
   public int getMax() {

      return maxCards;
   }


   /****  getCard  ******************************************************
   *  Returns the card that you ask for as long as there is such a card *
   *********************************************************************/
   public int getCard(int whichOne) {

      if (whichOne <= numCards && whichOne > 0)
         return cards[whichOne - 1];
      else
         return NO_CARD;
   }


   /****  removeCard  ***************************************************
   *  Takes the requested card out of the hand.  It is up to the user   *
   *  of the class to put the card back in the deck (using discard)     *
   *  or the card is just lost                                          *
   *********************************************************************/
   public void removeCard(int whichOne) {

      /***  Make sure the card exists  ***/
      if (whichOne <= numCards && whichOne >= 0)
         if (numCards == 1)
            numCards = 0;
         else {
            /***  More than one card means we have to move others up  ***/
            for (int i = whichOne; i < numCards-1 ; i++)
               cards[i] = cards[i + 1];

            numCards--;
         }
   }


   /****  emptyHand  ****************************************************
   *  Throw away all the cards in the hand.  (They do not go back into  *
   *  the deck.)                                                        *
   *********************************************************************/
   public void emptyHand() {

      numCards = 0;
   }


}