Advertisement
Guest User

Untitled

a guest
Sep 27th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. public class BJ extends JFrame {
  7.  
  8. private JButton hit;
  9. private JButton stay;
  10. private JPanel buttons;
  11. private Random random = new Random();
  12. private String deck[] = new String[52];
  13. private String faces[] = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"};
  14. private String suits[] = {"spades", "hearts", "diamonds", "clubs"};
  15. private int currentCard = 0; //reset and shuffle at what threshold?
  16. private JPanel cards = new JPanel();
  17.  
  18. public BJ() {
  19.  
  20. super("BlackJack");
  21. setLayout(new BorderLayout());
  22. hit = new JButton("HIT");
  23.  
  24. hit.addActionListener (
  25.  
  26. new ActionListener() {
  27.  
  28. public void actionPerformed (ActionEvent e) {
  29.  
  30. if (e.getSource() == hit) {
  31.  
  32. displayCard();
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. );
  41.  
  42. stay = new JButton("STAY");
  43.  
  44. stay.addActionListener(
  45.  
  46. new ActionListener() {
  47.  
  48. public void actionPerformed (ActionEvent e) {
  49.  
  50. if (e.getSource() == stay) {}
  51.  
  52. }
  53.  
  54. }
  55.  
  56. );
  57.  
  58. buttons = new JPanel();
  59. buttons.add(hit);
  60. buttons.add(stay);
  61. add(buttons, BorderLayout.SOUTH);
  62. add(cards, BorderLayout.CENTER);
  63.  
  64. fillDeck(deck);
  65. shuffleCards();
  66.  
  67. }
  68.  
  69. public void shuffleCards() {
  70.  
  71. for (int i=0; i<deck.length; i++) {
  72.  
  73. int num = random.nextInt(52);
  74. String temp = deck[i];
  75. deck[i] = deck[num];
  76. deck[num] = temp;
  77.  
  78. }
  79.  
  80. }
  81.  
  82. public void displayCard() {
  83.  
  84. String cardName[] = deck[currentCard].split(" ");
  85. String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
  86. Icon card = new ImageIcon(getClass().getResource(cardFile));
  87. add(new JLabel(card));
  88. currentCard++;
  89.  
  90. }
  91.  
  92. public void fillDeck (String[] array) {
  93.  
  94. for (int i= 0; i<array.length; i++) {
  95.  
  96. array[i] = faces[i % 13] + " " + suits[i/13];
  97.  
  98. }
  99.  
  100. }
  101.  
  102. public static void main (String[] args) {
  103.  
  104. BJ bj = new BJ();
  105. bj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106. bj.setVisible(true);
  107. bj.setSize(500, 500);
  108.  
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement