Java

No description

fork
loading
Files
  • Main.java
  • card.java
  • play.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Deck {
   private Card[] deck = new Card[52];  //An array to hold 52 Cards 
   private int cardsUsed = 0;          // Indicates the number of cards that have been dealt
   
   // The constructor creates a deck of 52 cards with 4 suits number 1-4, and 13 cards number 1-13. 
   public Deck () {
      int cardCount = 0; 
      for (int suit = 1; suit <= 4; suit++) {
         for (int value = 1; value <= 13; value++) {
            deck[cardCount] = new Card(suit, value);
            cardCount++;
         }
      }
      shuffle();
   }
   
   // Shuffle the remaining cards in the deck
   public void shuffle() {
      for (int i = 51; i > cardsUsed; i--) {
         int rand = (int)(Math.random()*(i-cardsUsed)+cardsUsed);
         Card tmp = deck[i];
         deck[i] = deck[rand];
         deck[rand] = tmp;
      }
   }
   
   // Deal the next card. Null is returned if all cards are used. 
   public Card dealCard() {
      if (cardsUsed == 52)
         return null;
      Card nextCard = deck[cardsUsed];
      cardsUsed++;
      return nextCard;
   }
   
   // Returns the number of cards that have been used.  
   public int getCardsUsed() {
      return cardsUsed;
   }
   
   // See a specific card in the deck.  
   public Card viewCard(int cardNum) {
      return deck[cardNum];
   }
   
   // Reset to a full deck.
   public void resetDeck() {
      cardsUsed = 0;
      shuffle();
   }
}