Repository navigation
Expand file tree
/
Copy pathHand.java
More file actions
64 lines (51 loc) · 1.36 KB
/
Copy pathHand.java
File metadata and controls
64 lines (51 loc) · 1.36 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
import java.util.ArrayList;
public class Hand {
private ArrayList<Card> hand = new ArrayList<>();
public Hand(){
}
//Makes the data to string
public String toString(){
String toReturn = "";
for(Card card : hand){
toReturn += card.toString() + ", ";
}
toReturn += " | Total: " + value();
return toReturn;
}
//adds a card to the hand
public void giveCard(Card card){
hand.add(card);
}
//prints out the hand
public void showHand(){
toString();
System.out.println(toString());
}
//Gets the value of the hand (adds up all the cards in the hand)
public int value(){
int sum = 0;
int Aces11 = 0;
for(Card card : hand){
sum += card.getValue();
//this part is for the Aces
if (card.getValue() == 11){
Aces11++;
}
}
//switches Aces when needed
while(sum > 21 && Aces11 > 0){
for(Card card : hand){
if (card.getValue() == 11){
card.changeValue(1);
sum-= 10;
break;
}
}
}
return sum;
}
//Might empty the hand (hasn't been tested yet lol)
public void emptyHand(){
hand.removeAll(hand);
}
}