-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.java
More file actions
59 lines (49 loc) · 1.27 KB
/
Hand.java
File metadata and controls
59 lines (49 loc) · 1.27 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
import java.io.Serializable;
import java.util.ArrayList;
public class Hand implements Serializable {
private final ArrayList<Card> cards = new ArrayList<>();
private final int wager;
public Hand(int wager) {
this.wager = wager;
}
public int bet() {
return wager;
}
public void addCard(Card card) {
if (card != null) {
cards.add(card);
} else {
System.out.println("Attempted to add a null card to the hand");
}
}
public ArrayList<Card> getCards() {
return cards;
}
public int getValue() {
int total = 0, aces = 0;
for (Card c : cards) {
int v = c.getValue();
total += v;
if (v == 11)
aces++;
}
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
}
public boolean isBlackjack() {
return cards.size() == 2 && getValue() == 21;
}
public boolean isBusted() {
return getValue() > 21;
}
public boolean canSplit() {
return cards.size() == 2 && cards.get(0).getValue() == cards.get(1).getValue();
}
@Override
public String toString() {
return cards.toString();
}
}