-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardCountingSimulator.java
More file actions
238 lines (206 loc) · 5.92 KB
/
CardCountingSimulator.java
File metadata and controls
238 lines (206 loc) · 5.92 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
public class CardCountingSimulator {
private int pool;
private Player p;
private Deck d;
private int bet;
private Player dealer;
private int runningCount;
private double trueCount;
private static final int[] KEY = {-1, 1, 1, 1, 1, 1, 0, 0, 0, -1, -1, -1, -1};
/**
* constructor for ccsim
* @param dealer of the game
* @param p player of game
* @param pool up for betting (twice of bet)
* @param d deck of cards
*/
public CardCountingSimulator (Player dealer, Player p, int pool, Deck d){
this.dealer = dealer;
this.p = p;
this.pool = pool;
this.d = d;
d.shuffle();
for(int i = 0; i < 2; i++) {
Card c = d.draw();
runningCount += countChecker(c);
p.setRunningCount(runningCount);
trueCount = ((double) runningCount) / (((double) d.cardsLeft()) / 52.0);
p.setTrueCount(trueCount);
p.addCard(c);
}
for(int i = 0; i < 2; i++) {
Card c = d.draw();
runningCount += countChecker(c);
p.setRunningCount(runningCount);
trueCount = ((double) runningCount) / (((double) d.cardsLeft()) / 52.0);
p.setTrueCount(trueCount);
dealer.addCard(c);
}
}
/**
* checks the count value for the card
* @param c card to be checked
* @return count value for this card
*/
public int countChecker(Card c) {
return KEY[c.getRank() - 1];
}
/**
* getter for running count
* @return running count
*/
public int getRunning() {
return runningCount;
}
/**
* getter for true count
* @return true count
*/
public double getTrue() {
return trueCount;
}
/**
* draw a card from deck and update counts
* @return drawn Card
*/
private Card drawCard() {
Card c = d.draw();
runningCount += countChecker(c);
p.setRunningCount(runningCount);
trueCount = ((double) runningCount) / (((double) d.cardsLeft()) / 52.0);
p.setTrueCount(trueCount);
System.out.println("THis is true count: " + trueCount + " this is running: " + runningCount);
return c;
}
/**
* "hit" command for blackjack
* @return an int to determine who won -1 -> dealer, 0 -> nothing, 1 -> player for who won
*/
public int hit(){
int initial = p.valOfCards();
System.out.println(initial);
p.addCard(drawCard());
p.placeBet(bet);
pool += (2 * bet);
int val = p.valOfCards();
p.makeDecision("hit");
System.out.println("hit");
if(val - initial == 11 && val > 21) val -= 10;
System.out.println(val);
if(val > 21){
pool = 0;
while(p.hand().peek() != null){
d.add(p.hand().remove());
}
return -1;
}
else if(val == 21) return 1;
return 0;
}
/**
* "stand" command for blackjack
* @return an int to determine who wins -1 -> dealer wins, 0 -> tie, 1 -> player wins
*/
public int stand(){
p.makeDecision("stand");
int dealerVal = 0;
int dealerInitial = 0;
System.out.println(p.valOfCards());
while(dealer.valOfCards() <= 16) {
dealerInitial = dealer.valOfCards();
System.out.println("dealer cards: " + dealerInitial);
dealer.addCard(drawCard());
dealer.makeDecision("hit");
System.out.println("dealer hit");
dealerVal = dealer.valOfCards();
if(dealerVal - dealerInitial == 11 && dealerVal > 21) dealerVal -= 10;
System.out.println(dealerVal);
}
dealerVal = dealer.valOfCards();
dealer.makeDecision("stand");
System.out.println(dealerVal);
System.out.println("dealer stand");
System.out.println(dealerVal);
if(dealerVal > 21 ||
p.valOfCards() > dealerVal){
p.setChips(p.getChips() + 2 * pool);
pool = 0;
while(p.hand().peek() != null){
d.add(p.hand().remove());
}
while(dealer.hand().peek() != null){
d.add(dealer.hand().remove());
}
return 1;
}
else if(dealerVal > p.valOfCards()){
pool = 0;
while(p.hand().peek()!= null){
d.add(p.hand().remove());
}
while(dealer.hand().peek()!= null){
d.add(dealer.hand().remove());
}
return -1;
} else {
while(p.hand().peek()!= null){
d.add(p.hand().remove());
}
while(dealer.hand().peek()!= null){
d.add(dealer.hand().remove());
}
return 0;
}
}
/**
* getter for dealer
* @return dealer
*/
public Player getDealer(){
return dealer;
}
/**
* getter for player
* @return player
*/
public Player getPlayer(){
return p;
}
/**
* getter for the deck
* @return deck
*/
public Deck getDeck(){
return d;
}
/**
* getter for the pool
* @return current pool
*/
public int getPool(){
return pool;
}
/**
* sets the pool
* @param p new pool
*/
public void setPool(int p){
pool = p;
}
/**
* clears the hand of a player/dealer
* @param 0 -> clears player hand, 1 -> clears dealer hand
*/
public void clearHand(int playerNum) {
if(playerNum == 0) {
while(p.hand().peek()!= null){
d.add(p.hand().remove());
}
}
else if(playerNum == 1) {
while(dealer.hand().peek()!= null){
d.add(dealer.hand().remove());
}
}
}
}