扑克类的实现

package com.loco;
/**
 * 扑克类(一副扑克)
 * @author Administrator
 *
 */
public class Poker {
    private static String[] suites = {"黑桃","红桃","草花","方块"};
    private static int[] faces = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    private Card[] cards;
    /**
     * 构造器
     */
    public Poker(){
        cards = new Card[52];
        for(int i=0;i<suites.length;i++){
            for(int j=0;j<faces.length;j++){
                cards[i*13 + j] = new Card(suites[i],faces[j]);
            }
        }       
    }
    /**
     * 洗牌
     * @author Administrator
     *
     */
    public void shuffle(){
        for(int i=0,len=cards.length;i<len;i++){
            int index = (int) (Math.random() * len);
            Card temp = cards[index];
            cards[index] = cards[i];
            cards[i] = temp;
        }
    }
    /**
     * 发牌
     * @author Administrator
     *
     */
    public Card deal(int index){
        return cards[index];
    }
    /**
     * 卡片类(一张扑克)
     * @author Administrator
     *
     */
    public class Card{
        private String suite;
        private int face;
        public Card(String suite, int face) {
            this.suite = suite;
            this.face = face;
        }
        public String toString(){
            String faceStr = "";
            switch(face){
                case 10:faceStr = "A";break;
                case 11:faceStr = "J";break;
                case 12:faceStr = "Q";break;
                case 13:faceStr = "K";break;
                default:faceStr = String.valueOf(face);
            }
            return suite + faceStr;
        }
    }
}

package com.loco;

public class PokerTest {

    public static void main(String[] args) {
        Poker poker = new Poker();
        poker.shuffle();
//        Poker.Card c1 = poker.deal(0);
        for(int i=1;i<=52;i++){
            if(i%10==0){
                System.out.println(poker.deal(i-1));
            }else{
                System.out.print(poker.deal(i-1) + " ");
            }
        }
    }
}

 

引用地址:http://blog.csdn.net/jackfrued/article/details/17339393

posted @ 2016-01-22 13:46  雁过了无痕  阅读(205)  评论(0)    收藏  举报