-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.java
131 lines (110 loc) · 5.46 KB
/
Table.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
import java.util.Scanner;
public class Table {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Black Jack!");
System.out.print("Enter the amount of money you want to put in: ");
double totalIn = scan.nextDouble();
scan.nextLine(); // Consume the newline character
String choice = "";
Deck deck = new Deck();
while (totalIn > 0 && !choice.equalsIgnoreCase("Collect")) {
System.out.println("\nYour current balance: $" + totalIn);
System.out.print("Enter your bet (or type 'all in'): ");
String betInput = scan.nextLine();
double bet = betInput.equalsIgnoreCase("all in") ? totalIn : Double.parseDouble(betInput);
if (bet > totalIn) {
System.out.println("You don't have enough money to make that bet. Try again.");
continue;
}
Player player = new Player();
Player dealer = new Player();
// Deal initial cards
player.addCard(deck.drawCard());
player.addCard(deck.drawCard());
dealer.addCard(deck.drawCard());
dealer.addCard(deck.drawCard());
System.out.println("Your cards: " + player.getHand() + " (Total: " + player.getTotal() + ")");
System.out.println("Dealer's face-up card: " + dealer.getFaceUpCard());
// Check for dealer blackjack
if (dealer.getFaceUpCard().equals("1/11") && dealer.hasHiddenTen()) {
System.out.println("Dealer has a blackjack!");
if (player.hasBlackjack()) {
System.out.println("It's a tie! Both have blackjack.");
} else {
System.out.println("You lose this round.");
totalIn -= bet;
}
continue;
}
// Check for player blackjack
if (player.hasBlackjack()) {
System.out.println("Blackjack! You win 1.5x your bet.");
totalIn += bet * 1.5;
continue; // Round ends immediately
}
// Player's turn
boolean playerTurn = true;
while (playerTurn && player.getTotal() < 21) {
System.out.print("Do you want to 'Hit', 'Stand', or 'Double Down'? ");
String action = scan.nextLine();
if (action.equalsIgnoreCase("Hit")) {
int newCard = deck.drawCard();
player.addCard(newCard);
System.out.println("You drew a " + formatCard(newCard) + ". Your cards: " + player.getHand() + " (Total: " + player.getTotal() + ")");
} else if (action.equalsIgnoreCase("Stand")) {
playerTurn = false;
} else if (action.equalsIgnoreCase("Double Down")) {
if (bet > totalIn - bet) {
System.out.println("You don't have enough money to double down. Choose another action.");
continue;
}
totalIn -= bet;
bet *= 2;
int newCard = deck.drawCard();
player.addCard(newCard);
System.out.println("You drew a " + formatCard(newCard) + ". Your cards: " + player.getHand() + " (Total: " + player.getTotal() + ")");
playerTurn = false;
} else {
System.out.println("Invalid choice. Please choose 'Hit', 'Stand', or 'Double Down'.");
}
}
if (player.getTotal() > 21) {
System.out.println("You busted with a total of " + player.getTotal() + "! You lose this round.");
totalIn -= bet;
continue;
}
System.out.println("Your final hand: " + player.getHand() + " (Total: " + player.getTotal() + ")");
// Dealer's turn
System.out.println("Dealer's turn:");
System.out.println("Dealer's current hand: " + dealer.getHand());
while (dealer.getTotal() < 17) {
int newCard = deck.drawCard();
dealer.addCard(newCard);
System.out.println("Dealer drew a " + formatCard(newCard) + ". Dealer's hand: " + dealer.getHand() + " (Total: " + dealer.getTotal() + ")");
}
System.out.println("Dealer's final hand: " + dealer.getHand() + " (Total: " + dealer.getTotal() + ")");
// Determine winner
if (dealer.getTotal() > 21 || player.getTotal() > dealer.getTotal()) {
System.out.println("You win this round!");
totalIn += bet;
} else if (player.getTotal() < dealer.getTotal()) {
System.out.println("You lose this round.");
totalIn -= bet;
} else {
System.out.println("It's a tie! Your bet is returned.");
}
// Prompt to continue or collect
if (totalIn > 0) {
System.out.print("Do you want to play another round or 'Collect' your winnings? ");
choice = scan.nextLine();
}
}
System.out.println("\nYou leave the table with $" + totalIn);
System.out.println("Thanks for playing!");
scan.close();
}
private static String formatCard(int card) {
return card == 1 ? "1/11" : String.valueOf(card);
}
}