Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  * The Deck class represents a shuffled deck of cards.
  6.  * It provides several operations including
  7.  *      initialize, shuffle, deal, and check if empty.
  8.  */
  9. public class Deck {
  10.  
  11.     /**
  12.      * cards contains all the cards in the deck.
  13.      */
  14.     private List<Card> cards;
  15.  
  16.     /**
  17.      * size is the number of not-yet-dealt cards.
  18.      * Cards are dealt from the top (highest index) down.
  19.      * The next card to be dealt is at size - 1.
  20.      */
  21.     private int size;
  22.  
  23.  
  24.     /**
  25.      * Creates a new <code>Deck</code> instance.<BR>
  26.      * It pairs each element of ranks with each element of suits,
  27.      * and produces one of the corresponding card.
  28.      * @param ranks is an array containing all of the card ranks.
  29.      * @param suits is an array containing all of the card suits.
  30.      * @param values is an array containing all of the card point values.
  31.      */
  32.     public Deck(String[] ranks, String[] suits, int[] values) {
  33.         cards = new ArrayList<Card>();
  34.         for (int j = 0; j < ranks.length; j++) {
  35.             for (String suitString : suits) {
  36.                 cards.add(new Card(ranks[j], suitString, values[j]));
  37.             }
  38.         }
  39.         size = cards.size();
  40.         shuffle(size);
  41.     }
  42.  
  43.  
  44.     /**
  45.      * Determines if this deck is empty (no undealt cards).
  46.      * @return true if this deck is empty, false otherwise.
  47.      */
  48.     public boolean isEmpty() {
  49.         return size == 0;
  50.     }
  51.  
  52.     /**
  53.      * Accesses the number of undealt cards in this deck.
  54.      * @return the number of undealt cards in this deck.
  55.      */
  56.     public int size() {
  57.         return size;
  58.     }
  59.  
  60.     /**
  61.      * Randomly permute the given collection of cards
  62.      * and reset the size to represent the entire deck.
  63.      */
  64.     public void shuffle(int size) {
  65.          size = 0;
  66. for(int k = cards.size()-1; k>0; k--) {
  67.            
  68.             int position = (int)(Math.random() * k);
  69.             Card cardshuffle = cards.remove(position);     
  70.             cards.add(k, cardshuffle);          //adds random cards back to deck at 'k' position
  71.                    
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Deals a card from this deck.
  77.      * @return the card just dealt, or null if all the cards have been
  78.      *         previously dealt.
  79.      */
  80.     public Card deal() {
  81.         if (isEmpty()) {
  82.             return null;
  83.         }
  84.         size--;
  85.         Card c = cards.get(size);
  86.         return c;
  87.     }
  88.  
  89.     /**
  90.      * Generates and returns a string representation of this deck.
  91.      * @return a string representation of this deck.
  92.      */
  93.     @Override
  94.     public String toString() {
  95.         String rtn = "size = " + size + "\nUndealt cards: \n";
  96.  
  97.         for (int k = size - 1; k >= 0; k--) {
  98.             rtn = rtn + cards.get(k);
  99.             if (k != 0) {
  100.                 rtn = rtn + ", ";
  101.             }
  102.             if ((size - k) % 2 == 0) {
  103.                 // Insert carriage returns so entire deck is visible on console.
  104.                 rtn = rtn + "\n";
  105.             }
  106.         }
  107.  
  108.         rtn = rtn + "\nDealt cards: \n";
  109.         for (int k = cards.size() - 1; k >= size; k--) {
  110.             rtn = rtn + cards.get(k);
  111.             if (k != size) {
  112.                 rtn = rtn + ", ";
  113.             }
  114.             if ((k - cards.size()) % 2 == 0) {
  115.                 // Insert carriage returns so entire deck is visible on console.
  116.                 rtn = rtn + "\n";
  117.             }
  118.         }
  119.  
  120.         rtn = rtn + "\n";
  121.         return rtn;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement