-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.java
More file actions
195 lines (179 loc) · 4.17 KB
/
Blackjack.java
File metadata and controls
195 lines (179 loc) · 4.17 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
import java.util.*;
import java.io.*;
public class Blackjack
{
private ArrayList<Card> deck;
private ArrayList<Card> playerHand;
private ArrayList<Card> dealerHand;
private boolean showDealerCard;
private BufferedReader in;
private String winOrLoss;
public static void main(String[] args)
{
Blackjack b = new Blackjack();
try{b.play();}
catch (IOException e){}
}
public Blackjack()
{
winOrLoss = "";
in = new BufferedReader(new InputStreamReader(System.in));
deal();
//updateDisplay();
}
public void play() throws IOException
{
while (true)
{
String temp = in.readLine();
if (temp.equals("h"))
hit();
else if(temp.equals("s"))
stand();
}
}
public void deal()
{
deck = new ArrayList<Card>();
playerHand = new ArrayList<Card>();
dealerHand = new ArrayList<Card>();
showDealerCard = false;
//put all cards in deck in order
for (int i = 1; i <= 13; i++)
{
deck.add(new Card(i,"h"));
deck.add(new Card(i,"d"));
deck.add(new Card(i,"c"));
deck.add(new Card(i, "s"));
}
//randomizes the player card selected
//makes sure that the cards dont repeat
int x = 0;
for (int i = 0; i <= 1; i++)
{
int n = (int)(Math.random()*deck.size());
Card player = deck.remove(n);
playerHand.add(player);
x = getScore(playerHand);
}
for (int i = 0; i <= 1; i++)
{
int n = (int)(Math.random()*deck.size());
Card dealer = deck.remove(n);
dealerHand.add(dealer);
int y = getScore(dealerHand);
if (y == 21)
stand();
}
if (x == 21)
{
//System.out.println("You Win!");
deal();
}
}
public int getScore(ArrayList<Card> hand)
{
int tot = 0;
int numAces = 0;
for (int i = 0; i <= hand.size() - 1; i++)
{
Card c = hand.get(i);
int add = c.getRank();
if (add > 10 && add <= 13)
add = 10;
if (add == 1)
{
add = 11;
numAces++;
}
tot = tot + add;
//if bust add 1 if ace
while (tot > 21 && numAces > 0)
{
tot = tot - 10;
numAces--;
}
}
return tot;
}
public ArrayList<Card> getPlayerHand()
{
return playerHand;
}
public Card getShownDealerCard()
{
return dealerHand.get(1);
}
public void hit()
{
//System.out.println("hit");
//System.out.println("");
int n = (int)(Math.random()*deck.size());
Card player = deck.remove(n);
playerHand.add(player);
//updateDisplay();
if (getScore(playerHand) > 21)
{
winOrLoss = "loss";
//System.out.println("HAHAH YOU BUSTED");
//System.out.println("");
deal();
//updateDisplay();
}
}
public void stand()
{
//System.out.println("stand");
//System.out.println("");
int playerScore = getScore(playerHand);
int dealerScore = getScore(dealerHand);
showDealerCard = true;
while (dealerScore < 16)
{
int n = (int)(Math.random()*deck.size());
Card dealer = deck.remove(n);
dealerHand.add(dealer);
dealerScore = getScore(dealerHand);
}
//updateDisplay();
if (dealerScore > playerScore && dealerScore < 22)
{
winOrLoss = "loss";
//System.out.println("HOUSE WINS");
//System.out.println("");
}
if (dealerScore > 21 || playerScore > dealerScore)
{
winOrLoss = "win";
//System.out.println("YOU WIN!");
//System.out.println("");
}
if (playerScore == dealerScore)
{
winOrLoss = "tie";
//System.out.println("TIE");
//System.out.println("");
}
deal();
//updateDisplay();
}
public void updateDisplay()
{
String s = "Dealer(" + getScore(dealerHand) +"): ";
for (Card card: dealerHand)
s += card.getImage() + " : ";
//System.out.println(s);
//System.out.println(" ");
s = "You("+ getScore(playerHand) + "): ";
for (Card card: playerHand)
s += card.getImage() + " : ";
//System.out.println(s);
//System.out.println(" ");
}
public String result()
{
String temp = winOrLoss;
winOrLoss = "";
return temp;
}
}