此程序比上一次发表的发牌程序在时间和空间上都有很大的改进,算法更有效,源代码更精简。欢迎各位指教。跳骚侦察来看看怎么样?
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
main()
{
char card[5][50];
char cardcard[]=;
char cardjoker[]=; /*jr 表示小王,JR表示大王*/
char *pr[]=;
int i,j,p[5],player,p_max[5]=;/*p[] 存储玩家牌的数目,player表示玩家,4表示底牌*/
printf(" ****************************************************************************** ");
printf(" * * ");
printf(" * Copyright belonged to mb459 * ");
printf(" * mb459@sohu.com QQ:83981459 * ");
printf(" * * ");
printf(" ****************************************************************************** ");
for(i=0;i<5;i++)p[i]=0;
srand((unsigned)time(NULL));
for(i=3;i<7;i++)
for(j=0;j<26;j++)
{player=rand()%108/25; /*随机生成将要得到当前牌的玩家*/
if(p[player]==p_max[player])
{
j--;
continue;
}
else
{
card[player][p[player]*2]=i;
card[player][p[player]*2+1]=cardcard[j];
p[player]++;
}
}
for(i=0;i<7;i+=2) /*处理大小王*/
{player=rand()%108/25;
if(p[player]==p_max[player])
{
i-=2;
continue;
}
else {
card[player][p[player]*2]=cardjoker[i];
card[player][p[player]*2+1]=cardjoker[i+1];
p[player]++;
}
}
for(i=0;i<4;i++) /*打印玩家的牌*/
{
printf("%s: ",pr[i]);
for(j=0;j<50;j++)
{
printf("%c",card[i][j]);
if(card[i][j]=='1')printf("0");
}
printf(" ");
}
printf("The left cards: "); /*打印底牌*/
for(i=0;i<16;i++)
{
printf("%c",card[4][i]);
if(card[4][i]=='1')printf("0");
}
printf(" ");
getche();
}