-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.java
More file actions
118 lines (100 loc) · 4.29 KB
/
Blackjack.java
File metadata and controls
118 lines (100 loc) · 4.29 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
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args){
//Welcome Message
System.out.println("Welcome to Blackjack!");
//Create our playing deck
Deck playingDeck = new Deck();
playingDeck.createFullDeck();
playingDeck.shuffle();
//Create a deck for the player
Deck playerDeck = new Deck();
Deck dealerDeck = new Deck();
double playerMoney = 100.00;
Scanner userInput = new Scanner(System.in);
//Game Loop
while(playerMoney > 0){
//Play
//Take the players bet
System.out.printf("You have %.2f, how much would like to bet?\n", playerMoney);
double playerBet = userInput.nextDouble();
if(playerBet > playerMoney){
System.out.println("Cheating. Please leave");
break;
}
boolean endRound = false;
//Start Dealing
//Player gets two cards
playerDeck.draw(playingDeck);
playerDeck.draw(playingDeck);
//Dealer gets two cards
dealerDeck.draw(playingDeck);
dealerDeck.draw(playingDeck);
while(true){
System.out.println("Your hand:");
System.out.println(playerDeck.toString());
System.out.println("Your deck is value at: "+playerDeck.cardsValue());
//Display Dealer Hand
System.out.println("Dealer Hand: "+ dealerDeck.getCard(0).toString() + " and [Hidden]");
//What does the player want to do?
System.out.println("Would you like to (1)HIT or (2)STAND");
int response = userInput.nextInt();
//HIT
if(response == 1){
playerDeck.draw(playingDeck);
System.out.println("You draw a:" + playerDeck.getCard(playerDeck.deckSize()-1).toString());
//Bust if > 21
if(playerDeck.cardsValue() > 21){
System.out.println("Bust. Currently valued at: "+ playerDeck.cardsValue());
playerMoney -= playerBet;
endRound = true;
break;
}
}
if(response==2){
break;
}
}
//Reveal Dealers Cards
System.out.println("Dealer Cards: " + dealerDeck.toString());
//See if dealer has more points than player
if((dealerDeck.cardsValue() > playerDeck.cardsValue()) && endRound == false){
System.out.println("Dealer beats you!");
playerMoney -= playerBet;
endRound = true;
}
//Dealer draws at 16, stand a 17
while((dealerDeck.cardsValue() < 17) && endRound == false){
dealerDeck.draw(playingDeck);
System.out.println("Dealer Draws " + dealerDeck.getCard(dealerDeck.deckSize()-1).toString());
}
//Display Total Value for Dealer
System.out.println("Dealer's Hand is valued at: " + dealerDeck.cardsValue());
//Determine if dealer buster
if((dealerDeck.cardsValue() > 21) && endRound == false){
System.out.println("Dealer busts! You win.");
playerMoney += playerBet;
endRound = true;
}
//Determine if push
if((playerDeck.cardsValue() == dealerDeck.cardsValue()) && endRound == false){
System.out.println("Push");
endRound = true;
}
if((playerDeck.cardsValue() > dealerDeck.cardsValue()) && endRound == false){
System.out.println("You win the hand!");
playerMoney += playerBet;
endRound = true;
}
else if(endRound == false){
System.out.println("You lose the hand");
playerMoney -= playerBet;
endRound = true;
}
playerDeck.moveAllToDeck(playerDeck);
dealerDeck.moveAllToDeck(playerDeck);
System.out.println("End of hand");
}
System.out.println("Game over! You are out of money-.");
}
}