关闭

洗牌、发牌过程模拟

951人阅读 评论(0) 收藏 举报
/*
  洗牌、发牌过程模拟
  假设一副牌没有大小王,有四种花色,每一种有13张,轮流发牌.
*/
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
enum style {RedPeach=6,BlackPeach=3,Club=5,Pane=4};
const char* Replace(char ch);
struct Card
{
     char  Value; //牌点
     style Style; //花色
}Person[4][13];

int main()
{
     char card[53]="3456789BCDEFG";//B~G:10~2
     style cs[53],cstmp;
     struct Card cardtmp;
     int i=0,j=0,k=0;
     char tmp;
     //初始化
     for(i=1;i<=3;i++)
          for(j=0;j<13;j++)
               *(card+i*13+j)=*(card+j);
     for(i=0;i<4;i++)
          for(j=0;j<13;j++)
               *(cs+i*13+j)=(style)(i+3);
     //洗牌
     srand((unsigned)time(NULL));
     for(i=0;i<sizeof(card);i++)
     {
          j=rand()%52; //0~51
          tmp=*(card+j);
          *(card+j)=*(card+51-j);
          *(card+51-j)=tmp;
          cstmp=*(cs+j);
          *(cs+j)=*(cs+51-j);
          *(cs+51-j)=cstmp;
     }
     //发牌
     k=0;
     for(i=0;i<13;i++)
     {
          for(j=0;j<4;j++)
          {
               Person[j][i].Style=*(cs+k);
               Person[j][i].Value=*(card+k++);
          }
     }
     for(i=0;i<4;i++)
     {
          for(j=0;j<13;j++)
               cout<<(char)(Person[i][j].Style)<<Replace(Person[i][j].Value);
          cout<<endl;
     }
     cout<<endl;
     //排序:按花色、大小
     for(i=0;i<4;i++)
          for(j=0;j<13;j++)
               for(k=j+1;k<13;k++)
               {
                    if(Person[i][j].Style>Person[i][k].Style)
                    {
                         cardtmp.Style=Person[i][j].Style;
                         cardtmp.Value=Person[i][j].Value;
                         Person[i][j].Style=Person[i][k].Style;
                         Person[i][j].Value=Person[i][k].Value;
                         Person[i][k].Style=cardtmp.Style;
                         Person[i][k].Value=cardtmp.Value;
                    }
               }
     for(i=0;i<4;i++)
          for(j=0;j<13;j++)
               for(k=j+1;k<13;k++)
               {
                    if(Person[i][j].Style==Person[i][k].Style  &&Person[i][j].Value>Person[i][k].Value)
                    {
                         cardtmp.Value=Person[i][j].Value;
                         Person[i][j].Value=Person[i][k].Value;
                         Person[i][k].Value=cardtmp.Value;
                    }
               }
     for(i=0;i<4;i++)
     {
          for(j=0;j<13;j++)
               cout<<(char)(Person[i][j].Style)<<Replace(Person[i][j].Value);
          cout<<endl;
     }
     return 0;
}

const char* Replace(char ch)
{
     const char* Find[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
     if((ch>='3') &&(ch<='9'))
          return Find[ch-'3'];
     else if((ch>='B') &&(ch<='G'))
          return Find[ch-'B'+7];
     else
          return NULL;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14184次
    • 积分:241
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档