BlackJack
Чё по Advance?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BlackjackGame extends JFrame {
private Deck deck;
private final Player player;
private final Player dealer;
private int currentBet;
private boolean gameInProgress;
private JPanel playerPanel;
private JPanel dealerPanel;
private JLabel playerScoreLabel;
private JLabel dealerScoreLabel;
private JLabel balanceLabel;
private JLabel messageLabel;
private JButton hitButton;
private JButton standButton;
private JButton betButton;
public BlackjackGame() {
setTitle("Blackjack");
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
deck = new Deck();
player = new Player("Игрок");
dealer = new Player("Дилер");
gameInProgress = false;
setupUI();
updateUI();
}
private void setupUI() {
JPanel cardsPanel = new JPanel(new GridLayout(2, 1, 0, 20));
cardsPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
cardsPanel.setBackground(new Color(53, 101, 77));
JPanel dealerArea = new JPanel(new BorderLayout());
dealerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
dealerPanel.setBackground(new Color(53, 101, 77));
dealerScoreLabel = new JLabel("Дилер: ?", SwingConstants.CENTER);
dealerScoreLabel.setFont(new Font("Arial", Font.BOLD, 16));
dealerScoreLabel.setForeground(Color.WHITE);
dealerScoreLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
dealerArea.add(dealerPanel, BorderLayout.CENTER);
dealerArea.add(dealerScoreLabel, BorderLayout.SOUTH);
dealerArea.setBackground(new Color(53, 101, 77));
JPanel playerArea = new JPanel(new BorderLayout());
playerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
playerPanel.setBackground(new Color(53, 101, 77));
playerScoreLabel = new JLabel("Счёт: 0", SwingConstants.CENTER);
playerScoreLabel.setFont(new Font("Arial", Font.BOLD, 16));
playerScoreLabel.setForeground(Color.WHITE);
playerScoreLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
playerArea.add(playerPanel, BorderLayout.CENTER);
playerArea.add(playerScoreLabel, BorderLayout.SOUTH);
playerArea.setBackground(new Color(53, 101, 77));
cardsPanel.add(dealerArea);
cardsPanel.add(playerArea);
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
controlPanel.setBackground(new Color(53, 101, 77));
betButton = new JButton("Ставка");
hitButton = new JButton("Взять");
standButton = new JButton("Остановиться");
Font buttonFont = new Font("Arial", Font.BOLD, 14);
betButton.setFont(buttonFont);
hitButton.setFont(buttonFont);
standButton.setFont(buttonFont);
betButton.setBackground(new Color(255, 215, 0));
hitButton.setBackground(new Color(220, 220, 220));
standButton.setBackground(new Color(220, 220, 220));
hitButton.setEnabled(false);
standButton.setEnabled(false);
balanceLabel = new JLabel("Баланс: $" + player.getBalance());
messageLabel = new JLabel("Сделайте ставку!");
Font labelFont = new Font("Arial", Font.BOLD, 16);
balanceLabel.setFont(labelFont);
messageLabel.setFont(labelFont);
balanceLabel.setForeground(Color.WHITE);
messageLabel.setForeground(Color.YELLOW);
controlPanel.add(betButton);
controlPanel.add(hitButton);
controlPanel.add(standButton);
controlPanel.add(balanceLabel);
controlPanel.add(messageLabel);
betButton.addActionListener(e -> placeBet());
hitButton.addActionListener(this::playerHit);
standButton.addActionListener(e -> dealerTurn());
add(cardsPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
}
private void startNewRound() {
deck = new Deck();
deck.shuffle();
player.clearHand();
dealer.clearHand();
player.addCard(deck.drawCard());
player.addCard(deck.drawCard());
dealer.addCard(deck.drawCard());
dealer.addCard(deck.drawCard());
gameInProgress = true;
hitButton.setEnabled(true);
standButton.setEnabled(true);
betButton.setEnabled(false);
updateUI();
if (player.hasBlackjack()) {
endRound("Blackjack! Вы выиграли x3 ставки!", currentBet * 3);
}
}
private void placeBet() {
String betStr = JOptionPane.showInputDialog(this, "Ваша ставка (1-" + player.getBalance() + "):");
try {
int bet = Integer.parseInt(betStr);
if (bet < 1 || bet > player.getBalance()) {
JOptionPane.showMessageDialog(this, "Некорректная ставка!");
return;
}
currentBet = bet;
player.placeBet(bet);
startNewRound();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Введите число!");
}
}
private void playerHit(ActionEvent e) {
player.addCard(deck.drawCard());
updateUI();
if (player.isBusted()) {
endRound("Перебор! Вы проиграли.", 0);
}
}
private void dealerTurn() {
gameInProgress = false;
while (dealer.getScore() < 17) {
dealer.addCard(deck.drawCard());
}
updateUI();
checkWinner();
}
private void checkWinner() {
if (dealer.isBusted()) {
endRound("Дилер перебрал! Вы выиграли x2 ставки!", currentBet * 2);
} else if (player.getScore() > dealer.getScore()) {
endRound("Вы выиграли x2 ставки!", currentBet * 2);
} else if (player.getScore() < dealer.getScore()) {
endRound("Дилер выиграл.", 0);
} else {
endRound("Ничья! Ставка возвращена.", currentBet);
}
}
private void endRound(String message, int winAmount) {
messageLabel.setText(message);
player.win(winAmount);
balanceLabel.setText("Баланс: $" + player.getBalance());
hitButton.setEnabled(false);
standButton.setEnabled(false);
betButton.setEnabled(true);
gameInProgress = false;
updateUI();
if (player.getBalance() <= 0) {
JOptionPane.showMessageDialog(this, "Игра окончена! Деньги закончились.");
System.exit(0);
}
}
private void updateUI() {
playerPanel.removeAll();
dealerPanel.removeAll();
for (Card card : player.getHand()) {
playerPanel.add(createCardPanel(card, false));
}
for (int i = 0; i < dealer.getHand().size(); i++) {
boolean hidden = gameInProgress && i == 0;
dealerPanel.add(createCardPanel(dealer.getHand().get(i), hidden));
}
playerScoreLabel.setText("Счёт: " + player.getScore());
dealerScoreLabel.setText("Дилер: " + (gameInProgress ? "?" : dealer.getScore()));
playerPanel.revalidate();
playerPanel.repaint();
dealerPanel.revalidate();
dealerPanel.repaint();
}
private JPanel createCardPanel(Card card, boolean hidden) {
JPanel cardPanel = new JPanel(new BorderLayout());
cardPanel.setPreferredSize(new Dimension(80, 120));
cardPanel.setBackground(Color.WHITE);
cardPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
JLabel cardLabel = new JLabel();
cardLabel.setHorizontalAlignment(SwingConstants.CENTER);
cardLabel.setFont(new Font("Arial", Font.BOLD, 24));
if (hidden) {
cardLabel.setText("?");
cardPanel.setBackground(new Color(70, 70, 70));
cardLabel.setForeground(Color.WHITE);
} else {
String rank = card.getRank();
String suit = card.getSuit();
cardLabel.setText("<html><center>" + rank + "<br>" + suit + "</center></html>");
if (suit.equals("♥") || suit.equals("♦")) {
cardLabel.setForeground(Color.RED);
} else {
cardLabel.setForeground(Color.BLACK);
}
}
cardPanel.add(cardLabel, BorderLayout.CENTER);
return cardPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
BlackjackGame game = new BlackjackGame();
game.setVisible(true);
});
}
}
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;
}
@Override
public String toString() {
return rank + suit;
}
}
class Deck {
private List<Card> cards;
private static final String[] RANKS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private static final String[] SUITS = {"♥", "♦", "♣", "♠"};
public Deck() {
cards = new ArrayList<>();
initializeDeck();
}
private void initializeDeck() {
for (String suit : SUITS) {
for (String rank : RANKS) {
cards.add(new Card(suit, rank));
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card drawCard() {
if (cards.isEmpty()) {
throw new IllegalStateException("Колода пуста!");
}
return cards.remove(0);
}
}
class Player {
private final String name;
private final List<Card> hand;
private int score;
private int balance;
public Player(String name) {
this.name = name;
this.hand = new ArrayList<>();
this.score = 0;
this.balance = 1000;
}
public void addCard(Card card) {
hand.add(card);
updateScore();
}
private void updateScore() {
score = 0;
int aces = 0;
for (Card card : hand) {
String rank = card.getRank();
if (rank.equals("A")) {
aces++;
score += 11;
} else if (rank.equals("K") || rank.equals("Q") || rank.equals("J")) {
score += 10;
} else {
score += Integer.parseInt(rank);
}
}
while (score > 21 && aces > 0) {
score -= 10;
aces--;
}
}
public int getScore() {
return score;
}
public List<Card> getHand() {
return hand;
}
public boolean hasBlackjack() {
return hand.size() == 2 && score == 21;
}
public boolean isBusted() {
return score > 21;
}
public void clearHand() {
hand.clear();
score = 0;
}
public int getBalance() {
return balance;
}
public void placeBet(int bet) {
balance -= bet;
}
public void win(int amount) {
balance += amount;
}
}