Repository navigation
Expand file tree
/
Copy pathBlackjack.java
More file actions
77 lines (68 loc) · 2.32 KB
/
Copy pathBlackjack.java
File metadata and controls
77 lines (68 loc) · 2.32 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
public class Blackjack {
public int parseCard(String card) {
switch (card) {
case "two":
return 2;
case "three":
return 3;
case "four":
return 4;
case "five":
return 5;
case "six":
return 6;
case "seven":
return 7;
case "eight":
return 8;
case "nine":
return 9;
case "ten":
case "jack":
case "queen":
case "king":
return 10;
case "ace":
return 11;
default:
return 0;
}
}
public boolean isBlackjack(String card1, String card2) {
return parseCard(card1) + parseCard(card2) == 21;
}
public String largeHand(boolean isBlackjack, int dealerScore) {
if (!isBlackjack) {
return "P";
} else if (dealerScore < 10) {
return "W";
}
return "S";
}
public String smallHand(int handScore, int dealerScore) {
if (handScore >= 17) {
// If your cards sum up to 17 or higher you should always stand.
return "S";
} else if (handScore <= 11) {
// If your cards sum up to 11 or lower you should always hit.
return "H";
} else if (handScore >= 12 && handScore <= 16 && dealerScore < 7) {
//If your cards sum up to a value within the range [12, 16] you should always stand unless the dealer has a 7 or higher.
return "S";
}
// Hit otherwise
return "H";
}
// FirstTurn returns the semi-optimal decision for the first turn, given the cards of the player and the dealer.
// This function is already implemented and does not need to be edited. It pulls the other functions together in a
// complete decision tree for the first turn.
public String firstTurn(String card1, String card2, String dealerCard) {
int handScore = parseCard(card1) + parseCard(card2);
int dealerScore = parseCard(dealerCard);
if (20 < handScore) {
return largeHand(isBlackjack(card1, card2), dealerScore);
} else {
return smallHand(handScore, dealerScore);
}
}
}