Advertisement
brilliant_moves

CardDeck.java

Nov 19th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.94 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class CardDeck {
  4.  
  5.     String[] faces = {
  6.         "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
  7.  
  8.     String[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
  9.  
  10.     Card[] cards = new Card[52];
  11.  
  12.     int max = cards.length;
  13.  
  14.     public CardDeck() {
  15.         int c = 0;
  16.         for (int s=0; s<suits.length; s++) {
  17.             for (int f=0; f<faces.length; f++) {
  18.                 cards[c++] = new Card(faces[f], suits[s]);
  19.             } // end for f
  20.         } // end for s
  21.     } // end default constructor
  22.  
  23.     public String toString() {
  24.         String theString = "";
  25.         for (int i=0; i<52; i++) {
  26.             theString += cards[i].toString()+"\n";
  27.         } // end for i
  28.         return theString;
  29.     } // end toString()
  30.  
  31.     public Card deal() {
  32.         Random r = new Random();
  33.         int i;
  34.  
  35.         i = r.nextInt(max);
  36.         Card temp = cards[i];
  37.         cards[i] = cards[--max];
  38.  
  39.         return temp;
  40.     } // end deal()
  41.  
  42.     public int getLength() {
  43.         return max;
  44.     } // end getLength()
  45. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement