-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
128 lines (113 loc) · 5.2 KB
/
Simulator.java
File metadata and controls
128 lines (113 loc) · 5.2 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
public class Simulator {
public static void main(String[] args) {
/*
Runs trials on large amounts of hands to determine player edge using different strategies and bet spreads
*/
//Settings
double bankroll = 0;
int decks = 4;
int penetration = 3;
int hands = 100000000;
Shoe shoe = new Shoe(decks, penetration);
//Loop for a full hand
for (int i = 0; i < hands; i++) {
//Sets bet size based on count
double bet = 1;
double splitBet = 1;
int count = shoe.getTrueCount();
if (count > 0) {
bet = count;
splitBet = count;
}
//Sets up hands: Gives player two cards, Gives dealer 1 card and draws a second but doesn't add it till later
Hand dealerHand = new Hand(shoe.draw());
Hand playerHand = new Hand(shoe.draw());
Hand splitHand = null;
playerHand.addCard(shoe.draw());
Card downCard = shoe.draw();
//Checks for blackjack
if (playerHand.valueOf() == 21 && (dealerHand.valueOf() + downCard.getValue()) == 21) {
dealerHand.addCard(downCard);
} else if (playerHand.valueOf() == 21 && (dealerHand.valueOf() + downCard.getValue()) != 21) {
bankroll += 1.5 * bet;
dealerHand.addCard(downCard);
} else if ((dealerHand.valueOf() + downCard.getValue()) == 21 && playerHand.valueOf() != 21) {
bankroll -= bet;
dealerHand.addCard(downCard);
} else {
//Loop for calculating and executing player's moves
while (true) {
String move = MoveCalculator.calculate(dealerHand, playerHand, shoe.getTrueCount(),
shoe.getRunningCount(), true);
if (move.equals("SP")) {
splitHand = new Hand(playerHand.split());
if (splitHand.valueOf() == 11) {
splitHand.setAcesSplit();
}
splitHand.denySplit();
playerHand.addCard(shoe.draw());
splitHand.addCard(shoe.draw());
} else if (move.equals("H")) {
playerHand.addCard(shoe.draw());
} else if (move.equals("D")) {
bet *= 2;
playerHand.addCard(shoe.draw());
break;
} else if (move.equals("S")) {
break;
}
}
//Loop for split hand moves if the split hand exists
if (splitHand != null) {
while (true) {
String splitMove = MoveCalculator.calculate(dealerHand, splitHand, shoe.getTrueCount(),
shoe.getRunningCount(), true);
if (splitMove.equals("H")) {
splitHand.addCard(shoe.draw());
} else if (splitMove.equals("D")) {
splitBet *= 2;
splitHand.addCard(shoe.draw());
break;
} else if (splitMove.equals("S")) {
break;
}
}
}
//Loop for dealer's moves. Adds downcard from earlier
dealerHand.addCard(downCard);
while (dealerHand.valueOf() < 17) {
dealerHand.addCard(shoe.draw());
}
//Determines the winner of the hand
if (playerHand.valueOf() > 21) {
bankroll -= bet;
} else if (dealerHand.valueOf() > 21) {
bankroll += bet;
} else if (playerHand.valueOf() > dealerHand.valueOf()) {
bankroll += bet;
} else if (playerHand.valueOf() < dealerHand.valueOf()) {
bankroll -= bet;
}
//Determines winner of split hand
if (splitHand != null) {
if (splitHand.valueOf() > 21) {
bankroll -= splitBet;
} else if (dealerHand.valueOf() > 21) {
bankroll += splitBet;
} else if (splitHand.valueOf() > dealerHand.valueOf()) {
bankroll += splitBet;
} else if (splitHand.valueOf() < dealerHand.valueOf()) {
bankroll -= splitBet;
}
}
}
}
//Final statistics
System.out.println("Bankroll: " + bankroll);
if (bankroll < 0)
System.out.println("Casino edge: " + bankroll/hands * -100 + "%");
else
System.out.println("Player edge: " + bankroll/hands * 100 + "%");
System.out.println(bankroll / hands * 100 + " bets/100 hands");
}
}