-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJack.java
executable file
·113 lines (99 loc) · 3.88 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
import java.util.Scanner;
public class BlackJack
{
private Player player;
private Dealer dealer;
public BlackJack()
{
}
//play a game of blackjack with one player and one dealer
public void playGame()
{
// Set Up
Scanner keyboard = new Scanner(System.in);
player = new Player();
dealer = new Dealer();
int winner;
// ++ Deal ++
boolean playing = true;
while (playing == true) {
winner = 2;
// Dealer's Cards
for (int d = 0; d != 2; d++) {
dealer.addCardToHand(dealer.dealCard());
}
System.out.println("Dealer is showing a " + dealer.hand[1].toString());
// Player's Cards
for (int d = 0; d != 2; d++) {
player.addCardToHand(dealer.dealCard());
}
System.out.println("You have a " + player.hand[0].toString() + " and a " + player.hand[1].toString());
// ++ Player's Turn ++
boolean playerActive = true;
while (playerActive == true) {
System.out.print("Hand Value: " + player.getHandValue() + " Hit or Stay? ");
String move = keyboard.next();
if (move.equals("hit")) {
player.addCardToHand(dealer.dealCard());
} else if (move.equals("stay")) {
playerActive = false;
} else {
System.out.println("Move Not Recognized");
}
if (player.getHandValue() > 21) {
System.out.println("Player Busts!");
playerActive = false;
winner = 1;
}
}
System.out.println("Player's Hand Value: " + player.getHandValue());
// ++ Dealer's Turn ++
boolean dealerActive = winner != 1;
while (dealerActive == true) {
if (dealer.getHandValue() < 16) {
dealer.addCardToHand(dealer.dealCard());
} else if (dealer.getHandValue() > 21) {
System.out.println("Dealer Busts!");
winner = 0;
} else {
dealerActive = false;
}
}
System.out.println("Dealer's Hand Value: " + dealer.getHandValue());
// ++ Comparison / End ++
if (winner == 0 || (player.getHandValue() > dealer.getHandValue() && winner != 1)) {
System.out.println("Player Wins!");
player.countWin();
} else if (winner == 1 || (player.getHandValue() < dealer.getHandValue() && winner != 0)) {
System.out.println("Dealer Wins!");
dealer.countWin();
} else {
System.out.println("Push!");
}
System.out.println("Dealer has " + dealer.getWinCount() + " wins");
System.out.println("Player has " + player.getWinCount() + " wins");
//Reset hands
player.resetHand();
dealer.resetHand();
// Play Again
boolean playAgainValid = false;
while (playAgainValid == false) {
System.out.println("Play Again?");
String playAgain = keyboard.next().toLowerCase();
if (playAgain.equals("no")) {
playing = false;
playAgainValid = true;
} else if (playAgain.equals("yes")) {
playAgainValid = true;
} else {
System.out.println("Invalid Response: Please enter yes or no");
}
}
}
}
public static void main(String[] args)
{
BlackJack game = new BlackJack();
game.playGame();
}
}