洗牌、发牌过程模拟

/*
  洗牌、发牌过程模拟
  假设一副牌没有大小王,有四种花色,每一种有13张,轮流发牌.
*/
#include
#include
#include
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     {
          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)<          cout<     }
     cout<     //排序:按花色、大小
     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)<          cout<     }
     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;
}

你可能感兴趣的:(C++)