-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack (4).java
136 lines (135 loc) · 4.35 KB
/
BlackJack (4).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
import java.util.*;
public class BlackJack
{
private DeckOfCards deck;
private Vector<String> player;
private Vector<String> dealer;
//Post: The BlackJack game, the dealer hand, and the player hand are all properly
// initialized. The DeckOfCards are shuffled. Both the player and the dealer are
// delt 2 cards.
public BlackJack()
{
deck=new DeckOfCards();
player=new Vector<String>();
dealer=new Vector<String>();
deck.shuffle();
for(int i=0;i<2;++i)
{
player.add(deck.deal());
}
for(int i=0;i<2;++i)
{
dealer.add(deck.deal());
}
}
//Desc: Play 1 game of BlackJack
//Input: When prompted, via the keyboard hit 'h' to hit or 's' to stay.
//Output: Various messages indicating the progress of the game.
public void play()
{
displayPlayer();
displayDealer();
playersTurnForBlackjack();
displayDealerTwo();
dealerTurn();
}
//Output: the cards currently in the player's hand.
private void displayPlayer()
{
System.out.println("Your hand: ");
for(int i=0;i<player.size();++i)
System.out.println(" "+player.get(i));
}
//Output: the dealer's initial hand. The second card is hidden by a row of stars.
private void displayDealer()
{
System.out.println("Dealer's hand: ");
for(int i=0;i<1;++i)
System.out.println(" "+dealer.get(i));
System.out.println(" *******");
}
//Output: the cards currently in the dealer's hand.
private void displayDealerTwo()
{
System.out.println("Dealer's hand:");
for(int i=0;i<dealer.size();++i)
System.out.println(" "+dealer.get(i));
}
//Desc: Draws another card if player chooses to hit. Exits the program if player busts.
//Input: 'h' to hit or 's' to stay when prompted.
//Output: the total of the players current hand. A message indicating that the player
// has busted if player's hand total is over 21.
public void playersTurnForBlackjack()
{
while(true)
{
Scanner f=new Scanner(System.in);
System.out.print("You have "+total(player));
System.out.print(" Hit or stay - H/S: ");
String pick=f.nextLine();
if(pick.charAt(0)=='h')
{
player.add(deck.deal());
displayPlayer();
if(total(player)>21)
{
System.out.println("You busted. Peace out!");
System.exit(0);
}
}
else break;
}
}
//Desc: If dealer's hand is less than 17 dealer must hit until it is over 17.
// If dealer's hand is more than 17 but less than or equal to 21, dealer must stay.
//Output: the total of the dealer's hand. Various messages indicating who has won the game.
public void dealerTurn()
{
while(true)
{
if(total(dealer)<17)
{
dealer.add(deck.deal());
displayDealerTwo();
}
else if((total(dealer)>=17) && (total(dealer)<=21))
{
if(total(dealer)>total(player))
{
System.out.println("Dealer has "+total(dealer)+". You lose. Bye Bye");
break;
}
else
{
System.out.println("Dealer has "+total(dealer)+". You Won! Bye Bye");
break;
}
}
else
{
System.out.println("Dealer lost "+total(dealer)+". You Won! Bye!Bye");
break;
}
}
}
//Pre: v contains playing cards
//Return: the numeric total of all cards in v
private int total(Vector<String> v)
{
int result=0;
for( int i=0;i<v.size();++i)
{
if(v.get(i).startsWith("Ace")) result+=11;
else if(v.get(i).startsWith("Two")) result+=2;
else if(v.get(i).startsWith("Three")) result+=3;
else if(v.get(i).startsWith("Four")) result+=4;
else if(v.get(i).startsWith("Five")) result+=5;
else if(v.get(i).startsWith("Six")) result=+6;
else if(v.get(i).startsWith("Seven")) result+=7;
else if(v.get(i).startsWith("Eight")) result+=8;
else if(v.get(i).startsWith("Nine")) result+=9;
else result+=10;
}
return result;
}
}