应用——模拟斗地主发牌

2018-02-26  本文已影响0人  嗷老板
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Random;

/*
 * 完成斗地主洗牌发牌
 */
public class Test {
    public static void main(String[] args) {
        String[] pai = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        String[] hs = { "♠","♥", "♣","♦"};
        
        
        //产生扑克牌组
        ArrayList<String> poker = new ArrayList<String>();
        for (String s1 : pai) {
            for (String s2 : hs) {
                poker.add(s2 + s1);
            }
        }
        poker.add("大王");
        poker.add("小王");
        Collections.shuffle(poker);

        // 创建玩家1,玩家2,玩家3,以及底牌的集合和记录各个集合中牌数的计数器
        ArrayList<String> person1 = new ArrayList<String>();
        int count1 = 0;
        ArrayList<String> person2 = new ArrayList<String>();
        int count2 = 0;
        ArrayList<String> person3 = new ArrayList<String>();
        int count3 = 0;
        ArrayList<String> dipai = new ArrayList<String>();
        int countD = 0;

        Random r = new Random();
        //使用列表迭代器得到每一张牌
        ListIterator<String> lit = poker.listIterator();
        while (lit.hasNext()) {
            String p = lit.next();
            a: while (true) {
                //随机产生1到4的数字,按照数字对应的玩家或底牌发牌
                int i = r.nextInt(4) + 1;
                switch (i) {
                case 1:
                    //如果玩家手中牌数达到17,停止给此玩家发牌,将得到的这张牌重新发给另一个玩家
                    if (count1 == 17) {
                        break;
                    }
                    //给玩家扑克集合中添加牌
                    person1.add(p);
                    //玩家手中牌数加1
                    count1++;
                    //将此牌从迭代器中移除
                    lit.remove();
                    //跳出发牌的循环,得到下一张牌
                    break a;
                case 2:
                    if (count2 == 17) {
                        break;
                    }
                    person2.add(p);
                    count2++;
                    lit.remove();
                    break a;
                case 3:
                    if (count3 == 17) {
                        break;
                    }
                    person3.add(p);
                    count3++;
                    lit.remove();
                    break a;
                case 4:
                    if (countD == 3) {
                        break;
                    }
                    dipai.add(p);
                    countD++;
                    lit.remove();
                    break a;
                }
            }

        }
        
        //输出打印玩家的牌
        System.out.print("玩家1:");
        for (String p1 : person1) {
            System.out.print(p1 + " ");
        }
        System.out.println();
        System.out.print("玩家2:");
        for (String p2 : person2) {
            System.out.print(p2 + " ");
        }
        System.out.println();
        System.out.print("玩家3:");
        for (String p3 : person3) {
            System.out.print(p3 + " ");
        }
        System.out.println();
        System.out.print("底牌:");
        for (String p4 : dipai) {
            System.out.print(p4 + " ");
        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读