-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
73 lines (65 loc) · 1.62 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A traditional 52-card deck of playing cards.
*
* Created by zilles on 2/23/17.
*/
public class Deck {
List<Card> cards;
/**
* Creates a shuffled deck of 52 playing cards.
*/
public Deck() {
this.cards = new ArrayList<>();
for (Card.Suit suit : Card.Suit.values()) {
for (int rank = 0; rank < Card.NUM_RANKS; rank++) {
cards.add(new Card(rank, suit));
}
}
shuffle();
}
/**
* Have all the cards been drawn?
*
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return cards.isEmpty();
}
/**
* Draw a single card, removing it from the deck
*
* @return the drawn card
*/
public Card draw() {
if (cards.isEmpty()) {
return null;
}
return cards.remove(cards.size() - 1);
}
/**
* Draw multiple cards. Can return less than the requested number
* if deck doesn't have enough cards.
*
* @param numberToDraw the desired number of cards to draw.
* @return a List of the drawn cards.
*/
public List<Card> draw(int numberToDraw) {
List<Card> cards = new ArrayList<>();
for (int i = 0; i < numberToDraw; i++) {
Card card = draw();
if (card != null) {
cards.add(card);
}
}
return cards;
}
/**
* Shuffle the cards remaining in the deck.
*/
public void shuffle() {
Collections.shuffle(cards);
}
}