Advertisement
mmayoub

Solitaire, Deck

Jul 30th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class Deck {
  5.     private static Random rnd = new Random();
  6.  
  7.     Card[] cards;
  8.     int start;
  9.  
  10.     public Deck() {
  11.         int cardsCount = Card.CARD_TYPE.length() * Card.CARD_VALUE.length;
  12.         cards = new Card[cardsCount];
  13.  
  14.         for (int t = 0; t < Card.CARD_TYPE.length(); t++) {
  15.             char type = Card.CARD_TYPE.charAt(t);
  16.             for (int v = 0; v < Card.CARD_VALUE.length; v++) {
  17.                 cards[v + 13 * t] = new Card(type, v + 1);
  18.             }
  19.         }
  20.  
  21.         for (int i = 0; i < cardsCount; i++) {
  22.             int c1, c2;
  23.  
  24.             c1 = rnd.nextInt(cardsCount);
  25.             do {
  26.                 c2 = rnd.nextInt(cardsCount);
  27.             } while (c1 == c2);
  28.  
  29.             Card temp = cards[c1];
  30.             cards[c1] = cards[c2];
  31.             cards[c2] = temp;
  32.         }
  33.  
  34.         start = 0;
  35.     }
  36.  
  37.     public Card[] getCards(int count) {
  38.         int last = start + count;
  39.         Card[] res = Arrays.copyOfRange(cards, start, last);
  40.         start = last;
  41.  
  42.         return res;
  43.     }
  44.  
  45.     public int getCount() {
  46.         return cards.length - start;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement