-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRound.java
More file actions
132 lines (122 loc) · 3.46 KB
/
Copy pathRound.java
File metadata and controls
132 lines (122 loc) · 3.46 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
package blackJack;
import java.util.Scanner;
/**
* A class to run a round of blackjack with the appropriate game logic.
* @author Adam Wiener
*
*/
public class Round {
private boolean _win;
private boolean _bust;
private boolean _blackjack;
private boolean _draw;
private Deck _deck;
private Hand _dealer;
private Hand _player;
private Scanner userInput;
public Round(Scanner userInput) {
_win = false;
_bust = false;
_blackjack = false;
_draw = false;
_deck = new Deck();
_dealer = new Hand();
_player = new Hand();
this.userInput = userInput;
}
public void play() {
_deck.shuffleDeck();
//deal the player and the dealer two cards each
initialDeal();
System.out.println("Dealer has: " + _dealer.getCardFromHand(0).displayCard() +" []");
playerHandWithScore();
if (_player.handValue() == 21) {
System.out.println("Blackjack! You win!");
_blackjack = true;
}
//perform player and dealer turns
playerMove();
dealerMove();
}
public void playerMove() {
while (!_bust && !_blackjack) {
if (hitOrStand() != 1) {
break;
}
//draw a card and add to hand.
Card drawnCard = _deck.drawTopCard();
System.out.println("You were delt: " + drawnCard.displayCard());
_player.addCardToHand(drawnCard);
playerHandWithScore();
//if new sum is greater than 21, then hand is bust.
if (_player.handValue() > 21) {
System.out.println("You went bust! \nThe dealer wins.\n");
_bust = true;
break;
}
}
}
public int hitOrStand(){
System.out.println("\nWould you like to hit(1) or stand(2)?");
int command = userInput.nextInt();
if (command == 1) {
System.out.println("You chose to hit.\n");
} else {
System.out.println("You chose to stand.\n");
}
return command;
}
public void dealerMove() {
if (!_bust && !_blackjack) {
//dealer draws while hand value is less than 17
while (_dealer.handValue() < 17) {
dealerHandWithScore();
Card drawnCard = _deck.drawTopCard();
_dealer.addCardToHand(drawnCard);
System.out.println("The dealer drew: " + drawnCard.displayCard());
//dealer busts over 21
if (_dealer.handValue() > 21) {
dealerHandWithScore();
System.out.println("The dealer went bust. You win!");
_win = true;
break;
}
}
}
//compare dealer hand and player hand and decide game outcome
if (!_win && !_bust) {
dealerHandWithScore();
System.out.println("\nThe dealer has: " + _dealer.handValue() + " and you have " + _player.handValue());
if (_dealer.handValue() > _player.handValue()) {
System.out.println("The dealer wins.");
} else if (_dealer.handValue() == _player.handValue()) {
System.out.println("The game is a draw.");
_draw = true;
} else {
System.out.println("You win!");
_win = true;
}
}
}
private void initialDeal() {
_player.addCardToHand(_deck.drawTopCard());
_dealer.addCardToHand(_deck.drawTopCard());
_player.addCardToHand(_deck.drawTopCard());
_dealer.addCardToHand(_deck.drawTopCard());
}
public boolean isBlackjack() {
return _blackjack;
}
public boolean isWin() {
return _win;
}
public boolean isDraw() {
return _draw;
}
public void dealerHandWithScore() {
System.out.println("The dealer has: " + _dealer.displayHandWithScore());
}
public void playerHandWithScore() {
System.out.println("You have: " + _player.displayHandWithScore());
}
}