// Fig. 10.21: DeckOfCards.java
// Card shuffling and dealing program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeckOfCards extends JFrame {
   private Card deck[];
   private int currentCard;
   private JButton dealButton, shuffleButton;
   private JTextField displayCard;
   private JLabel status;

   public DeckOfCards()
   {
      super( "Card Dealing Program" );

      String faces[] = { "Ace", "Deuce", "Three", "Four", 
                         "Five", "Six", "Seven", "Eight", 
                         "Nine", "Ten", "Jack", "Queen",
                         "King" };
      String suits[] = { "Hearts", "Diamonds",
                         "Clubs", "Spades" };

      deck = new Card[ 52 ];
      currentCard = -1;
      
      for ( int i = 0; i < deck.length; i++ ) 
         deck[ i ] = new Card( faces[ i % 13 ],
                               suits[ i / 13 ] );

      Container c = getContentPane();
      c.setLayout( new FlowLayout() );     

      dealButton = new JButton( "Deal card" );
      dealButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               Card dealt = dealCard();

               if ( dealt != null ) {
                  displayCard.setText( dealt.toString() );
                  status.setText( "Card #: " + currentCard );
               }
               else {
                  displayCard.setText(
                     "NO MORE CARDS TO DEAL" );
                  status.setText(
                     "Shuffle cards to continue" );
               }
            }
         }
      );
      c.add( dealButton );

      shuffleButton = new JButton( "Shuffle cards" );
      shuffleButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               displayCard.setText( "SHUFFLING ..." );
               shuffle();
               displayCard.setText( "DECK IS SHUFFLED" );
            }
         }
      );
      c.add( shuffleButton );

      displayCard = new JTextField( 20 );
      displayCard.setEditable( false );
      c.add( displayCard );

      status = new JLabel();
      c.add( status );

      setSize( 275, 120 );  // set the window size
      show();               // show the window
   }

   public void shuffle()
   {
      currentCard = -1;

      for ( int i = 0; i < deck.length; i++ ) {
         int j =  ( int ) ( Math.random() * 52 );
         Card temp = deck[ i ];   // swap
         deck[ i ] = deck[ j ];   // the
         deck[ j ] = temp;        // cards
      }

      dealButton.setEnabled( true );
   }

   public Card dealCard()
   {
      if ( ++currentCard < deck.length )
         return deck[ currentCard ];
      else {        
         dealButton.setEnabled( false );
         return null;
      }
   }

   public static void main( String args[] )
   {
      DeckOfCards app = new DeckOfCards();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}

class Card {
   private String face;
   private String suit;

   public Card( String f, String s )
   {
      face = f;
      suit = s;
   }

   public String toString() { return face + " of " + suit; }
}

/**************************************************************************
 * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/
