zoukankan      html  css  js  c++  java
  • 如何生成一副Poker

    import java.util.LinkedList;
    import java.util.Random;
    //扑克类
    class Poker{
        String color;//花色
        String num;//点数
        public Poker (String color,String num) {
            this.color=color;
            this.num =num;
        }
        public String toString() {
            return "{"+color+num+"}";
        }
    }

    public class Demo{
        public static void main(String[] args) {
            LinkedList<Poker> pokers=createPoker();
            showPoker(pokers);
            System.out.println("洗牌,洗牌。。。");
            sufflePoker(pokers);
            showPoker(pokers);
        }
        //生成一副扑克
        public static LinkedList<Poker> createPoker() {
            LinkedList<Poker> list=new LinkedList<Poker>();
            String []color={"黑桃","红桃","梅花","方块"};
            String []num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
            for (int i = 0; i < color.length; i++) {
                for (int j = 0; j < num.length; j++) {
                    list.add(new Poker(color[i], num[j]));
                }
            }
            return list;
        }
        //更好展示扑克
        public static void showPoker(LinkedList<Poker> pokers){
            for (int i = 0; i < pokers.size(); i++) {
                System.out.print(pokers.get(i));
                if (i%13==12) {
                    System.out.println();//换行
                }   
            }
        }
        //洗扑克功能
        public static void sufflePoker(LinkedList<Poker> pokers) {
            Random random=new Random();//创建随机数对象
            for (int i = 0; i < 100; i++) {//实现多次洗牌
                //随机产生两个索引值
                int index1=random.nextInt(pokers.size());
                int index2=random.nextInt(pokers.size());
                //根据索引值取出两张牌,然后交换两张牌的顺序
                Poker poker1=pokers.get(index1);
                Poker poker2=pokers.get(index2);
                pokers.set(index1, poker2);
                pokers.set(index2, poker1);
            }   
        }
    }

  • 相关阅读:
    BZOJ 1305 dance跳舞(最大流+二分答案)
    计蒜客 贝壳找房计数比赛(可重全排列+逆元)
    计蒜客 贝壳找房函数最值(好题,巧妙排序)
    Codeforces 463D Gargari and Permutations(求k个序列的LCS)
    Codeforces 552C Vanya and Scales(进制转换+思维)
    Codeforces 682C Alyona and the Tree (树上DFS+DP)
    Codeforces 332B Maximum Absurdity(DP+前缀和处理)
    Codeforces 981D Bookshelves(按位贪心+二维DP)
    Codeforces 225C Barcode(矩阵上DP)
    Codeforces 988F Rain and Umbrellas(DP)
  • 原文地址:https://www.cnblogs.com/lifi/p/5225862.html
Copyright © 2011-2022 走看看