• 【JAVA - ArrayList】炸金花的模拟实现流程(买牌,洗牌,发牌)


    是微风,是晚霞,是心跳,是无可替代

    在这里插入图片描述

    大家好,这里是新一,请多关照🙈🙉🙊。在本篇博客中,新一将会为大家介绍JAVA的ArrayList模拟炸金花的买牌,洗牌,发牌流程,干货满满哟。(以下结果均在IDEA中编译)希望在方便自己复习的同时也能帮助到大家。😜😜😜🥇🥈🥉

    废话不多说,直接进入我们的文章。



    🌕 炸金花

    此处我们只对泛型做一个了解,在后续更新中我们会对泛型进行深入学习

    1.1🍂 扑克牌对象

    既然我们要玩经典的游戏:“炸金花”,那么扑克牌这样的对象时必不可少的,那么扑克牌(除去大小王)具有哪些元素呢 —— 花色 + 数字 这便是其两大元素。

    class Card{
        private String rank;//数字
        private String suit;//花色
        
        //构造方法
        public Card(String rank, String suit) {
            this.rank = rank;
            this.suit = suit;
        }
    
        //重写toString方法
        @Override
        public String toString() {
            return "[ " + this.suit + " " + this.rank+ " ]";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    1.2🍂 炸金花流程

    首先,我们需要在我们的主类里边定义两个数组:

    //代表花色 - 遍历生成扑克牌
    private static final String[] suits = {"♥","♠","♣","♦"};
    //J Q K 为特殊数字
    private static final String[] ranks = {"J", "Q", "K"};
    
    • 1
    • 2
    • 3
    • 4

    接下来是我们的买牌,洗牌,发牌流程。

    1.2.1 🍃 买牌

    这里就要用到我们上一篇博客讲的泛型

    public static List<Card> buyCard() {
            ArrayList<Card> cards = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                cards.add(new Card("A",suits[i]));
                for (int j = 2; j <= 13; j++) {
                    cards.add(new Card(j <= 10 ? j + "" : ranks[j - 11], suits[i]));
                }
            }
            return cards;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.2.2 🍃 洗牌

    这里我们需要用到随机数生成器Random

    private static void swap(List<Card> cards,int i,int j){
            Card tmp = cards.get(i);
            cards.set(i,cards.get(j));
            cards.set(j,tmp);
        }
        public static void shuffle(List<Card> cards){
            int size = cards.size();
            for (int i = size - 1; i > 0; i--) {
                Random random = new Random();
                int rand = random.nextInt(i);
                swap(cards,i,rand);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.2.3 🍃 发牌

    这里我们设置五个人的炸金花,那么其中每一个人都要有三张牌,即每个人都是一个小型的ArrayList,故我们需要设置一个二维数组,行代表,列代表其拥有的,也就是ArrayList>

    public static void getCard(List<Card> cards){
            ArrayList<ArrayList<Card>> hand = new ArrayList<>();
    
            ArrayList<Card> hand1 = new ArrayList<>();
            ArrayList<Card> hand2 = new ArrayList<>();
            ArrayList<Card> hand3 = new ArrayList<>();
            ArrayList<Card> hand4 = new ArrayList<>();
            ArrayList<Card> hand5 = new ArrayList<>();
    
            hand.add(hand1);
            hand.add(hand2);
            hand.add(hand3);
            hand.add(hand4);
            hand.add(hand5);
    
            //每个人轮流揭牌
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 5; j++) {
                    Card card = cards.remove(0);
                    hand.get(j).add(card);
                }
            }
            System.out.println("第一个人的牌:"+hand1);
            System.out.println("第二个人的牌:"+hand2);
            System.out.println("第三个人的牌:"+hand3);
            System.out.println("第四个人的牌:"+hand4);
            System.out.println("第五个人的牌:"+hand5);
            System.out.println("剩下的牌:"+cards);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    1.3🍂 具体实现

    这里是我们主函数入口。

    public static void main(String[] args) {
    
            List<Card> cards = buyCard();//买牌
            System.out.println("买牌:"+cards);
    
            shuffle(cards);//洗牌
            System.out.println("洗牌:"+cards);
    
            System.out.println("炸金花:5个人每个人轮流接3张牌");
            getCard(cards);//发牌
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    想对大家说的话

    家人们,博主能力有限,没有把打牌的过程实现出来,后续博主一定会继续努力的,感谢大家的支持🥳🥳🥳后续新一会持续更JAVA的有关内容,学习永无止境,技术宅,拯救世界!
    在这里插入图片描述

  • 相关阅读:
    关于ITSS认证资质整改和降级
    微分中值定理证明和总结
    git常用命令
    使用 AI 学习 Python web 的 django 代码(1/30天)
    05、Python 简单计算器和进制转换
    MongoDB CRUD操作:快照查询
    【算法】十一月阳光下的阴影面积
    第一章 操作系统概述
    智慧公厕整体解决方案,厕所革命实施方案的范本
    AWS S3上传下载
  • 原文地址:https://blog.csdn.net/m0_73204758/article/details/126899038