发牌算法(java)

来源:互联网 发布:定时短信软件 编辑:程序博客网 时间:2024/04/28 19:34

思想描述:先将牌排好序,然后随机抽出一张牌和前面的牌交换,count–(count为未排序的牌个数),最后输出类数组。

class sendPoke {    private String num,type;    public sendPoke(String t,String n)    {        this.type = t;        this.num = n;    }    public String toString()    {        return type+" "+num+"  ";    }}class send{    sendPoke poke[];    public void chushihua()    {        poke = new sendPoke[52];        String num[]={"A","2","3","4","5","6","7","8","9","10""J","Q","K"};        String type[]={"方块","梅花","红桃","黑桃"};        for(int i=0;i<52;i++)        {            poke[i]=new sendPoke(type[i/13],num[i%13]);        }    }    public void xipai()    {        int count=52;        for(int j=0;j<52;j++)        {            int x=(int)(Math.random()*count);            sendPoke temp=poke[count-1];            poke[count-1]=poke[x];            poke[x]=temp;            count--;        }    }    public void fapai()    {        for(int i=0;i<52;i++)        {             if(i%13==0)                 System.out.println("\n");             System.out.print(poke[i]);        }    }}

测试块:

public class test_sendpoke {    public static void main(String[] args)    {        send c1=new send();        c1.chushihua();        c1.xipai();        c1.fapai();    }}

其中一种结果:
方块 8 红桃 4 红桃 7 方块 5 梅花 5 梅花 2 方块 3 黑桃 7 梅花 8 红桃 Q 红桃 9 方块 Q 黑桃 10

黑桃 J 梅花 Q 梅花 6 黑桃 4 方块 4 黑桃 3 方块 9 黑桃 A 红桃 3 红桃 J 梅花 10 方块 2 梅花 K

方块 6 黑桃 K 梅花 7 红桃 2 红桃 5 黑桃 6 梅花 J 红桃 6 梅花 3 方块 A 黑桃 5 方块 J 红桃 10

方块 7 红桃 K 黑桃 Q 红桃 A 梅花 9 黑桃 9 黑桃 8 红桃 8 黑桃 2 梅花 A 方块 10 方块 K 梅花 4

0 0
原创粉丝点击