一副扑克牌的随机发牌

前几天被问如何存储一副牌并公平发牌,就看了下,写的简单的代码如下:

//这是一副牌斗地主的发牌(54张加了大小王)
//定义一个结构体,然后定义一个该结构体的数组Card[54](大写的),存储扑克牌(这个存储的是字符串) 
//定义一个int类型的card[54](小写的),来表示扑克牌,比如105,1代表花色(红桃),05代表面值5  
//再比如313,就是方块,401就是梅花A  
 
//********************************************************** 
#include
#include
using namespace std;
struct puke
{
         charcolor;  //花色
         charnumber[2]; //牌的数值
         intflag;    //3,4...J Q K A 2 XW DW
};
struct puke Card[54];  //加入大小王,54张,3人斗地主
int card[54];        //每张牌对应的整数数组
//如果按照card[52]排序,那么牌由小到大就是:A,2,3,4,5,6,7,8,9,10,J,Q,K 
//而牌由小到大排序是:3,4,5,6,7,8,9,10,J,Q,K,A,2 
 
//*************************************************************
void _shuffleCard()   //洗牌
{
         inti,j,k,temp;
         for(i=0;i<4;i++)
                   for(j=0;j<13;j++)
                            card[i*13+j]=(i+1)*100+j+1;  //101 102...初始化牌的数组
         card[52]=516;//小王大王
         card[53]=517;
         srand(time(NULL));   //产生随机数列的种子
         for(i=0;i<54;i++)
         {
                   k=rand()%(53-i+1)+i;   //利用随机数打乱数组
                   temp=card[i];
                   card[i]=card[k];
                   card[k]=temp;
         }
         cout<Card[52],将牌的存储由int类型转换为char类型
void _turnCard()   //将数字转换成对应的花色的牌存入扑克数组
{ 
         inti,temp;
         for(i=0;i<54;i++)
         {
                   temp= card[i]%100; //取牌的数值,A为1 一直K 13
                   Card[i].color= card[i]/100+'0';
                   if(Card[i].color=='1')   //为了可视化的代替红黑方梅四种不同的花色
                            Card[i].color='*';
                   if(Card[i].color=='2')
                            Card[i].color='&';
                   if(Card[i].color=='3')
                            Card[i].color='#';
                   if(Card[i].color=='4')
                            Card[i].color='~';
                   if(Card[i].color=='5')
                            Card[i].color='\0';
                   if(temp==1){
                            Card[i].number[0]='A';
                            Card[i].number[1]='\0';
                            continue;
                   }
                   if(temp==10){
                            Card[i].number[0]='1';
                            Card[i].number[1]='0';
                            continue;
                   }   
                   if(temp==11){
                            Card[i].number[0]='J';
                            Card[i].number[1]='\0';
                            continue;
                   }
                   if(temp==12){
                            Card[i].number[0]='Q';
                            Card[i].number[1]='\0';
                            continue;
                   }
                   if(temp==13){
                            Card[i].number[0]='K';
                            Card[i].number[1]='\0';
                            continue;
                   }
                   if(temp==16){
                            Card[i].number[0]='X';
                            Card[i].number[1]='W';
                            continue;
                   }
                   if(temp==17){
                            Card[i].number[0]='D';
                            Card[i].number[1]='W';
                            continue;
                   }
 
                   Card[i].number[0]=temp+48;
                   Card[i].number[1]='\0';
 
         }
 
         for(i=0;i<54;i++)
         {
                   temp= card[i]%100; 
                   Card[i].flag= temp;
                   if(temp==1||temp==2)   //A 2比 3,4,,,k 大
                            Card[i].flag+=13;
 
         }
         //cout<Card[i*18+k+1].flag)
                                     {
                                               temp= Card[i*18+k];
                                               Card[i*18+k]= Card[i*18+k+1];
                                               Card[i*18+k+1]= temp;         
                                     }
                            }
 
         }
}
 
/*
void _print2()  //打印每个人的牌对应的数字
{
         inti = 0;
         for(;i<54;i++)
                   cout<

运行如下:


一副扑克牌的随机发牌_第1张图片

你可能感兴趣的:(C++简单小demo)