import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Card {
private final String suit;
private final String rank;
public Card(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public String getSuit() {
return suit;
}
public String getRank() {
return rank;
}
public int getValue() {
switch (rank) {
case "Ace":
return 11;
case "King":
case "Queen":
case "Jack":
return 10;
default:
return Integer.parseInt(rank);
}
}
@Override
public String toString() {
return rank + " of " + suit;
}
}
class Deck {
private final List<Card> cards;
public Deck() {
cards = new ArrayList<>();
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
for (String suit : suits) {
for (String rank : ranks) {
cards.add(new Card(suit, rank));
}
}
shuffle();
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card dealCard() {
if (cards.isEmpty()) {
throw new IllegalStateException("Deck is empty");
}
return cards.remove(0);
}
}
class BlackjackHand {
private final List<Card> cards;
public BlackjackHand() {
cards = new ArrayList<>();
}
public void addCard(Card card) {
cards.add(card);
}
public int getHandValue() {
int value = 0;
int numAces = 0;
for (Card card : cards) {
value += card.getValue();
if (card.getRank().equals("Ace")) {
numAces++;
}
}
while (numAces > 0 && value > 21) {
value -= 10;
numAces--;
}
return value;
}
public List<Card> getCards() {
return cards;
}
}
public class BlackjackGame extends JFrame {
private final Deck deck;
private final BlackjackHand playerHand;
private final BlackjackHand dealerHand;
private JButton hitButton;
private JButton standButton;
private JButton newGameButton;
private JLabel playerLabel;
private JLabel dealerLabel;
private JLabel messageLabel;
private JLabel turnLabel;
public BlackjackGame() {
super("Blackjack Game");
deck = new Deck();
playerHand = new BlackjackHand();
dealerHand = new BlackjackHand();
initializeUI();
dealInitialCards();
}
private void initializeUI() {
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
playerLabel = new JLabel("Player's Hand: ");
dealerLabel = new JLabel("Dealer's Hand: ");
messageLabel = new JLabel(" ");
hitButton = new JButton("Hit");
standButton = new JButton("Stand");
newGameButton = new JButton("New Game");
JPanel buttonPanel = new JPanel();
buttonPanel.add(hitButton);
buttonPanel.add(standButton);
buttonPanel.add(newGameButton);
JPanel turnPanel = new JPanel();
turnLabel = new JLabel("Turn: Player"); // Initial turn is for the player
turnPanel.add(turnLabel);
add(turnPanel, BorderLayout.WEST); // Add turnPanel to the left side of the frame
hitButton.addActionListener(e -> handleHit());
standButton.addActionListener(e -> handleStand());
newGameButton.addActionListener(e -> handleNewGame());
add(playerLabel, BorderLayout.NORTH);
add(dealerLabel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END); // Use PAGE_END instead of SOUTH
add(messageLabel, BorderLayout.SOUTH); // Move messageLabel to SOUTH
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void dealInitialCards() {
playerHand.addCard(deck.dealCard());
dealerHand.addCard(deck.dealCard());
playerHand.addCard(deck.dealCard());
dealerHand.addCard(deck.dealCard());
updateUI();
}
private void handleHit() {
// Player's turn
playerHand.addCard(deck.dealCard());
updateUI();
if (playerHand.getHandValue() > 21) {
endGame("You've busted! Sorry, you lose.");
}
}
private void handleStand() {
// Player's turn ends, dealer's turn begins
turnLabel.setText("Turn: Dealer");
while (dealerHand.getHandValue() < 17) {
dealerHand.addCard(deck.dealCard());
}
updateUI();
determineWinner(); // This method should be responsible for checking both player's and dealer's hands
}
private void handleNewGame() {
playerHand.getCards().clear();
dealerHand.getCards().clear();
deck.shuffle();
dealInitialCards();
messageLabel.setText(" ");
// Re-enable Hit and Stand buttons
hitButton.setEnabled(true);
standButton.setEnabled(true);
// Reset turn label to "Player"
turnLabel.setText("Turn: Player");
}
private void endGame(String message) {
updateUI();
messageLabel.setText(message);
hitButton.setEnabled(false);
standButton.setEnabled(false);
}
private void updateUI() {
updateLabel(playerLabel, "Player's Hand: ", playerHand);
updateLabel(dealerLabel, "Dealer's Hand: ", dealerHand);
}
private void updateLabel(JLabel label, String labelText, BlackjackHand hand) {
StringBuilder builder = new StringBuilder(labelText);
for (Card card : hand.getCards()) {
builder.append(card.toString()).append(" | ");
}
builder.append("Value: ").append(hand.getHandValue());
label.setText(builder.toString());
}
private void determineWinner() {
// Check both hands and determine the winner
if (playerHand.getHandValue() > 21) {
endGame("You've busted! Sorry, you lose.");
} else if (dealerHand.getHandValue() > 21 || dealerHand.getHandValue() < playerHand.getHandValue()) {
endGame("You win! Dealer has busted with " + dealerHand.getHandValue() + ".");
} else if (dealerHand.getHandValue() == playerHand.getHandValue()) {
endGame("It's a tie! Dealer wins on a tie.");
} else {
endGame("Sorry, you lose. Dealer wins with " + dealerHand.getHandValue() + " to " + playerHand.getHandValue() + ".");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new BlackjackGame());
}
}