Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Random;
- public class BJ extends JFrame {
- private GridBagLayout layout = new GridBagLayout();
- private GridBagConstraints gbc = new GridBagConstraints();
- private Random random = new Random();
- private JLabel cards = new JLabel();
- private JButton hit;
- private JButton stay;
- private JButton dealCards;
- private JButton increaseBet;
- private JButton decreaseBet;
- private JPanel topPanel = new JPanel();
- private JPanel playerCards = new JPanel();
- private JPanel dealerCards = new JPanel();
- private JPanel allCards = new JPanel();
- private JPanel leftPanel = new JPanel();
- private JPanel buttons;
- private JTextField playerTotal;
- private JTextField dealerTotal;
- private JTextField result;
- private JTextField bet;
- private String deck[] = new String[52];
- private String faces[] = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"};
- private String suits[] = {"spades", "hearts", "diamonds", "clubs"};
- private int currentCard = 0;
- private boolean gameInProgress = false;
- private int xPosDealer = 0;
- private int xPosPlayer = 0;
- private int currentPlayerTotal = 0;
- private int currentDealerTotal = 0;
- private int betAmount = 0;
- private String playerCardsInHand;
- private String dealerCardsInHand;
- enum Status {WON, LOST, CONTINUE};
- private Status gameStatus;
- public BJ() {
- super("BlackJack");
- setLayout(new BorderLayout());
- hit = new JButton("HIT");
- hit.addActionListener ( new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == hit && gameInProgress == true) {
- displayPlayerCard();
- }
- }
- }
- );
- stay = new JButton("STAY");
- stay.addActionListener(
- new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == stay && gameStatus != Status.LOST) {
- while (currentDealerTotal < 17)
- displayDealerCard();
- }
- gameResult();
- }
- }
- );
- dealCards = new JButton("DEAL CARDS");
- dealCards.addActionListener (new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == dealCards && gameInProgress == false) {
- clearTable();
- startGame();
- checkStatus();
- }
- }
- });
- increaseBet = new JButton("INCREASE BET");
- decreaseBet = new JButton("DECREASE BET");
- increaseBet.addActionListener (new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == increaseBet && gameInProgress == false) {
- betAmount += 5;
- bet.setText(Integer.toString(betAmount));
- }
- }
- });
- playerTotal = new JTextField(10);
- dealerTotal = new JTextField(10);
- result = new JTextField(10);
- playerTotal.setEditable(false);
- dealerTotal.setEditable(false);
- result.setEditable(false);
- buttons = new JPanel();
- buttons.add(hit);
- buttons.add(stay);
- add(buttons, BorderLayout.SOUTH);
- topPanel.add(dealCards);
- topPanel.add(playerTotal);
- topPanel.add(dealerTotal);
- topPanel.add(result);
- add(topPanel, BorderLayout.NORTH);
- playerCards.setLayout(new GridLayout());
- dealerCards.setLayout(new GridLayout());
- fillDeck(deck);
- shuffleCards();
- }
- public void dealerTotal (String cards) {
- for (int i=0; i<faces.length; i++) {
- if (cards.equals(faces[i]) && i <=8)
- currentDealerTotal += i + 2;
- else if (cards.equals(faces[i]) && i == 12) {
- if ( (currentPlayerTotal += 11) > 21)
- currentPlayerTotal += 1;
- if ( (currentPlayerTotal += 11) == 21)
- currentPlayerTotal += 11;
- }
- else
- currentDealerTotal += 10;
- }
- dealerTotal.setText(Integer.toString(currentDealerTotal));
- dealerCardsInHand = "";
- if (currentDealerTotal > 21) {
- gameStatus = Status.WON;
- gameResult();
- }
- }
- public void playerTotal (String cards) {
- for (int i=0; i<faces.length; i++) {
- if (cards.equals(faces[i]) && i <=8)
- currentPlayerTotal += i + 2;
- else if (cards.equals(faces[i]) && i == 12) {
- if ( (currentPlayerTotal += 11) > 21)
- currentPlayerTotal += 1;
- else if ( (currentPlayerTotal += 11) == 21)
- currentPlayerTotal += 11;
- }
- else
- currentPlayerTotal += 10;
- }
- playerTotal.setText(Integer.toString(currentPlayerTotal));
- playerCardsInHand = "";
- checkStatus();
- }
- public void checkStatus () {
- if (currentPlayerTotal > 21) {
- gameStatus = Status.LOST;
- gameResult();
- }
- else if (currentPlayerTotal == 21) {
- while (currentDealerTotal < 17)
- displayDealerCard();
- gameResult();
- }
- else if (currentDealerTotal >= 17 && currentPlayerTotal > currentDealerTotal) {
- gameStatus = Status.WON;
- gameResult();
- }
- }
- public void startGame() {
- currentPlayerTotal = 0;
- currentDealerTotal = 0;
- displayPlayerCard();
- displayPlayerCard();
- displayDealerCard();
- displayDealerCard();
- gameInProgress = true;
- gameStatus = Status.CONTINUE;
- xPosPlayer = 3;
- xPosDealer = 3;
- if (currentCard > 40) {
- currentCard = 0;
- shuffleCards();
- }
- }
- public void gameResult() {
- if (gameStatus == Status.WON)
- result.setText("You Win!");
- else if (gameStatus == Status.LOST)
- result.setText("You Lost!");
- else if (currentDealerTotal > currentPlayerTotal)
- result.setText("You Lost!");
- else if (currentDealerTotal == currentPlayerTotal)
- result.setText("Push");
- else
- result.setText("You Win!");
- gameInProgress = false;
- }
- public void clearTable() {
- playerCards.removeAll();
- playerCards.revalidate();
- playerCards.repaint();
- dealerCards.removeAll();
- dealerCards.revalidate();
- dealerCards.repaint();
- result.setText("");
- playerCardsInHand = "";
- dealerCardsInHand = "";
- }
- public void shuffleCards() {
- for (int i=0; i<deck.length; i++) {
- int num = random.nextInt(52);
- String temp = deck[i];
- deck[i] = deck[num];
- deck[num] = temp;
- }
- }
- public void displayPlayerCard() {
- String cardName[] = deck[currentCard].split(" ");
- playerCardsInHand = cardName[0];
- String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
- Icon card = new ImageIcon(getClass().getResource(cardFile));
- playerCards.add(new JLabel(card));
- playerCards.revalidate();
- playerCards.repaint();
- add(playerCards, BorderLayout.WEST);
- currentCard++;
- playerTotal(playerCardsInHand);
- }
- public void displayDealerCard() {
- String cardName[] = deck[currentCard].split(" ");
- dealerCardsInHand = cardName[0];
- String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
- Icon card = new ImageIcon(getClass().getResource(cardFile));
- dealerCards.add(new JLabel(card));
- dealerCards.revalidate();
- dealerCards.repaint();
- add(dealerCards, BorderLayout.EAST);
- currentCard++;
- dealerTotal(dealerCardsInHand);
- }
- public void fillDeck (String[] array) {
- for (int i= 0; i<array.length; i++) {
- array[i] = faces[i % 13] + " " + suits[i/13];
- }
- }
- public static void main (String[] args) {
- BJ bj = new BJ();
- bj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- bj.setVisible(true);
- bj.setSize(750, 750);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement