import java.lang.System; import java.util.Random; import Card; import Hand; import Player; import BuildStack; /* The deck is an array of cards with an index pointing to the last card on the deck. So, cards are dealt from the bottom of the deck:) An index of less than 0 indicates that the deck is empty. The deck is shuffled by generating random indexes and swapping their contents. */ public class Deck { private int index, size; Card deck[]; Random rand = new Random(); public Deck () /* Creates an unitialized, empty deck. The array is not allocated until "Fill" */ { index = -1; } public void Fill (int num_decks) /* Allocates the array and fills the deck with the appropriate cards to match a standard playing deck. index points at the last card on the deck. */ { int i; int numcards; int total; numcards = (num_decks * 54); index = numcards; deck = new Card[numcards]; total = numcards - (2 * num_decks); //all cards except jokers for (i=0; i < total; i++) { deck [i] = new Card (i%13 + 1); } while (i < numcards) { //adds in the jokers deck [i] = new Card (14); i++; } index = i - 1; } public boolean addCard (Card c) /* provides the mechanism to put more cards into the deck after the deck has been in use. If the deck is not full then the card is added and index is incremented to it always points at the last card on the deck. */ { if (index < (size - 1)) { deck [index++] = c; return true; } else return false; } public void Shuffle () /* Shuffles order of cards in the deck by randomly generating indexes and swapping the cards in the array. */ { Card temp; int i, index1, index2, num; for (i = 0; i <= index*5; i++) { num = rand.nextInt (); index1 = num % (index + 1); num = rand.nextInt (); index2 = num % (index + 1); if (index1 >= 0 && index2 >= 0) { temp = deck [index1]; deck [index1] = deck [index2]; deck [index2] = temp; } } } public boolean isEmpty () /* an index less than 0 indicates and empty deck. */ { return (index < 0); } public Card Deal () /* returns a card from the back of the deck and decrements the index. Returns a null card (card with zero use value) if the deck is empty. */ { Card temp = new Card(0); if (this.isEmpty ()) return temp; else temp = deck [index--]; return temp; } }