-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack.java
121 lines (105 loc) · 3.85 KB
/
BlackJack.java
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
import java.util.*;
import java.lang.Integer;
import static java.util.Arrays.asList;
public class BlackJack {
Deque<Integer> shoe;
ArrayList<Integer> dealerHand;
ArrayList<Integer> playerHand;
int dealerVal;
int playerVal;
int losses, wins, rounds;
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public BlackJack(int shoeSize){
losses = 0; wins = 0; rounds = 0;
// Build the shoe
ArrayList<Integer> shoeList = new ArrayList<Integer>();
int[] ranks = new int[] {2,3,4,5,6,7,8,9,10,10,10,10,11};
for(int suit = 0; suit < 4*shoeSize; suit++)
for(int j = 0; j < 13; j++)
shoeList.add(ranks[j]);
// Shuffle the shoe and convert it to a Deque
Collections.shuffle(shoeList);
shoe = new ArrayDeque<Integer>(shoeList);
}
public static void main(String[] args){
int ss = Integer.parseInt(args[0]);
int result;
BlackJack bj = new BlackJack(ss);
int loses = 0 , wins = 0;
for (int roundCt = 0; bj.shoe.size() > 10; roundCt++){
System.out.print("Dealing...\t");
bj.deal();
bj.play();
bj.getResult();
}
for(int i = 0; i<80; i++)
System.out.print("-");
System.out.print('\n');
System.out.printf("Summary:\nWins: %d\tLosses: %d\t Score: %d\n",
wins,loses,wins-loses);
}
/**
* Deals two cards to player and dealer hands. Plays for dealer also, since
* this is entirely deterministic and independent of player decisions.
* Dealer stands on Soft 17's.
* @return rank of dealers face-up card.
*/
public int deal(){
// Create new empty ArrayLists for dealer and player hands
dealerHand = new ArrayList<>();
playerHand = new ArrayList<>();
// deal two cards from shoe to dealer and player.
for (int i = 0; i < 2; ++i){
dealerHand.add(shoe.poll());
playerHand.add(shoe.poll());
}
// dealer hits until > 17 stays on all > 17s
while(dealerHand.stream().mapToInt(Integer::intValue).sum() < 17)
dealerHand.add(shoe.poll());
dealerVal = dealerHand.stream().mapToInt(Integer::intValue).sum();
return dealerHand.get(1);
}
public void play(){
while(playerHand.stream().mapToInt(Integer::intValue).sum() < 17)
playerHand.add(shoe.poll());
playerVal = playerHand.stream().mapToInt(Integer::intValue).sum();
}
public int getResult(){
if (playerVal > 21){
System.out.println(ANSI_RED + "Player Busts, Dealer Wins" +
ANSI_RESET);
return -1;
}
else if(dealerVal > 21){
System.out.println(ANSI_GREEN + "Dealer Busts, Player Wins" +
ANSI_RESET);
return 1;
}
else if(playerVal == dealerVal){
System.out.println(ANSI_BLUE + "Push." + ANSI_RESET);
return 0;
}
else if(playerVal > dealerVal){
System.out.println(ANSI_GREEN +
"Player exceeds dealer, Player Wins" +
ANSI_RESET);
return 1;
}
else if(dealerVal > playerVal){
System.out.println(ANSI_GREEN +
"Dealer exceeds player, Dealer Wins" +
ANSI_RESET);
return - 1;
}
else
return -1;
}
}