-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCardCollection.java
243 lines (196 loc) · 5.5 KB
/
CardCollection.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
public class CardCollection implements Serializable {
private static final long serialVersionUID = 19645L;
/*
* CopyOnWriteArrayList is used for thread safety purposes as a normal ArrayList
* will throw ConcurrentModificationException if multiple threads try to modify it.
*/
private CopyOnWriteArrayList<Card> cards = new CopyOnWriteArrayList<>();
private int deckLimit;
private Boolean isLimited = false; // Indicates whether a limit on the number of cards in the collection has been set.
// ______________PUBLIC______________
// __Constructors__
public CardCollection() {}
public CardCollection(int numOfDecks) {
addDecks(numOfDecks);
}
public CardCollection(int numOfDecks, int deckLimit) {
this.deckLimit = deckLimit;
isLimited = true;
enforceTotalCardLimit(numOfDecks * Deck.DECK_SIZE);
addDecks(numOfDecks);
}
public CardCollection(CardCollection aCollec) {
addCardCollection(aCollec);
}
public CardCollection(CardCollection aCollec, int deckLimit) {
this.deckLimit = deckLimit;
isLimited = true;
enforceTotalCardLimit(aCollec.size());
addCardCollection(aCollec);
}
// __Methods__
/*
* Add a single card to the collection.
*/
public void addCard(Card card) {
if (isLimited) {
enforceTotalCardLimit(1);
}
cards.add(card);
}
/*
* Add all the cards in the passed collection into this collection.
*/
public void addCardCollection(CardCollection collection) {
if (isLimited) {
enforceTotalCardLimit(collection.size());
}
cards.addAll(collection.cards);
}
/*
* Add the given number of 52 card standard decks to the collection.
*/
public void addDecks(int numOfDecks) {
if (isLimited) {
enforceTotalCardLimit(numOfDecks * Deck.DECK_SIZE);
}
for(int i = 0; i < numOfDecks; ++i)
addDeck();
}
/*
* Shuffle the card collection.
*/
public void shuffle() {
Collections.shuffle(cards);
}
/*
* Print the names of all the cards in the collection on the console.
*/
public void printCollection() {
for (Card card : cards) {
System.out.println(card.getName());
}
}
/*
* Removes a random card from the collection and returns it.
*/
public Card drawRandomCard() {
int randomIndex = new Random().nextInt(this.cards.size());
return cards.remove(randomIndex);
}
/*
* Removes the highest score card from the collection and returns it.
*/
public Card drawMaxScoreCard(Suit selectedSuit) {
Card maxCard = null;
int maxValue = 0;
for(Card card : cards) {
int cardValue = card.getCardType().getCardValue();
// If the card's suit is the selected suit, then the
// card's score doubles.
if (card.getSuit() == selectedSuit)
cardValue *= 2;
if (cardValue > maxValue) {
maxCard = card;
maxValue = cardValue;
}
}
cards.remove(maxCard);
return maxCard;
}
/*
* Removes the lowest score card from the collection and returns it.
*/
public Card drawMinScoreCard(Suit selectedSuit) {
Card minCard = null;
int minValue = 30;
for(Card card : cards) {
int cardValue = card.getCardType().getCardValue();
if (card.getSuit() == selectedSuit)
cardValue *= 2;
if (cardValue < minValue) {
minCard = card;
minValue = cardValue;
}
}
cards.remove(minCard);
return minCard;
}
/*
* Removes the passed card from the collection.
*/
public Boolean removeCard(Card card) {
return cards.remove(card);
}
public Boolean removeCards(CardCollection aCollec) {
if (hasCards(aCollec)) {
Iterator<Card> iter = aCollec.iterator();
while(iter.hasNext())
removeCard(iter.next());
return true;
}
else {
return false;
}
}
/*
* Returns an iterator for the ArrayList of Cards in the collection.
*/
public Iterator<Card> iterator() {
return cards.iterator();
}
public Boolean hasCard(Card aCard) {
return cards.contains(aCard);
}
public Boolean hasCards(CardCollection aCollec) {
Iterator<Card> iter = aCollec.iterator();
while(iter.hasNext()) {
if (!hasCard(iter.next())) return false;
}
return true;
}
/*
* Sorts the hand according to suit and card value. Sorting criteria specified in
* the assignment file.
*/
public void sort() {
Collections.sort(cards, new CardComparator());
}
public void clear() {
cards.clear();
}
public int size() {
return cards.size();
}
public Boolean isEmpty() {
return cards.isEmpty();
}
// ______________PRIVATE______________
/*
* Add a standard 52 card deck to the card collection.
*/
private void addDeck() {
Deck defaultDeck = new Deck();
Iterator<Card> iter = defaultDeck.iterator();
while(iter.hasNext()) {
cards.add(iter.next());
}
}
/*
* Throws an exception if adding the passed number of cards exceeds the total card limit
* of the CardCollection.
*/
private void enforceTotalCardLimit(int numOfCards) {
if (!isLimited)
throw new IllegalStateException("CardCollection is not limited!");
if (cards.size() + numOfCards > (deckLimit * Deck.DECK_SIZE)) {
throw new IllegalStateException("Number of cards being added to the pile exceeds"
+ " the DECK_LIMIT which has been set to " + deckLimit);
}
}
}