Guest User

Untitled

a guest
Mar 21st, 2011
954
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2.  
  3. public class Deck{
  4.     private Card[] cards = new Card[52];
  5.  
  6.     //Constructor
  7.     public Deck(){
  8.         int i = 0;
  9.         for (short j=0; j<4; j++){
  10.             for (short k=0; k<13;k++){
  11.                 cards[i++] = new Card(k, j);   
  12.             }
  13.         }
  14.     }
  15.    
  16.     // Print entire deck in order
  17.     protected void printDeck(){
  18.         for(int i=0; i<cards.length;i++){
  19.             System.out.println(i+1 + ": " + cards[i].printCard());
  20.         }
  21.         System.out.println("\n");
  22.     }
  23.    
  24.     // Find card in deck in a linear fashion
  25.     // Use this method if deck is shuffled/random
  26.     protected int findCard(Card card){
  27.         for (int i=0;i<52;i++){
  28.             if (Card.sameCard(cards[i], card)){
  29.                 return i;
  30.             }
  31.         }
  32.         return -1;
  33.     }
  34.    
  35.     //return specified card from deck
  36.     protected Card getCard(int cardNum){
  37.         return cards[cardNum];
  38.     }
  39.    
  40.     protected void shuffle(){
  41.         int length = cards.length;
  42.         Random random = new Random();
  43.         //random.nextInt();
  44.         for (int i=0;i<length;i++){
  45.             int change = i + random.nextInt(length-i);
  46.             swapCards(i, change);
  47.         }
  48.     }
  49.    
  50.     protected void cutDeck(){
  51.         Deck tempDeck = new Deck();
  52.         Random random = new Random();
  53.         int cutNum = random.nextInt(52);
  54.         for (int i=0;i<cutNum;i++){
  55.             tempDeck.cards[i] = this.cards[52-cutNum+i];           
  56.         }
  57.         for (int j=0;j<52-cutNum;j++){
  58.             tempDeck.cards[j+cutNum] = this.cards[j];          
  59.         }
  60.         this.cards = tempDeck.cards;
  61.     }
  62.    
  63.     // Swap cards in array to 'shuffle' the deck.
  64.     private void swapCards(int i, int change){     
  65.         Card temp = cards[i];
  66.         cards[i] = cards[change];
  67.         cards[change] = temp;
  68.     }
  69. }
RAW Paste Data