Java_17_斗地主游戏思想

该代码示例展示了如何在Java中创建一个斗地主游戏的牌组,包括定义牌的类、初始化牌组、洗牌、发牌给三个玩家以及对牌进行排序的过程。程序使用ArrayList存储牌,并通过Collections.shuffle进行洗牌操作,按照索引顺序对玩家的手牌进行排序。
摘要由CSDN通过智能技术生成

斗地主游戏思想

创建新牌

要创建一个牌组数组

public class Cards {
    private String number;
    private String color;
    private int index;
    public Cards() {
    }
    public Cards(String number, String color, int index) {
        this.number = number;
        this.color = color;
        this.index = index;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    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;
    }
    public String toString() {
        return  number + color;
    }
}

牌组加入新牌

 public static List<Cards> All_Cards = new ArrayList<>();
    static{
        // 1.定义一个数组存储牌的点数,类型确定,个数确定请用数组存储!
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        // 2.定义一个数组存储牌的花色,类型确定,个数确定请用数组存储!
        int index = 0;
        String[] colors = { "♠", "♥", "♣", "♦" };
        for (String number : numbers) {
            for (String color : colors) {
                Cards card = new Cards(number,color,index ++);
                All_Cards.add(card);
            }
        }
        Collections.addAll(All_Cards,new Cards("","🃏",index ++),new Cards("","👲",index ++));
    }

洗牌

/*洗牌*/
        System.out.println("洗牌前" + All_Cards);
        Collections.shuffle(All_Cards);
        System.out.println("洗牌后" + All_Cards);

发牌

 /* 定义三个玩家*/
        List<Cards> zhangwuji = new ArrayList<>();
        List<Cards> zhaomin = new ArrayList<>();
        List<Cards> zhouzhiruo = new ArrayList<>();
        /*发牌*/
        for(int i = 0;i < All_Cards.size() - 3;i ++){
            Cards ca = All_Cards.get(i);
            switch (i % 3) {
                case 0 -> zhangwuji.add(ca);
                case 1 -> zhaomin.add(ca);
                case 2 -> zhouzhiruo.add(ca);
            }
        }

发牌排序

private static void sortCards(List<Cards> cards){
        cards.sort(Comparator.comparingInt(Cards::getIndex));
    }
/*对牌排序*/
        sortCards(zhangwuji);
        sortCards(zhouzhiruo);
        sortCards(zhaomin);

看牌

/*看牌*/
        System.out.println("张无忌" + zhangwuji);
        System.out.println("赵敏" + zhaomin);
        System.out.println("周芷若" + zhouzhiruo);
        List<Cards> LastCards = All_Cards.subList(All_Cards.size() - 3,All_Cards.size());
        System.out.println("底牌" + LastCards);

总体代码

牌组类

public class Cards {
    private String number;
    private String color;
    private int index;
    public Cards() {
    }
    public Cards(String number, String color, int index) {
        this.number = number;
        this.color = color;
        this.index = index;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    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;
    }
    public String toString() {
        return  number + color;
    }
}

牌的操作

public class Main_Demo {
    public static List<Cards> All_Cards = new ArrayList<>();
    static{
        // 1.定义一个数组存储牌的点数,类型确定,个数确定请用数组存储!
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        // 2.定义一个数组存储牌的花色,类型确定,个数确定请用数组存储!
        int index = 0;
        String[] colors = { "♠", "♥", "♣", "♦" };
        for (String number : numbers) {
            for (String color : colors) {
                Cards card = new Cards(number,color,index ++);
                All_Cards.add(card);
            }
        }
        Collections.addAll(All_Cards,new Cards("","🃏",index ++),new Cards("","👲",index ++));
    }
    //对分的牌排序   降序  "3","4","5","6","7","8","9","10","J","Q","K","A","2","🃏","👲"
    private static void sortCards(List<Cards> cards){
        cards.sort(Comparator.comparingInt(Cards::getIndex));
    }
    public static void main(String[] args){
        /*洗牌*/
        System.out.println("洗牌前" + All_Cards);
        Collections.shuffle(All_Cards);
        System.out.println("洗牌后" + All_Cards);
        /* 定义三个玩家*/
        List<Cards> zhangwuji = new ArrayList<>();
        List<Cards> zhaomin = new ArrayList<>();
        List<Cards> zhouzhiruo = new ArrayList<>();
        /*发牌*/
        for(int i = 0;i < All_Cards.size() - 3;i ++){
            Cards ca = All_Cards.get(i);
            switch (i % 3) {
                case 0 -> zhangwuji.add(ca);
                case 1 -> zhaomin.add(ca);
                case 2 -> zhouzhiruo.add(ca);
            }
        }
        /*对牌排序*/
        sortCards(zhangwuji);
        sortCards(zhouzhiruo);
        sortCards(zhaomin);
        /*看牌*/
        System.out.println("张无忌" + zhangwuji);
        System.out.println("赵敏" + zhaomin);
        System.out.println("周芷若" + zhouzhiruo);
        List<Cards> LastCards = All_Cards.subList(All_Cards.size() - 3,All_Cards.size());
        System.out.println("底牌" + LastCards);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐鑫本鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值