-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.java
227 lines (175 loc) · 3.65 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
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
import java.util.Scanner;
import java.util.Random;
public class Game21
{
public static Scanner keyboard = new Scanner (System.in);
public static void main (String [] args)
{
System.out.println("The game is called 21\nTry to get as close to 21" + " without going over and have a higher hand than the computer dealer.\n" + "Here we go!");
int play = 0;
int won = 0;
int lost = 0;
int tied = 0;
String ans;
char repeat = 'y';
int totalPlayer, totalDealer;
while (repeat == 'y' || repeat == 'Y')
{
play = play+1;
int total = generateCard();
System.out.println("The dealer is showing a " + total);
totalPlayer=Player();
if (totalPlayer ==-1)
{
System.out.println("You busted with a " + totalPlayer);
lost++;
}
else{
totalDealer = Dealer(total);
if (totalDealer ==-1)
{
System.out.println("The dealer has busted. You win!");
won++;
}
else if (totalDealer < totalPlayer)
{
System.out.println("The dealer has " + totalDealer + " and you have " + totalPlayer + "\nYou win!");
won++;
}
else if (totalDealer > totalPlayer)
{
System.out.println("The dealer has " + totalDealer + " and you have " + totalPlayer + "You lose!");
lost++;
}
else
{
System.out.println("Tis a draw!");
tied++;
}
}
System.out.println("Would you like to play again? (y/n)");
ans = keyboard.nextLine();
repeat = ans.charAt(0);
}
System.out.println("Games played: " + play);
System.out.println("Games won: " + won);
System.out.println("Games tied: " + tied);
System.out.println("Games lost: " + lost);
if (won > lost)
{
System.out.println("Must be your luck day!");
}
else
{
System.out.println("Maybe just stick t penny slots...");
}
System.out.println("Have a good day!");
}
public static int Player ()
{
int card = generateCard( );
int card2 = generateCard( );
if (card == 11 && card2 == 11)
{
card = 1;
}
int total2 = card + card2;
PlayerTotal(total2);
String ans1;
char repeat1;
System.out.println("Would you like another card? y / n?");
ans1 = keyboard.nextLine();
repeat1 = ans1.charAt(0);
while (repeat1 == 'y'|| repeat1 == 'Y')
{
card = generateCard( );
int total3 = total2 + card;
if (card == 11 && total3 > 21)
{
card = 1;
}
System.out.println("Your new card is " + card);
total2 += card;
PlayerTotal(total2);
if (total2 <= 21)
{
System.out.println("Would you like another card? y / n?");
ans1 = keyboard.nextLine();
repeat1 = ans1.charAt(0);
}
else
{
total2 = -1;
repeat1 =' ';
}
}
return total2;
}
public static int generateCard ()
{
Random rand = new Random();
int card = rand.nextInt(14)+1;
if ((card >= 10 && card < 14))
{
card = 10;
}
else if (card == 14)
{
card = 11;
}
return card;
}
public static void PlayerTotal (int total)
{
if (total <= 21)
{
if (total >= 20 || total <= 21)
{
System.out.println("Your total is " + total + " that is pretty good");
}
else if (total >= 17 && total <= 19)
{
System.out.println("Your total is " + total + "That is good");
}
else if (total <=16 && total >= 12)
{
System.out.println("Your total is " + total + " bet you can do better");
}
else
{
System.out.println("It's only a " + total + "please do not stop");
}
}
else {
System.out.println("total is" + total);
}
}
public static int Dealer (int total)
{
int card = generateCard( );
if ((total == 11) && (card == 11))
{
card = 1;
}
total += card;
System.out.println("The dealer has " + total);
while (total < 17)
{ // takes cards until they get 17
card = generateCard( );
if (card == 11)
{
if ((card + total) > 21)
{
card = 1;
}
}
total += card;
System.out.println("The dealer got " + card + " for a total of " + total);
}
if (total <= 21)
{
return total;
}
else return -1;
}
}