-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.java
More file actions
222 lines (207 loc) · 11.5 KB
/
Copy pathblackjack.java
File metadata and controls
222 lines (207 loc) · 11.5 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
// Author: Jack Hatch
// Teacher: Mr. Vriesinga
// Program Name: blackjack.java
// Purpose: To be able to play a game of blackjack.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
public class blackjack {
static public String cardNames(int n){
if(n == 9){
return "Jack";
}else if(n == 10){
return "Queen";
}else if(n == 11){
return "King";
}else{
return "Ace";
}
}
static public String cardHouses(int x){
if(x == 0){
return "Spades";
}else if(x == 1){
return "Clubs";
}else if(x == 2){
return "Hearts";
}else{
return "Diamonds";
}
}
static public void dealCard(int x, int cardsDealt, String split, int[] handVals, ArrayList<String> playerHand, ArrayList<String> dealerHand, String[][] cards, int[][] cardValues, ArrayList<Integer> playerHandVals){
if(x == 0){ //Deals cards to either the player or the dealer based on the value of x and the number of cards is determined by the value of cardsDealt.
for(int n = 0; n < cardsDealt; n++){
boolean repeat = true;
while(repeat == true){
int dealtHouse = randNum.nextInt(0,3);
int dealtCard = randNum.nextInt(0,12);
if(cardValues[dealtHouse][dealtCard] != 0){
playerHand.add(cards[dealtHouse][dealtCard]);
playerHandVals.add(cardValues[dealtHouse][dealtCard]);
if(split.substring(0,1).equals("y")){
if(cardValues[dealtHouse][dealtCard] == 11 && (handVals[2] + 11) > 21){ //Changes the value of an ace based on what better suits the players current hand.
handVals[3] = handVals[3] + 1;
}else{
handVals[3] = handVals[3] + cardValues[dealtHouse][dealtCard];
}
cardValues[dealtHouse][dealtCard] = 0;
repeat = false;
}else{
if(cardValues[dealtHouse][dealtCard] == 11 && (handVals[2] + 11) > 21){ //Changes the value of an ace based on what better suits the players current hand.
handVals[2] = handVals[2] + 1;
}else{
handVals[2] = handVals[2] + cardValues[dealtHouse][dealtCard];
}
cardValues[dealtHouse][dealtCard] = 0;
repeat = false;
}
}
}
}
}else{ //Deals the dealers hand.
for(int n = 0; n < cardsDealt; n++){
boolean repeat = true;
while(repeat == true){
int dealtHouse = randNum.nextInt(0,3);
int dealtCard = randNum.nextInt(0,12);
if(cardValues[dealtHouse][dealtCard] != 0){
dealerHand.add(cards[dealtHouse][dealtCard]);
handVals[1] = handVals[1] + cardValues[dealtHouse][dealtCard];
if(n == 0){
handVals[0] = cardValues[dealtHouse][dealtCard];
}
cardValues[dealtHouse][dealtCard] = 0;
repeat = false;
}
}
}
}
}
static Scanner obj = new Scanner(System.in);
static Random randNum = new Random();
public static void main(String[]args){
String[][] cards = new String[4][13];
int[][] cardValues = new int[4][13];
String playAgain = "y";
double wallet = 100.00;
while(playAgain.substring(0,1).equals("y") && wallet > 0){
ArrayList<String> playerHand = new ArrayList<>();
ArrayList<String> playerSecondHand = new ArrayList<>();
ArrayList<Integer> playerHandVals = new ArrayList<>();
ArrayList<String> dealerHand = new ArrayList<>();
for(int x = 0; x < 4; x++){ //Sets up the deck of cards.
for(int n = 0; n < 13; n++){
if(n < 9){
cardValues[x][n] = n + 2;
}else{ //Takes into account that the jack queen and king are all cards worth 10 and the ace is worth 11 or 1 depending on your hand.
if(n < 12){
cardValues[x][n] = 10;
}else{
cardValues[x][n] = 11;
}
}
if(n < 9){ //Enters the name of the card in a given position.
cards[x][n] = (n + 2) + " of " + cardHouses(x);
}else{
cards[x][n] = cardNames(n) + " of " + cardHouses(x);
}
}
}
double wager;
System.out.printf("You currently have $%,.2f.\nThe odds of winning are 3/2 (Payout is 1.5x the entered amount).\n", wallet);
while(true){
System.out.print("Enter the amount you would like to wager:");
wager = obj.nextDouble();
if(wager > wallet || wager < 0){
System.out.println("Please enter a valid wager (the value entered was either too high or too low).");
}else{
break;
}
}
wallet = wallet - wager;
int[] handVals = {0, 0, 0, 0};
String split = "n";
dealCard(0, 2, split, handVals, playerHand, dealerHand, cards, cardValues, playerHandVals);
dealCard(1, 2, split, handVals, playerHand, dealerHand, cards, cardValues, playerHandVals);
int dealerVisibleVal = handVals[0];
int dealerEntireVal = handVals[1];
int playerHandVal1 = handVals[2];
int playerHandVal2 = handVals[3];
String playerMove = "n";
String doublePerformed = "n";
String hitAllowed = "y";
System.out.println("Your hand contains the " + playerHand.get(0) + " and the " + playerHand.get(1) + ", with a value of " + playerHandVal1 + ".");
System.out.println("The dealers visible card is the " + dealerHand.get(0) + " with a value of " + dealerVisibleVal + ".");
while(true){
if(split.equals("y") && hitAllowed.equals("n") || doublePerformed.equals("y")){
System.out.println("What do you want to do?(Stand)");
playerMove = obj.next().toLowerCase();
}else if(split.equals("y") && hitAllowed.equals("y")){
System.out.println("What do you want to do?(Hit, Stand)");
playerMove = obj.next().toLowerCase();
}else{
System.out.println("What do you want to do?(Hit, Stand, Split, Double)");
playerMove = obj.next().toLowerCase();
}
if(playerMove.substring(0,1).equals("h") && hitAllowed.equals("y")){
if(split.equals("y")){
dealCard(0, 1, "no", handVals, playerHand, dealerHand, cards, cardValues, playerHandVals);
dealCard(0, 1, split, handVals, playerSecondHand, dealerHand, cards, cardValues, playerHandVals);
playerHandVal1 = handVals[2];
playerHandVal2 = handVals[3];
System.out.println("You got a " + playerHand.get(playerHand.size()-1) + ", which raises your first hands value to " + playerHandVal1 + ".");
System.out.println("You got a " + playerSecondHand.get(playerSecondHand.size()-1) + ", which raises your second hands value to " + playerHandVal2 + ".");
hitAllowed = "n";
}else{
dealCard(0, 1, split, handVals, playerHand, dealerHand, cards, cardValues, playerHandVals);
playerHandVal1 = handVals[2];
System.out.println("You got a " + playerHand.get(playerHand.size()-1) + ", which raises your hands value to " + playerHandVal1 + ".");
}
if(playerHandVal1 > 21 || playerHandVal2 > 21) break;
}else if(playerMove.substring(0,2).equals("sp") && playerHandVals.get(0) == playerHandVals.get(1) && split.equals("n") && doublePerformed.equals("n") && playerHand.get(0).substring(0,1).equals(playerHand.get(1).substring(0,1))){
split = "y";
playerSecondHand.add(playerHand.get(1));
playerHand.remove(1);
if(playerHandVals.get(0) == 11){
handVals[2] = handVals[2] - 1;
}else{
handVals[2] = handVals[2] - playerHandVals.get(0);
}
handVals[3] = handVals[2];
}else if(playerMove.substring(0,1).equals("d") && split.equals("n") && doublePerformed.equals("n")){
wallet = wallet - wager;
wager = wager * 2;
System.out.println("Your wager was doubled to " + wager + ".");
dealCard(0, 1, split, handVals, playerHand, dealerHand, cards, cardValues, playerHandVals);
playerHandVal1 = handVals[2];
System.out.println("You got a " + playerHand.get(playerHand.size()-1) + ", which raises your hands value to " + playerHandVal1 + ".");
doublePerformed = "y";
}else if(playerMove.substring(0,2).equals("st")){
break;
}else{
System.out.println("A valid move was not entered, please enter another move.");
}
}
System.out.println("The dealers entire hand included the " + dealerHand.get(0) + " and the " + dealerHand.get(1) + ", with a value of " + dealerEntireVal + ".");
if(playerHandVal1 > 21 || playerHandVal2 > 21){
System.out.println("You busted and lost " + wager + ".");
}else if(playerHandVal1 > dealerEntireVal || playerHandVal2 > dealerEntireVal){
System.out.printf("You win and earned $%,.2f.\n", (wager*0.5));
wallet = wallet + (wager*1.5);
}else if(playerHandVal1 == dealerEntireVal || playerHandVal2 == dealerEntireVal){
System.out.println("You tied and broke even.");
wallet = wallet + wager;
}else if(playerHandVal1 < dealerEntireVal && playerHandVal2 < dealerEntireVal){
System.out.println("You lose and lost " + wager + ".");
}
if(wallet > 0){
System.out.println("Do you want to play again?");
playAgain = obj.next().toLowerCase();
}else{
System.out.println("You are out of money and can no longer play.");
}
}
System.out.printf("Based on your current wallet, your wallet changed by $%,.2f.", (wallet - 100) );
}
}