一副扑克牌的随机发牌

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

<span style="font-size:12px;">
//这是一副牌斗地主的发牌(54张加了大小王)
//定义一个结构体,然后定义一个该结构体的数组Card[54](大写的),存储扑克牌(这个存储的是字符串) 
//定义一个int类型的card[54](小写的),来表示扑克牌,比如105,1代表花色(红桃),05代表面值5  
//再比如313,就是方块,401就是梅花A  
 
//********************************************************** 
#include<iostream>
#include<ctime>
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<<endl;
}
 
 
//转换函数:card[52]->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<<endl;
 
}
 
//*************************冒泡排序******************************
void _sortCard() //二维数组,每层排序(即每个人18张牌排序)
{
         inti,j,k;
         structpuke temp;
         for(i=0;i<3;i++)
         {
                   for(j=0;j<18;j++)
                            for(k=0;k<18-j-1;k++)
                            {
                                     if(Card[i*18+k].flag>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<<card[i]<<"";
         cout<<endl;
 
}
*/
 
/* void _print3()  //检测牌重复不
{
         inti = 0;
         for(;i<54;i++)
                   cout<<Card[i].flag<<"";
         cout<<endl;
 
}
*/
 
//********************************************
void _printCard()  //打印每个人的牌
{
         inti,j;
         for(i=0;i<3;i++){
                   cout<<"第"<<i+1<<"个人的牌:";
                   for(j=0;j<18;j++)
                   {
                            cout<<Card[i*18+j].color<<Card[i*18+j].number[0]<<Card[i*18+j].number[1]<<"";
 
                   }
                   cout<<endl;
         }
         cout<<endl;
}
 
 
 
//*****************************
void _testCard() //验牌重复不
{
         inti,j,flag = 0;
         for(i=0;i<54;i++)
                   for(j=0;j<54-i-1;j++)
                   {
                            if(i!=j)
                            {
                                     if(card[i]==card[j])
                                               flag= 1;
                            }
                   }
                   if(flag==0)
                            cout<<"验证成功,无重复牌!"<<endl;
                   if(flag==1)
                            cout<<"验证失败,存在重复牌!"<<endl;
 
 
}
 
int main()
{
         _shuffleCard();
         _turnCard();
         //  _print2();
         //  _print3();
         _sortCard();
         _testCard();
         _printCard();
         system("pause");
         return0;
}
 </span>

运行如下:


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值