使用Arraylist实现斗地主发牌,洗牌,牌的排序

public class Card {
    private String color;
    private String size;
    private int index;

    public Card() {
    }

    public Card(String color, String size, int index) {
        this.color = color;
        this.size = size;
        this.index = index;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return  color + size ;
    }
}

public class GameDemo {
    private static final List<Card>ALL_CARDS=new ArrayList<>();


    static {
        String[]sizes={"3", "4", "5", "6", "7", "8", "9","10", "J", "Q", "K", "A", "2"};
        String[]colors={"♠", "♥", "♣", "♦"};
        //组合点数和花色做牌
        int index=0;
        for (String size : sizes) {
            for (String color : colors) {
                //创建一张牌对象
                Card c = new Card(color, size,index++);
                ALL_CARDS.add(c);
            }
        }
        //单独存入大小王各一张
        ALL_CARDS.add(new Card("" , "",index++));
        ALL_CARDS.add(new Card("", "",index++));
        System.out.println("新牌:"+ALL_CARDS);

    }

    public static void main(String[] args) {
        /**
         * 洗牌
         */
        Collections.shuffle(ALL_CARDS);
        System.out.println("洗牌后:"+ALL_CARDS);
        /**
         * 定义3个玩家
         */
        List<Card> dongFangBuBai = new ArrayList<>();
         List<Card> lingHuChong = new ArrayList<>();
         List<Card> renYingYing = new ArrayList<>();
        /**
         * 发牌:发出51张牌,剩余3张作为底牌
         */
        for (int i = 0; i < ALL_CARDS.size() - 3; i++) {
            Card c = ALL_CARDS.get(i);
            if (i%3==0){
                //请不败接牌
                 dongFangBuBai.add(c);

            }else if (i%3==1){
                lingHuChong.add(c);

            }else if (i%3==2){
                renYingYing.add(c);

            }
        }
        sortPork(lingHuChong);
        sortPork(renYingYing);
        sortPork(dongFangBuBai);


        //底牌:截取最后三张底牌
        List<Card> lastThreeCards = ALL_CARDS.subList(ALL_CARDS.size() - 3, ALL_CARDS.size());
        //看牌
        System.out.println("啊败:"+dongFangBuBai);
        System.out.println("啊冲:"+lingHuChong);
        System.out.println("啊盈:"+renYingYing);
        System.out.println("三张底牌:"+lastThreeCards);
    }
    public static void sortPork(List<Card>pork){
        pork.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getIndex()-o2.getIndex();
            }
        });
    }





    }

你可能感兴趣的:(使用Arraylist实现斗地主发牌,洗牌,牌的排序)