-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathGamer.java
More file actions
96 lines (81 loc) · 2.48 KB
/
Gamer.java
File metadata and controls
96 lines (81 loc) · 2.48 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
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
package blackjack.domain.gamer;
import blackjack.domain.card.Card;
import blackjack.domain.card.Cards;
import blackjack.domain.card.Denomination;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public abstract class Gamer {
private static final String ERROR_NOT_ENOUGH_CARD = "덱에 남아있는 카드가 없습니다.";
private static final int TEN = 10;
private static final int THRESHOLD = 21;
private static final int INIT_CARD_COUNT = 2;
private List<Card> cardsBundle = Cards.getCardList();
private List<Card> cards = new ArrayList<>();
private String name;
public Gamer(String name) {
this.name = name;
this.cards = initSetting();
}
public Gamer(String name, List<Card> cards) {
this.name = name;
for(Card card : cards) {
this.cards.add(card);
}
for(Card card : cards) {
cardsBundle.remove(card);
}
}
private List<Card> initSetting() {
Collections.shuffle(cardsBundle);
this.cards = cardsBundle.stream()
.limit(INIT_CARD_COUNT)
.collect(Collectors.toList());
removeCard();
removeCard();
return this.cards;
}
public int calcScore(Gamer player) {
int score = player.cards.stream()
.map(Card::getDenomination)
.mapToInt(Denomination::getValue)
.sum();
long aceCount = player.cards.stream().filter(card -> card.getDenomination().isAce())
.count();
for (int i = 0; i < aceCount; i++) {
score = adjustScore(score);
}
return score;
}
private int adjustScore(int score) {
if (score + TEN <= THRESHOLD) {
score += TEN;
}
return score;
}
public List<Card> addCard(List<Card> cards) {
cards.add(cardsBundle.get(0));
removeCard();
return cards;
}
private void removeCard() {
try {
cardsBundle.remove(0);
} catch (RuntimeException runtimeException) {
throw new RuntimeException(ERROR_NOT_ENOUGH_CARD);
}
}
public boolean isBlackJack(Gamer player) {
return calcScore(player) == THRESHOLD;
}
public boolean isBust(Gamer player) {
return calcScore(player) > THRESHOLD;
}
public List<Card> getCards() {
return cards;
}
public String getName() {
return name;
}
}