/* * frmMain.java * * Created on January 23, 2008, 8:04 AM * * @author jpurdum */ public class frmMain extends javax.swing.JFrame { /** Creates new form frmMain */ public frmMain() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // private void initComponents() { btnShuffle = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); txtArea = new javax.swing.JTextArea(); btnClose = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Shuffle Deck"); btnShuffle.setText("Shuffle"); btnShuffle.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnShuffleActionPerformed(evt); } }); txtArea.setColumns(20); txtArea.setRows(5); jScrollPane1.setViewportView(txtArea); btnClose.setText("Close"); btnClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCloseActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(btnShuffle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(224, 224, 224) .add(btnClose)) .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(btnShuffle) .add(btnClose)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(23, Short.MAX_VALUE)) ); pack(); }// private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(1); } private void btnShuffleActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i; int index = 1; String buff = ""; ClsCardDeck myDeck = new ClsCardDeck(); myDeck.shuffleDeck(); // Shuffle it for (i = 1; i < 53; i++) { // Show the cards buff += myDeck.ShowCard(index++) + " "; if (i % 13 == 0) buff += "\n"; } index = myDeck.getPassCount(); buff += "\n Passes to shuffle: " + Integer.toString(index); txtArea.setText(buff); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new frmMain().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnClose; private javax.swing.JButton btnShuffle; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea txtArea; // End of variables declaration } //=================================================================================================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author jpurdum */ import java.util.Random; public class ClsCardDeck { // ========================= Instance variables ======================= private final int DECKSIZE = 52; // The number of cards in the deck private int nextCard; // The next card to be dealth from deck private int[] deck; // The deck of cards. private int[] bigDeck; // In case they use multiple decks private int passCount; // Used to see how long we're stuck in the loop that fills the deck private int decksToUse = 1; // Number of decks the user wants to use private static String[] translate = {"", "Ace", "Two", "Three", "Four", "Five","Six", "Seven", "Eight","Nine", "Ten", "Jack", "Queen","King"}; private static String[] suits = {"S","H","D","C"}; // ========================== Constructor =============================== /** * Constructor for objects of class clsCardDeck */ public ClsCardDeck() { // initialise instance variables nextCard = 1; // The first card to deal deck = new int[DECKSIZE + 1]; // Create the deck, It is "one too big" to account for zero-based arrays } /** * Purpose: Constructor for objects of class clsCardDeck when multiple decks are wanted * * Parameter list: * int n the number of decks the user wants to use * * Return value: * n/a */ public ClsCardDeck(int n) { // initialise instance variables this(); // Call first constructor decksToUse = n; // Save the number of decks to use bigDeck = new int[(DECKSIZE + 1) * n]; // Create large deck } // ========================= Field methods (getters and setters) ========= /** * Return the number of passes it took to shuffle the deck. * NOTE: This is also the return value from the shuffleDeck() * call. * * @param N/A * @return number of passes to shuffle the deck */ public int getPassCount() { return passCount; } // ======================= General methods =============================== /** * Shuffle the deck * * @param N/A * @return number of passes to shuffle the deck */ public int shuffleDeck() { int index = 1; int val; Random rnd = new Random(); passCount = 0; // Count how many times through the while loop java.util.Arrays.fill(deck, 0); // Clear out any old values while (index != DECKSIZE + 1) // Must add one to offset 0-based arrays { val = 1 + rnd.nextInt(52); // Generates values between 1 and 52 if (deck[val] == 0) { // Is this card place in the deck is "unused"? deck[val] = index; // Yep, so assign it a card place index++; // Get ready for next card } passCount++; } nextCard = 1; // Prepare to deal the first card return passCount; } /** * Show a given card in the deck. * * @param * int the index of the position where the card is found * @return * string the pip for the card */ public String ShowCard(int index) { int card; int suit; String pip=""; try { card = deck[index] % 13; suit = deck[index] / 13; switch (card) { case 0: // King pip = "K"; suit--; // Because Kings are 1 too high for suit break; case 1: // Ace pip = "A"; break; case 2: // The non-face cards case 3: case 4: case 5: case 6: case 7: case 8: case 9: pip = Integer.toString(card); break; case 10: // Ten pip = "T"; break; case 11: // Jack pip = "J"; break; case 12: // Queen pip = "Q"; break; } pip += suits[suit]; return pip; } catch (Exception ex) { System.out.println(ex.getMessage()); return ""; } } /** * Show a given card in the deck. * * @param * int the index of the position where the card is found * @return * string the pip for the card */ public String ShowLongCard(int index) { String temp; String val; char ch; int offset; if (decksToUse > 1) temp = ShowCard(bigDeck[index]); else temp = ShowCard(deck[index]); val = temp.substring(0, 1); ch = (char) temp.charAt(0); switch (ch) { case 'T': val = "Ten of "; break; case 'A': val = "Ace of "; break; case 'J': val = "Jack of "; break; case 'Q': val = "Queen of "; break; case 'K': val = "King of "; break; default: offset = Integer.parseInt(val); val = translate[offset] + " of "; break; } ch = (char) temp.charAt(1); switch (ch) { case 'S': val += "Spades"; break; case 'H': val += "Hearts"; break; case 'D': val += "Diamonds"; break; case 'C': val += "Clubs"; break; } return val; } /** * Deal a card from the deck. Note: If you want a string representation of * the card just dealt, pass ShowCard() the index that is returned from this * call. * * @param * void * @return * int the pip for the card */ public int dealOneCard() { if (decksToUse > 1) { if (nextCard <= (DECKSIZE * decksToUse) + 1) return bigDeck[nextCard++]; else return 0; } else { if (nextCard <= DECKSIZE + 1) return deck[nextCard++]; else return 0; } } /** * Purpose: This method shuffles multiple decks of cards * * @param int n the number of decks to shuffle * @return number of passes to shuffle the deck */ public int shuffleDeck(int n) { int i; int j; int index = 1; // Where we are in the big deck array for (i = 0; i < n; i++) { passCount += shuffleDeck(); for (j = 1; j < DECKSIZE + 1; j++) { bigDeck[index++] = deck[j]; } } return passCount; } private String CodePip(int val) { int divisor; int remainder; String suits[] = {"S","C","D","H"}; String cards[] = {"K", "A","2","3","4","5","6","7","8","9","T","J","Q"}; divisor = val / 13; remainder = val % 13; return (cards[remainder] + suits[divisor]); } }