黑马程序员技术交流社区

标题: 斗地主代码,排序带地主牌和底牌添加 [打印本页]

作者: 郝永    时间: 2017-4-14 22:25
标题: 斗地主代码,排序带地主牌和底牌添加
写了一下午,出了BUG,找了好一会,亮出来分享一下!
public class DDZ {
        static HashMap<Integer, Card> hm = new HashMap<>();
        static ArrayList<Integer> list = new ArrayList<>();
        static {
                hm.put(1, new Card("大", "☺"));// ♠♥♣♦
                hm.put(2, new Card("小", "☺"));
                list.add(1);
                list.add(2);
                String[] arr = { "♠", "♥", "♣", "♦" };
                String[] arr1 = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };
                int i = 3;
                for (String s : arr1) {
                        for (String st : arr) {
                                list.add(i);
                                hm.put(i++, new Card(st, s));
                        }
                }
        }

        public static void main(String[] args) {
                Collections.shuffle(list);
                Random r = new Random();
                int d = r.nextInt(52);// 获取list的一个角标
                System.out.println("随机的list的一个角标:" + d);
                System.out.println("扑克牌:" + hm);
                int dzj = list.get(d);
                System.out.println("地主牌的键:" + dzj);

                Card dzp = hm.get(dzj);
                System.out.println("地主牌是:" + dzp);
                ArrayList<Integer> p1 = fapai();
                System.out.println("成龙的键" + p1);
                ArrayList<Integer> p2 = fapai();
                System.out.println("甄子丹的键" + p2);
                ArrayList<Integer> p3 = fapai();
                System.out.println("李连杰的键" + p3);
                ArrayList<Integer> dp = new ArrayList<>();
                dp.addAll(list);
                ArrayList<Card> dipai = zhuanhuan(dp);
                System.out.println("底牌是:" + dipai);

                if (p1.contains(dzj)) {
                        p1.addAll(dp);
                        Collections.sort(p1);
                        System.out.println("成龙是地主");
                }
                ArrayList<Card> person1 = zhuanhuan(p1);
                if (p2.contains(dzj)) {
                        p2.addAll(dp);
                        Collections.sort(p2);
                        System.out.println("甄子丹是地主");
                }
                ArrayList<Card> person2 = zhuanhuan(p2);
                if (p3.contains(dzj)) {
                        p3.addAll(dp);
                        Collections.sort(p3);
                        System.out.println("李连杰是地主");
                }
                ArrayList<Card> person3 = zhuanhuan(p3);
                System.out.println("成龙:" + person1);
                System.out.println("甄子丹:" + person2);
                System.out.println("李连杰:" + person3);

        }

        private static ArrayList<Integer> fapai() {

                ArrayList<Integer> p1 = new ArrayList<>();
                for (int i = 0; i < 17; i++) {
                        int a = list.remove(0);
                        p1.add(a);
                }

                Collections.sort(p1);

                return p1;
        }

        private static ArrayList<Card> zhuanhuan(ArrayList<Integer> list) {
                ArrayList<Card> p = new ArrayList<>();
                for (Integer integer : list) {

                        Card c = hm.get(integer);
                        p.add(c);
                }
                return p;
        }
}

class Card {
        private String color;
        private String numb;

        /**
         * @param color
         * @param numb
         */
        public Card(String color, String numb) {
                super();
                this.color = color;
                this.numb = numb;
        }

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

}
作者: 烟雨清寒时    时间: 2017-4-15 07:50
这个在基础班属于很牛逼很牛逼的代码了当时领悟了好久





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2