Advertisement
prprice16

Deck

Oct 14th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.security.SecureRandom;
  2. import java.util.ArrayList;
  3.  
  4. public class Deck
  5. {
  6.     //a Deck is an array of Card objects
  7.     private Card[] deck;
  8.     //index of next card to "deal" (0 - 51)
  9.     private int currentCard;
  10.     //number of cards in deck
  11.     private static final int NUMBER_OF_CARDS = 52;
  12.     //names of all the image files
  13.     private static final String[] pics =
  14.         {"ac.gif", "2c.gif", "3c.gif", "4c.gif", "5c.gif","6c.gif", "7c.gif",
  15.          "8c.gif", "9c.gif", "tc.gif", "jc.gif", "qc.gif", "kc.gif",
  16.          "ad.gif", "2d.gif", "3d.gif", "4d.gif", "5d.gif","6d.gif", "7d.gif",
  17.          "8d.gif", "9d.gif", "td.gif", "jd.gif", "qd.gif", "kd.gif",
  18.          "ah.gif", "2h.gif", "3h.gif", "4h.gif", "5h.gif","6h.gif", "7h.gif",
  19.          "8h.gif", "9h.gif", "th.gif", "jh.gif", "qh.gif", "kh.gif",
  20.          "as.gif", "2s.gif", "3s.gif", "4s.gif", "5s.gif","6s.gif", "7s.gif",
  21.          "8s.gif", "9s.gif", "ts.gif", "js.gif", "qs.gif", "ks.gif"        
  22.         };
  23.        // constructor fills deck of Cards
  24.        public Deck()
  25.        {
  26.            int[] values = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
  27.            String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
  28.              "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
  29.            String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
  30.  
  31.            //create array of Card objects
  32.            deck = new Card[NUMBER_OF_CARDS];
  33.            //start with position 0
  34.            currentCard = 0;
  35.  
  36.            //folder for pics is cards
  37.            //String base = "cards\\";
  38.            //String base = "cards" + java.io.File.separator;
  39.            // populate deck with Card objects
  40.            for (int count = 0; count < deck.length; count++)
  41.            {
  42.                String picname = "cards" + java.io.File.separator + pics[count];
  43.                deck[count] = new Card(faces[count % 13], suits[count / 13], values[count%13], picname);            
  44.            }
  45.          
  46.        }
  47.  
  48.        // shuffle deck of Cards with one-pass algorithm
  49.        public void shuffle()
  50.        {
  51.           SecureRandom randomNumbers = new SecureRandom();
  52.  
  53.           // for each Card, pick another random Card (0-51) and swap them
  54.           for (int first = 0; first < deck.length; first++)
  55.           {
  56.              // select a random number between 0 and 51
  57.              int second =  randomNumbers.nextInt(NUMBER_OF_CARDS);        
  58.              // swap current Card with randomly selected Card
  59.              Card temp = deck[first];        
  60.              deck[first] = deck[second];  
  61.              deck[second] = temp;            
  62.           }
  63.           // next call to method dealCard should start at deck[0] again
  64.           currentCard = 0;
  65.        }
  66.  
  67.        // deal one Card
  68.        public Card dealCard()
  69.        {
  70.           // determine whether Cards remain to be dealt
  71.           if (currentCard == deck.length)
  72.           {
  73.               //reshuffle deck
  74.               shuffle();
  75.           }
  76.          
  77.           int dealme = currentCard;
  78.           currentCard++;
  79.           // return current Card in array
  80.           return deck[dealme];                  
  81.        }        
  82.  
  83. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement