Java案例--斗地主游戏(完成洗牌、发牌、牌排序的逻辑)List & Collections

需求:

        在启动游戏房间的时候,应该提前准备好54张牌,完成洗牌、发牌、牌排序、逻辑。

分析:

        ①当系统启动的同时需要准备好数据的时候,就可以用静态代码块了。

        ②洗牌就是打乱牌的顺序。 

        ③定义三个玩家、依次发出51张牌 

        ④给玩家的牌进行排序(拓展)

        ⑤输出每个玩家的牌数据。

业务: 总共有54张牌。
点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
花色: "♠", "♥", "♣", "♦"
大小王: "👲" , "🃏"
点数分别要组合4种花色,大小王各一张。
斗地主:发出51张牌,剩下3张作为底牌。
//牌类
public class Card {
    private String size;
    private String color;
    private int index; // 牌的真正大小

    public Card(){
    }

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

    public String getSize() {
        return size;
    }

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

    public String getColor() {
        return color;
    }

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

    public int getIndex() {
        return index;
    }

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

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

//测试类
public class GameDemo {

    //1、定义一个静态的集合存储54张牌对象
    public static List<Card> allCards = new ArrayList<>();

    //2、做牌:定义静态代码块初始化牌数据
    static {
        // 3、定义点数:个数确定,类型确定,使用数组
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 4、定义花色:个数确定,类型确定,使用数组
        String[] colors = {"♠", "♥", "♣", "♦"};
        // 5、组合点数和花色
        int index = 0; // 记录牌的大小
        for (String size : sizes) {
            index++;
            for (String color : colors) {
                // 6、封装成一个牌对象。
                Card c = new Card(size, color, index);
                // 7、存入到集合容器中去
                allCards.add(c);
            }
        }
        // 8 大小王存入到集合对象中去 "👲" , "🃏"
        Card c1 = new Card("", "🃏", ++index);
        Card c2 = new Card("", "👲", ++index);
        Collections.addAll(allCards, c1, c2);
        System.out.println("新牌:" + allCards);
    }

    public static void main(String[] args) {
        // 9、洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后:" + allCards);

        // 10、发牌(定义三个玩家,每个玩家的牌也是一个集合容器)
        List<Card> linhuchong = new ArrayList<>();
        List<Card> jiumozhi = new ArrayList<>();
        List<Card> renyingying = new ArrayList<>();

        // 11、开始发牌(从牌集合中发出51张牌给三个玩家,剩余3张作为底牌)
        for (int i = 0; i < allCards.size() - 3; i++) {
            // 先拿到当前牌对象
            Card c = allCards.get(i);
            if (i % 3 == 0) {
                // 请阿冲接牌
                linhuchong.add(c);
            } else if (i % 3 == 1) {
                // 请阿鸠
                jiumozhi.add(c);
            } else if (i % 3 == 2) {
                // 请盈盈接牌
                renyingying.add(c);
            }
        }

        // 12、拿到最后三张底牌(把最后三张牌截取成一个子集合)
        List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size());

        // 13、给玩家的牌排序(从大到小 可以自己先试试怎么实现)
        sortCards(linhuchong);
        sortCards(jiumozhi);
        sortCards(renyingying);

        // 14、输出玩家的牌:
        System.out.println("啊冲:" + linhuchong);
        System.out.println("啊鸠:" + jiumozhi);
        System.out.println("盈盈:" + renyingying);
        System.out.println("三张底牌:" + lastThreeCards);
    }

    //给牌排序
    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getIndex() - o1.getIndex();
            }
        });
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值