2011年4月19日

<Java> JVA409-撲克牌發牌程式




本題重點:
1. 訊息對話框(JOptionPane)

//import java.awt.* 用於使用事件處理
import java.awt.*;
import java.awt.event.*;

//swing成立視窗介面
import javax.swing.*;

public class JVA409 extends JFrame {
   private Card deck[]; //一個Card物件名為deck的Array
   private int currentCard;//currentCard變數代表發了幾張牌
   private JButton dealButton, shuffleButton;//兩個JButton
   private JTextField[] displayField;
           //因為一次要發六張牌所以要六個JTextField
   private JLabel statusLabel;//JLabel用來擺放currentCard的值


   //以下為DeckOfCard的建構子

   public JVA409()
   {
      super"撲克牌發牌程式" );//繼承自父類別JFrame

      //請以下程式補足程式碼

      String suits[] ={"黑桃","紅心","方塊","梅花"};
      String faces[] ={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
                        ;
    
      deck = new Card[52]     ;  //跳去看Card這個class
      currentCard = 0    ;

      // populate deck with Card objects
      for (int i = 0; i< suits.length ; i++ )
      for(int j = 0 ; j< faces.length ; j++)
         deck[i*13+j] = new Card(faces[j],suits[i] );

      // set up GUI and event handling
      Container container = getContentPane();
      container.setLayout(new FlowLayout(FlowLayout.CENTER));

      dealButton = new JButton( "發牌" );
      dealButton.addActionListener(

         // anonymous inner class
         new ActionListener() {

            // 發六張牌
            public void actionPerformed( ActionEvent actionEvent )
            {
               //分別產生六個Card物件
         Card dealt[] = new Card[6];
         for(int i = 0 ; i < dealt.length ; i++){
               dealt[i] = dealCard();
               if(dealt[i]!=null)
                  currentCard ++;
            }
         for(int i = 0 ; i < dealt.length ; i++){
               if(dealt[i]!=null)
                  displayField[i].setText(dealt[i].toString());
               else
                  displayField[i].setText("");
            }
          

               //因為一次發六張牌 最後一次發currentCard==47
               //當currentCard為47時 沒有牌可以發了-->跳出errorMessage
            statusLabel.setText("已發張數: "+currentCard+"張" );
            if(currentCard>51){
               statusLabel.setText("請重新洗牌再進行發牌!" );
            }
            else if (currentCard>47){
                 JOptionPane.showMessageDialog(null,"沒牌可發了!");
            }
       
          }

         }  // end anonymous inner class

      ); // end call to addActionListener

      container.add( dealButton );//將發牌的JButton加入container

      shuffleButton = new JButton( "重新洗牌" );
      shuffleButton.addActionListener(

         // anonymous inner class
         new ActionListener() {

            // shuffle deck
            public void actionPerformed( ActionEvent actionEvent )
            {
            JOptionPane.showMessageDialog(null,"洗牌中...");
            statusLabel.setText("");
            shuffle();
            for(int i = 0 ; i < displayField.length ; i++){
               displayField[i].setText("洗好牌了!!");

            }
            }

         }  // end anonymous inner class

      ); // end call to addActionListener

      container.add( shuffleButton );//把洗牌的button加入container

      //建立六個JTextFiled
     displayField = new JTextField[6];
      for(int i = 0 ; i < displayField.length ; i++){
         displayField[i] = new JTextField(20);
         displayField[i].setEditable(false);
         container.add( displayField[i] );
      }


      //設定JTextField內容不可被改變


      statusLabel = new JLabel();
      container.add( statusLabel );

      setSize(275,250);
     setVisible(true);               // show window
   }

   // 這是洗牌的method
   public void shuffle()
   {
      currentCard = 0;

      // for each card, pick another random card and swap them
      for (int first = 0 ; first<deck.length ; first++) {
         int second = (int)(Math.random()*52.0);
         Card temp = deck[first];
         deck[ first ] = deck[second];
         deck[ second ] = temp;   //swap
      }
      dealButton.setEnabled( true );
   }

   // deal one card
   public Card dealCard()
   {
      if (currentCard<52)
      return deck[currentCard];
      else {
         dealButton.setEnabled( false );
         return null;
      }
   }

   // execute application
   public static void main( String args[] )
   {
      JVA409 app = new JVA409();

      app.addWindowListener(

         // anonymous inner class
         new WindowAdapter() {

            // terminate application when user closes window
            public void windowClosing( WindowEvent windowEvent )
            {
               System.exit( 0 );
            }

         }  // end anonymous inner class

      ); // end call to addWindowListener

   }  // end method main

}  // end class DeckOfCards

// 這個class 代表一張牌
class Card {
   private String face;
   private String suit;

   // 建構子實體化一張牌
   public Card( String cardFace, String cardSuit )
   {
      face = cardFace;
      suit = cardSuit;
   }

   // return String represenation of Card
   public String toString()
   {
      return   suit + face;
   }

}

沒有留言:

張貼留言