/
Blackjack.java
122 lines (100 loc) · 3.41 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
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args){
System.out.println("Welcome to Blackjack!");
Deck playingDeck = new Deck();
playingDeck.createFullDeck();
playingDeck.shuffle();
Deck playerCards = new Deck();
double playerMoney = 300.0;
Deck dealerCards = new Deck();
Scanner userInput = new Scanner(System.in);
while(playerMoney>0){
System.out.println("You have $" + playerMoney + ", how much would you like to bet?");
double playerBet;
while (true) {
try {
playerBet = userInput.nextInt();
break;
} catch (Exception e) {
System.out.println("Please try again");
continue;
}
}
boolean endRound = false;
if(playerBet > playerMoney){
System.out.println("You cannot bet more than you have.");
break;
}
System.out.println("Dealing...");
playerCards.draw(playingDeck);
playerCards.draw(playingDeck);
dealerCards.draw(playingDeck);
dealerCards.draw(playingDeck);
while(true) {
System.out.println("Your Hand:" + playerCards.toString());
System.out.println("Your hand is currently valued at: " + playerCards.cardsValue());
System.out.println("Dealer Hand: " + dealerCards.getCard(0).toString() + " and [hidden]");
System.out.println("Would you like to (1)Hit or (2)Stand");
int response;
while (true) {
try {
response = userInput.nextInt();
break;
} catch (Exception e) {
System.out.println("Please try again");
continue;
}
}
if(response == 1){
playerCards.draw(playingDeck);
System.out.println("You draw a:" + playerCards.getCard(playerCards.deckSize()-1).toString());
if(playerCards.cardsValue() > 21){
System.out.println("Bust. Currently valued at: " + playerCards.cardsValue());
playerMoney -= playerBet;
endRound = true;
break;
}
}
if(response == 2){
break;
}
}
System.out.println("Dealer Cards:" + dealerCards.toString());
if((dealerCards.cardsValue() > playerCards.cardsValue())&&endRound == false){
System.out.println("Dealer beats you " + dealerCards.cardsValue() + " to " + playerCards.cardsValue());
playerMoney -= playerBet;
endRound = true;
}
while((dealerCards.cardsValue() < 17) && endRound == false){
dealerCards.draw(playingDeck);
System.out.println("Dealer draws: " + dealerCards.getCard(dealerCards.deckSize()-1).toString());
}
System.out.println("Dealers hand value: " + dealerCards.cardsValue());
if((dealerCards.cardsValue()>21)&& endRound == false){
System.out.println("Dealer Busts. You win!");
playerMoney += playerBet;
endRound = true;
}
if((dealerCards.cardsValue() == playerCards.cardsValue()) && endRound == false){
System.out.println("Push.");
endRound = true;
}
if((playerCards.cardsValue() > dealerCards.cardsValue()) && endRound == false){
System.out.println("You win the hand.");
playerMoney += playerBet;
endRound = true;
}
else if(endRound == false)
{
System.out.println("Dealer wins.");
playerMoney -= playerBet;
}
playerCards.moveAllToDeck(playingDeck);
dealerCards.moveAllToDeck(playingDeck);
System.out.println("End of Hand.");
}
System.out.println("Game over! You lost all your money. :(");
userInput.close();
}
}