-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathCardDeck.java
More file actions
53 lines (43 loc) · 1.39 KB
/
CardDeck.java
File metadata and controls
53 lines (43 loc) · 1.39 KB
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
package blackjack.domain.cards;
import blackjack.domain.card.Card;
import blackjack.domain.card.Denomination;
import blackjack.domain.card.Shape;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class CardDeck {
public static final int FIRST_INDEX = 0;
private final List<Card> cards;
public CardDeck() {
cards = createCardDeck();
}
public Card pickOneCard() {
validateEmptyCardDeck();
Collections.shuffle(cards);
Card pickedCard = cards.get(FIRST_INDEX);
cards.remove(FIRST_INDEX);
return pickedCard;
}
public List<Card> createCardDeck() {
List<Card> cards = new ArrayList<>();
for (Shape shape : Shape.values()) {
cards.addAll(DenominationByShape(shape));
}
return cards;
}
private List<Card> DenominationByShape(Shape shape) {
return Arrays.stream(Denomination.values())
.map(denomination -> new Card(shape, denomination))
.collect(Collectors.toList());
}
private void validateEmptyCardDeck() {
if (cards.isEmpty()) {
throw new IllegalArgumentException("[ERROR] 카드덱이 비어 카드를 뽑을 수 없습니다.");
}
}
public List<Card> getCards() {
return new ArrayList<>(cards);
}
}