zoukankan      html  css  js  c++  java
  • Java 用集合实现简单的斗地主发牌

    创建数组、集合,存放数据

    public class FightAgainstLandlords {
        /**
         * poker集合,存储54张牌
         */
        private ArrayList<String> poker;
    
        /**
         * colors数组存储牌的花色
         */
        private String[] colors;
    
        /**
         * numbers数组存储牌的值
         */
        private String[] numbers;
    }

    构造方法FightAgainstLandlords

    public class FightAgainstLandlords {
        public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) {
            this.poker = poker;
            this.colors = colors;
            this.numbers = numbers;
        }
    }

    定义打乱牌牌序方法

    public class FightAgainstLandlords {
        /**
         * 存储54张牌
         */
        public ArrayList<String> fiftyFive() {
            for (String color: colors) {
                for (String number: numbers) {
                    poker.add(color + number);
                }
            }
            poker.add("大王");
            poker.add("小王");
            // 洗牌,调用Collections类的静态方法shuffle(),用默认随机源对指定列表进行置换
            Collections.shuffle(poker);
            return poker;
        }
    }

    发牌

    public class FightAgainstLandlords {
        /**
         * 发牌
         * 获取玩家牌或者底牌
         * j = 1, 2, 3 代表玩家牌
         * j = 其他数字 代表底牌
         */
        public ArrayList<String> licensing(int j, ArrayList<String> pokers) {
            // 三个玩家
            ArrayList<String> people1 = new ArrayList<>();
            ArrayList<String> people2 = new ArrayList<>();
            ArrayList<String> people3 = new ArrayList<>();
            // 底牌
            ArrayList<String> basePoker = new ArrayList<>();
    
            for (int i = 0; i < pokers.size(); i++) {
                String p = pokers.get(i);
                if ( i < 51) {
                    if (i % 3 == 0) {
                        people1.add(p);
                    } else if (i % 3 == 1) {
                        people2.add(p);
                    } else {
                        people3.add(p);
                    }
                } else {
                    basePoker.add(p);
                }
            }
    
            // 返回玩家的牌、底牌
            if (j == 1) {
                return people1;
            } else if (j == 2) {
                return people2;
            } else if (j == 3) {
                return people3;
            } else {
                return basePoker;
            }
        }
    }

    测试FightAgainstLandlords类

    import java.util.ArrayList;
    
    public class DemoFightAgainstLandlords {
        public static void main(String[] args) {
    
            ArrayList<String> poker = new ArrayList<>();
            String[] colors = {"红桃", "黑桃", "梅花", "方块"};
            String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    
            // new一个斗地主
            FightAgainstLandlords fightAgainstLandlords = new FightAgainstLandlords(poker, colors, numbers);
            // 54张牌
            ArrayList<String> pokers = fightAgainstLandlords.fiftyFive();
    
            // 获取每个人的牌,和底牌
            ArrayList<String> people1 = fightAgainstLandlords.licensing(1, pokers);
            ArrayList<String> people2 = fightAgainstLandlords.licensing(2, pokers);
            ArrayList<String> people3 = fightAgainstLandlords.licensing(3, pokers);
            ArrayList<String> basePoker = fightAgainstLandlords.licensing(4, pokers);
    
            // 看一下它们每个人的牌,和底牌
            System.out.println("people1:" + people1);
            System.out.println("people2:" + people2);
            System.out.println("people3:" + people3);
            System.out.println("basePoker:" + basePoker);
        }
    }
    输出结果(每个人的牌,和底牌都是随机的):
    people1:[红桃3, 梅花J, 梅花K, 方块J, 方块K, 梅花10, 红桃6, 梅花9, 黑桃Q, 红桃Q, 梅花4, 黑桃A, 方块2, 红桃8, 方块4, 黑桃8, 红桃K]
    people2:[梅花A, 方块3, 小王, 黑桃J, 红桃7, 方块5, 方块9, 黑桃10, 方块8, 梅花Q, 方块6, 梅花6, 红桃10, 方块Q, 黑桃5, 黑桃2, 红桃A]
    people3:[梅花5, 梅花8, 黑桃7, 黑桃4, 红桃9, 黑桃9, 黑桃K, 方块7, 黑桃6, 梅花3, 方块10, 红桃4, 黑桃3, 红桃5, 大王, 红桃J, 方块A]
    basePoker:[红桃2, 梅花2, 梅花7]

    FightAgainstLandlords类的所有代码

    import java.util.ArrayList;
    import java.util.Collections;
    
    public class FightAgainstLandlords {
        /**
         * poker集合,存储54张牌
         * 不是斗地主也可以存储52张牌(不存储大王、小王牌)
         */
        private ArrayList<String> poker;
    
        /**
         * colors数组存储牌的花色
         */
        private String[] colors;
    
        /**
         * numbers数组存储牌的值
         */
        private String[] numbers;
    
        public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) {
            this.poker = poker;
            this.colors = colors;
            this.numbers = numbers;
        }
    
        /**
         * 存储54张牌
         */
        public ArrayList<String> fiftyFive() {
            for (String color: colors) {
                for (String number: numbers) {
                    poker.add(color + number);
                }
            }
            poker.add("大王");
            poker.add("小王");
            // 洗牌,调用Collections类的静态方法shuffle(),用默认随机源对指定列表进行置换
            Collections.shuffle(poker);
            return poker;
        }
    
        /**
         * 发牌
         * 获取玩家牌或者底牌
         * j = 1, 2, 3 代表玩家牌
         * j = 其他数字 代表底牌
         */
        public ArrayList<String> licensing(int j, ArrayList<String> pokers) {
            // 三个玩家
            ArrayList<String> people1 = new ArrayList<>();
            ArrayList<String> people2 = new ArrayList<>();
            ArrayList<String> people3 = new ArrayList<>();
            // 底牌
            ArrayList<String> basePoker = new ArrayList<>();
    
            for (int i = 0; i < pokers.size(); i++) {
                String p = pokers.get(i);
                if ( i < 51) {
                    if (i % 3 == 0) {
                        people1.add(p);
                    } else if (i % 3 == 1) {
                        people2.add(p);
                    } else {
                        people3.add(p);
                    }
                } else {
                    basePoker.add(p);
                }
            }
    
            // 返回玩家的牌、底牌
            if (j == 1) {
                return people1;
            } else if (j == 2) {
                return people2;
            } else if (j == 3) {
                return people3;
            } else {
                return basePoker;
            }
        }
    }
  • 相关阅读:
    (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
    阅读&lt;反欺骗的艺术&gt;思考
    顺序查找(改进)
    win7电脑那些事
    激活office 2010
    MyEclipse10安装SVN插件
    合并排序法
    希尔排序法
    直接插入排序法
    快速排序法——较优方法
  • 原文地址:https://www.cnblogs.com/liyihua/p/12187533.html
Copyright © 2011-2022 走看看