如何生成一副Poker

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何生成一副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);
        }   
    }
}

以上是关于如何生成一副Poker的主要内容,如果未能解决你的问题,请参考以下文章

php poker 生成的随机结果与预期不符

零知识扑克牌游戏开发教程ZK Poker

德州扑克AI实现 TexasHoldem Poker

过年了,用 PyQt5 生成一副春联吧...

在opencv中,如何将二维数组转化为一副图像进行显示?

matlab如何求一副图像的信噪比SNR与均方根误差RMSE