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.Random;
- public class BJ extends JFrame {
- private JButton hit;
- private JButton stay;
- private JPanel buttons;
- private Random random = new Random();
- 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; //reset and shuffle at what threshold?
- private JPanel cards = new JPanel();
- public BJ() {
- super("BlackJack");
- setLayout(new BorderLayout());
- hit = new JButton("HIT");
- hit.addActionListener (
- new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == hit) {
- displayCard();
- }
- }
- }
- );
- stay = new JButton("STAY");
- stay.addActionListener(
- new ActionListener() {
- public void actionPerformed (ActionEvent e) {
- if (e.getSource() == stay) {}
- }
- }
- );
- buttons = new JPanel();
- buttons.add(hit);
- buttons.add(stay);
- add(buttons, BorderLayout.SOUTH);
- add(cards, BorderLayout.CENTER);
- fillDeck(deck);
- shuffleCards();
- }
- 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 displayCard() {
- String cardName[] = deck[currentCard].split(" ");
- String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
- Icon card = new ImageIcon(getClass().getResource(cardFile));
- add(new JLabel(card));
- currentCard++;
- }
- 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(500, 500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement