随机发牌:去掉大小王,发给4个人,每人发13张,要求分四行,并按花色和牌点排序。样例:SAK9 HKT9876 DQ32 CJ。
1 #include<iostream> 2 #include<cstdlib> 3 using namespace std; 4 const char puke[]="123456789TJQK";//转换数组 5 const char color[] = "SHDC";//四种花色 6 void outpuke(int i ,int &j, int a[4][13], int top) 7 //第i个人的出牌函数,j表示当前第几张牌,top代表花色的数字界限(例如:黑桃top=13,红桃top=26) 8 { 9 if(a[i][j]<top)cout<<color[top/13-1];//color为了省事直接用top/13-1即可 10 while(a[i][j]<top&&j<13) 11 { 12 cout<<puke[a[i][j++]%13]; 13 } 14 cout<<" "; 15 } 16 int main() 17 { 18 int people[4][13];//四个人,每人13张牌 19 int num[4] = {0,0,0,0};//四个人的当前手牌数 20 srand(time(NULL)); 21 for(int i = 0 ; i < 52 ; i++)//随机发牌 22 { 23 int tem = rand()%4; 24 while(1) 25 { 26 if(num[tem]<13)break; 27 else tem = rand()%4; 28 } 29 if(tem==0)people[tem][num[0]++] = i; 30 if(tem==1)people[tem][num[1]++] = i; 31 if(tem==2)people[tem][num[2]++] = i; 32 if(tem==3)people[tem][num[3]++] = i; 33 } 34 for(int peo=0; peo < 4 ; peo ++ ) 35 { 36 cout<<"第"<<peo+1<<"个人的手牌: "; 37 int j = 0; 38 for(int i = 1 ; i < 5 ; i ++) 39 { 40 outpuke(peo,j,people,13*i); 41 } 42 cout<<endl; 43 } 44 }
时间: 2024-10-21 01:00:04