Repository navigation
Expand file tree
/
Copy pathDealer.java
More file actions
164 lines (137 loc) · 3.61 KB
/
Copy pathDealer.java
File metadata and controls
164 lines (137 loc) · 3.61 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Stack;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
/*
* This class contains the deck and card classes that creates the dealer hand and the deck itself.
*/
public class Dealer {
private Deck deck;
private Queue<Card> dealerHand;
private Card holeCard;
private int total = 0;
public Dealer() {
MakeDeck();
}
public void MakeDeck() {
deck = new Deck();
deck.ShuffleDeck();
}
public void CreateDealerHand() {
dealerHand = new LinkedList<>();
AddDealerCard();
holeCard = deck.GetDeckCards().pop();
}
public void AddDealerCard() {
Card newCard = deck.GetDeckCards().pop();
dealerHand.add(newCard);
total += newCard.GetRankValue(newCard.GetRank(), total);
}
public void AddHoleCard() {
dealerHand.add(holeCard);
total += holeCard.GetRankValue(holeCard.GetRank(), total);
}
public Deck GetDeck() {
return deck;
}
public Queue<Card> GetDealerHand() {
return dealerHand;
}
public void PrintDealerHand() {
System.out.println("Dealer's Hand: ");
System.out.println("--------------");
System.out.println(GetDealerHand());
}
public int GetDealerTotal() {
return total;
}
public void SetDealerTotal(int newTotal) {
total = newTotal;
}
public void ClearDealerHand() {
dealerHand.clear();
}
public String GetHoleCard() {
return "[" + holeCard.toString() + "]";
}
}
class Deck {
private Stack<Card> deck;
public Deck() {
deck = new Stack<>();
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
for (String suit : suits) {
for (String rank : ranks) {
Card card = new Card(suit, rank);
deck.push(card);
}
}
}
public void ShuffleDeck() {
Collections.shuffle(deck);
}
public Stack<Card> GetDeckCards() {
return deck;
}
public String toString() {
String s = "Deck{ ";
for (Card card : deck) {
s += card + ", ";
}
s += " }";
return s;
}
}
class Card {
// There are 52 cards in a deck:
private String rank; // number or name
private String suit; // symbol
/*
* > Clubs
* > Diamonds
* > Hearts
* > Spades
*/
public Card(String cardSuit, String cardRank) {
rank = cardRank;
suit = cardSuit;
}
public Card() {
rank = "noRank";
suit = "noSuit";
}
public String GetRank() {
return rank;
}
public String GetSuit() {
return suit;
}
public String toString() {
return rank + " of " + suit;
}
public int GetRankValue(String rank, int total) {
int value;
switch (rank) {
case "2" -> value = 2;
case "3" -> value = 3;
case "4" -> value = 4;
case "5" -> value = 5;
case "6" -> value = 6;
case "7" -> value = 7;
case "8" -> value = 8;
case "9" -> value = 9;
case "10" -> value = 10;
case "Jack", "Queen", "King" -> value = 10;
case "Ace" -> {
if (total <= 10) {
value = 11;
} else {
value = 1;
}
}
default -> value = 0;
}
return value;
}
}