Source of DeckOfCards.java


  1: // Fig. 7.10: DeckOfCards.java
  2: // DeckOfCards class represents a deck of playing cards.
  3: import java.util.Random;
  4: 
  5: public class DeckOfCards
  6: {
  7:    private Card deck[]; // array of Card objects
  8:    private int currentCard; // index of next Card to be dealt
  9:    private final int NUMBER_OF_CARDS = 52; // constant number of Cards
 10:    private Random randomNumbers; // random number generator
 11: 
 12:    // constructor fills deck of Cards
 13:    public DeckOfCards()
 14:    {
 15:       String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", 
 16:          "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
 17:       String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
 18: 
 19:       deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
 20:       currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
 21:       randomNumbers = new Random(); // create random number generator
 22: 
 23:       // populate deck with Card objects
 24:       for ( int count = 0; count < deck.length; count++ ) 
 25:          deck[ count ] = 
 26:             new Card( faces[ count % 13 ], suits[ count / 13 ] );
 27:    } // end DeckOfCards constructor
 28: 
 29:    // shuffle deck of Cards with one-pass algorithm
 30:    public void shuffle()
 31:    {
 32:       // after shuffling, dealing should start at deck[ 0 ] again
 33:       currentCard = 0; // reinitialize currentCard
 34: 
 35:       // for each Card, pick another random Card and swap them
 36:       for ( int first = 0; first < deck.length; first++ ) 
 37:       {
 38:          // select a random number between 0 and 51 
 39:          int second =  randomNumbers.nextInt( NUMBER_OF_CARDS );
 40:          
 41:          // swap current Card with randomly selected Card
 42:          Card temp = deck[ first ];        
 43:          deck[ first ] = deck[ second ];   
 44:          deck[ second ] = temp;            
 45:       } // end for
 46:    } // end method shuffle
 47: 
 48:    // deal one Card
 49:    public Card dealCard()
 50:    {
 51:       // determine whether Cards remain to be dealt
 52:       if ( currentCard < deck.length )
 53:          return deck[ currentCard++ ]; // return current Card in array
 54:       else        
 55:          return null; // return null to indicate that all Cards were dealt
 56:    } // end method dealCard
 57: } // end class DeckOfCards
 58: 
 59: 
 60: /**************************************************************************
 61:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 62:  * Pearson Education, Inc. All Rights Reserved.                           *
 63:  *                                                                        *
 64:  * DISCLAIMER: The authors and publisher of this book have used their     *
 65:  * best efforts in preparing the book. These efforts include the          *
 66:  * development, research, and testing of the theories and programs        *
 67:  * to determine their effectiveness. The authors and publisher make       *
 68:  * no warranty of any kind, expressed or implied, with regard to these    *
 69:  * programs or to the documentation contained in these books. The authors *
 70:  * and publisher shall not be liable in any event for incidental or       *
 71:  * consequential damages in connection with, or arising out of, the       *
 72:  * furnishing, performance, or use of these programs.                     *
 73:  *************************************************************************/