模拟斗地主的洗牌和发牌功能

需求:牌共有四种花色,每个花色从A、2、3....到J、O、K。以及大小王。

构建一个集合对象用来存放起始牌(顺序打乱),拿出三张底牌,剩余的发给三个集合对象(玩家)。

首先将牌的模板抽象出来,写作Card类。

public class Card {
    private String color;//四种花色♥♦♣♠
    private String name;//牌面大小 3 4 5 6 7 8 9 10 J Q K A 2 小王 大王
    private int weight;//权重,用来排序 14 15 16 17
    public Card(){}
    public Card(String color,String name,int weight){
        super();
        this.color = color;
        this.name = name;
        this.weight =weight;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

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

------新建一个Test类用来实现。

将花色和牌值存入两个数组集合中,两两匹配,并且维护权重与牌面数字的关系

 String[] colors = {"♥", "♦", "♣", "♠"};
 String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //维护数字和权重之间的关系
        HashMap weights = new HashMap<>();
        for (String num:numbers) {
            int weight = -1;
            if ("J".equals(num)){
                weight =11;
            } else if ("Q".equals(num)) {
                weight =12;
            } else if ("K".equals(num)) {
                weight = 13;
            }else if ("A".equals(num)){
                weight = 14;
            } else if ("2".equals(num)) {
                weight = 15;
            }else {
                weight = Integer.parseInt(num);
            }
            weights.put(num,weight);
        }

创建一个容器集合,用来存放洗牌之前生成的54张牌

           ArrayList container = new ArrayList(54);
            container.add(new Card("", "小王", 16));
            container.add(new Card("", "大王", 17));

            //生成牌
            for (int i = 0; i < colors.length; i++) {//取花色
                for (int j = 0; j < numbers.length; j++) {//取数字
                    container.add(new Card(colors[i], numbers[j], (int) weights.get(numbers[j])));
                }
            }

进行洗牌操作(使用类Collections中的shuffle方法对容器集合中的牌随机置换顺序)

发牌操作(创建三个玩家集合和底牌集合接受)并将玩家的手牌排序。玩家1和3的手牌按照从小到大的顺序,玩家2的手牌按照从大到小的顺序。

            Collections.shuffle(container);
            System.out.println("洗牌后:"+container);

            //发牌
            List palyer1 = new ArrayList(17);
            List palyer2 = new ArrayList(17);
            List palyer3 = new ArrayList(17);
            List last = new ArrayList(3);  //List last =container.subList(51,54);
            for (int i = 0; i < container.size(); i++) {
                if (i < 51) {
                    if (i % 3 == 0) {
                        palyer1.add(container.get(i));
                    } else if (i % 3 == 1) {
                        palyer2.add(container.get(i));
                    } else if (i % 3 == 2) {
                        palyer3.add(container.get(i));
                    }
                } else {
                    last.add(container.get(i));
                }
            }
            Comparator comparator = new Comparator() { //重写排序方法
                @Override
                public int compare(Object o1, Object o2) {
                    Card c1 = (Card) o1;
                    Card c2 = (Card) o2;
                    return c1.getWeight() - c2.getWeight();
                }
            };
            Collections.sort(palyer1, comparator);
            Collections.sort(palyer2, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    Card c1 = (Card) o1;
                    Card c2 = (Card) o2;
                    return c2.getWeight() - c1.getWeight();
                }
            });
            Collections.sort(palyer3, comparator);

            System.out.println("玩家1手牌:" + palyer1);
            System.out.println("玩家2手牌:" + palyer2);
            System.out.println("玩家3手牌:" + palyer3);
            System.out.println("底牌" + last);
        }

运行结果

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值