-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
133 lines (123 loc) · 4.53 KB
/
Deck.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* An object of type Deck represents a deck of playing cards. The deck
* is a regular poker deck that contains 52 regular cards and that can
* also optionally include two Jokers.
*/
public class Deck {
/**
* An array of 52 or 54 cards. A 54-card deck contains two Jokers,
* in addition to the 52 cards of a regular poker deck.
*/
private Card[] deck;
/**
* Keeps track of the number of cards that have been dealt from
* the deck so far.
*/
private int cardsUsed=-1;
/**
* Constructs a regular 52-card poker deck. Initially, the cards
* are in a sorted order. The shuffle() method can be called to
* randomize the order. (Note that "new Deck()" is equivalent
* to "new Deck(false)".)
*/
public Deck() {
// ***(2) Fill in one line to call the other constructor for Deck
// Let's have the default be to NOT use Jokers.
this(false);
}
/**
* Constructs a poker deck of playing cards, The deck contains
* the usual 52 cards and can optionally contain two Jokers
* in addition, for a total of 54 cards. Initially the cards
* are in a sorted order. The shuffle() method can be called to
* randomize the order.
* @param includeJokers if true, two Jokers are included in the deck; if false,
* there are no Jokers in the deck.
*/
public Deck(boolean includeJokers) {
if (includeJokers)
deck = new Card[54];
else
deck = new Card[52];
int cardCt = 0; // How many cards have been created so far.
for(int currentSuit = 0; currentSuit <= 3; currentSuit++) {
for(int currentValue = 1; currentValue <= 13; currentValue++) {
deck[cardCt] = new Card(currentValue,currentSuit);
cardCt++;
}
}
if (includeJokers) {
deck[52] = new Card(1,Card.JOKER);
deck[53] = new Card(2,Card.JOKER);
}
cardsUsed = 0;
}
/**
* Put all the used cards back into the deck (if any), and
* shuffle the deck into a random order.
*/
public void shuffle() {
//***(3) Go through and place the Cards in 'deck' in a random order.
// Make sure every place in the Card[] 'deck'
// is still filled with Cards, however,
// to avoid a NullPointerException
Card[] temp= new Card[deck.length];
java.util.Random rand = new java.util.Random();
for(int i = 0; i < deck.length; i++) {
int randomIndex = rand.nextInt(deck.length);
while(temp[randomIndex] != null) {
randomIndex = rand.nextInt(deck.length);
}
temp[i] = deck[randomIndex];
}
deck = temp;
}
/**
* As cards are dealt from the deck, the number of cards left
* decreases. This function returns the number of cards that
* are still left in the deck. The return value would be
* 52 or 54 (depending on whether the deck includes Jokers)
* when the deck is first created or after the deck has been
* shuffled. It decreases by 1 each time the dealCard() method
* is called.
*/
public int cardsLeft() {
return deck.length - cardsUsed;
}
/**
* Removes the next card from the deck and return it. It is illegal
* to call this method if there are no more cards in the deck. You can
* check the number of cards remaining by calling the cardsLeft() function.
* @return the card which is removed from the deck.
* @throws IllegalStateException if there are no cards left in the deck
*/
public Card dealCard() {
if (cardsUsed == deck.length)
throw new IllegalStateException("No cards are left in the deck.");
return deck[cardsUsed++];
// Programming note: Cards are not literally removed from the array
// that represents the deck. We just keep track of how many cards
// have been used.
}
/**
* Test whether the deck contains Jokers.
* @return true, if this is a 54-card deck containing two jokers, or false if
* this is a 52 card deck that contains no jokers.
*/
public boolean hasJokers() {
return (deck.length == 54);
}
public String toString() {
String str = "";
for(int i = 0; i < deck.length; i++) {
str += deck[i].toString();
if(i == deck.length - 1)
str += ".";
else
str += ", ";
if(i > 0 && i % 4 == 0)
str += "\n";
}
return str;
}
} // end class Deck