Код: Выделить всё
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BlackjackGUI {
public JFrame frame;
public JPanel gamePanel;
// Define the dimensions of the game board
public int boardWidth = 600;
public int boardHeight = boardWidth; // This will be 600
int cardWidth = 110;
int cardHeight = 154;
Deck deck;
Card card;
JButton hitButton;
JButton stayButton;
public BlackjackGUI() {
frame = new JFrame("Blackjack");
frame.setSize(boardWidth, boardHeight); // Set the size of the frame
frame.setLocationRelativeTo(null); // Center the frame
frame.setResizable(false); // Make the frame non-resizable
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set default close operation
deck = new Deck(); // Create a new deck and initialize the game
// Create and configure the game panel
gamePanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
Image hiddenCardImage = new ImageIcon(getClass().getResource("./cards/BACK.png")).getImage();
if (!stayButton.isEnabled()) {
hiddenCardImage = new ImageIcon(getClass().getResource(deck.hiddenCard.getImagePath())).getImage();
}
g.drawImage(hiddenCardImage, 20, 20, cardWidth, cardHeight, null);
for (int i = 0; i < deck.dealerHand.size(); i++) {
Card card = deck.dealerHand.get(i);
// Test// System.out.println(card.getImagePath());
Image cardImg = new ImageIcon(getClass().getResource(card.getImagePath())).getImage();
g.drawImage(cardImg, cardWidth + 25 + (cardWidth + 5) * i, 20, cardWidth, cardHeight, null);
}
for (int i = 0; i < deck.playerHand.size(); i++) {
Card card = deck.playerHand.get(i);
Image cardImg = new ImageIcon(getClass().getResource(card.getImagePath())).getImage();
g.drawImage(cardImg, 20 + (cardWidth + 5) * i, 320, cardWidth, cardHeight, null);
}
if (!stayButton.isEnabled() || deck.dealerSum == 21) {
String message = getResultMessage();
g.setFont(new Font("Arial", Font.PLAIN, 30));
g.setColor(Color.white);
g.drawString(message, 220, 250);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
gamePanel.setLayout(new BorderLayout());
gamePanel.setBackground(new Color(53, 101, 77));
// Add buttons
JPanel buttonPanel = new JPanel();
hitButton = new JButton("Hit!");
stayButton = new JButton("Stand!");
hitButton.setFocusable(false);
hitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hit();
if (reducePlayerAce() >= 21) {
hitButton.setEnabled(false);
stayButton.setEnabled(false);
}
}
});
stayButton.setFocusable(false);
stayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stay();
hitButton.setEnabled(false);
stayButton.setEnabled(false);
}
});
buttonPanel.add(hitButton);
buttonPanel.add(stayButton);
gamePanel.add(buttonPanel, BorderLayout.SOUTH); // Add the button panel to the game panel
// Add the game panel to the frame
frame.add(gamePanel);
// Check if the dealer has blackjack immediately after the initial deal
if (deck.dealerSum == 21) {
hitButton.setEnabled(false);
stayButton.setEnabled(false);
gamePanel.repaint();
} else if (deck.playerSum == 21) {
hitButton.setEnabled(false);
stayButton.setEnabled(false);
}
frame.setVisible(true); // Make the frame visible
}
private void hit() {
// Example action for the hit button
System.out.println("Hit button pressed");
Card card = deck.deck.remove(deck.deck.size() - 1);
deck.playerSum += card.getValue();
deck.playerAceCount += card.isAce() ? 1 : 0;
deck.playerHand.add(card);
gamePanel.repaint();
}
public int reducePlayerAce() {
while (deck.playerSum > 21 && deck.playerAceCount > 0) {
deck.playerSum -= 10;
deck.playerAceCount -= 1;
}
return deck.playerSum;
}
private void stay() {
// Example action for the stay button
System.out.println("Stay button pressed");
while (deck.dealerSum < 17) {
Card card = deck.deck.remove(deck.deck.size() - 1);
deck.dealerSum += card.getValue();
deck.dealerAceCount += card.isAce() ? 1 : 0;
deck.dealerHand.add(card);
}
// Reduce aces after the dealer stops drawing cards
reduceDealerAce();
reducePlayerAce();
// Trigger repaint to show final state and message
gamePanel.repaint();
}
public int reduceDealerAce() {
while (deck.dealerSum > 21 && deck.dealerAceCount > 0) {
deck.dealerSum -= 10;
deck.dealerAceCount -= 1;
}
return deck.dealerSum;
}
private String getResultMessage() {
if (deck.dealerSum == 21 && deck.dealerHand.size() == 2) {
return "You Lose";
} else if (deck.playerSum > 21) {
return "You Lose";
} else if (deck.dealerSum > 21) {
return "You Win";
} else if (deck.playerSum == deck.dealerSum) {
return "PUSH";
} else if (deck.playerSum > deck.dealerSum) {
return "You Win";
} else {
return "You Lose";
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new BlackjackGUI());
}
}
Код: Выделить всё
public class Card {
String value;
String type;
// Constructor to create a card with value and type
public Card(String value, String type) {
this.value = value;
this.type = type;
}
// Override the toString method to return the card's value and type as a string
@Override
public String toString() {
return value + "-" + type;
}
// Method to get the card's value in points
public int getValue() {
// Check if the value is one of AJQK
if ("AJQK".contains(value)) {
if (value.equals("A")) {
return 11; // Ace is worth 11 points
}
return 10; // J, Q, and K are worth 10 points
}
// If it's a numeric value, return it as an int
return Integer.parseInt(value);
}
// Method to check if the card is an ace
public boolean isAce() {
return value.equals("A");
}
// Method to get the path to the card's image
public String getImagePath() {
return "./cards/" + toString() + ".png";
}
}
Код: Выделить всё
import java.util.ArrayList;
import java.util.Random;
public class Deck {
ArrayList deck;
Random random = new Random();
// Dealer
Card hiddenCard;
ArrayList dealerHand;
int dealerSum;
int dealerAceCount;
// Player
ArrayList playerHand;
int playerSum;
int playerAceCount;
// Constructor to build and shuffle the deck, and deal the initial cards
public Deck() {
buildDeck();
shuffleDeck();
dealerHand = new ArrayList();
dealerSum = 0;
dealerAceCount = 0;
// Deal the first card to the dealer (hidden card)
hiddenCard = deck.remove(deck.size() - 1);
dealerSum += hiddenCard.getValue();
dealerAceCount += hiddenCard.isAce() ? 1 : 0;
// Deal the second card to the dealer
Card card = deck.remove(deck.size() - 1);
dealerSum += card.getValue();
dealerAceCount += card.isAce() ? 1 : 0;
dealerHand.add(card);
System.out.println("DEALER:");
System.out.println(hiddenCard);
System.out.println(dealerHand);
System.out.println(dealerSum);
System.out.println(dealerAceCount);
// Initialize the player's hand and deal two cards to the player
playerHand = new ArrayList();
playerSum = 0;
playerAceCount = 0;
for (int i = 0; i < 2; i++) {
card = deck.remove(deck.size() - 1);
playerSum += card.getValue();
playerAceCount += card.isAce() ? 1 : 0;
playerHand.add(card);
}
System.out.println("PLAYER:");
System.out.println(playerHand);
System.out.println(playerSum);
System.out.println(playerAceCount);
}
// Method to build the deck with all 52 cards
public void buildDeck() {
deck = new ArrayList();
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] types = {"C", "D", "H", "S"};
for (int i = 0; i < types.length; i++) {
for (int j = 0; j < values.length; j++) {
Card card = new Card(values[j], types[i]);
deck.add(card);
}
}
System.out.println("BUILD DECK:");
for (Card card : deck) {
System.out.println(card);
}
}
// Method to shuffle the deck
public void shuffleDeck() {
for (int i = 0; i < deck.size(); i++) {
int j = random.nextInt(deck.size());
Card currCard = deck.get(i);
Card randomCard = deck.get(j);
deck.set(i, randomCard);
deck.set(j, currCard);
}
System.out.println("AFTER SHUFFLE");
System.out.println(deck);
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... lehandling