通过C语言模拟洗发牌的过程,牌一共有52张,四种花色,13中数字。
typedef struct
{
char suit[10]; //花色
char face[10]; //牌面
}CARD;
void FillCard(CARD card[]);
//构成扑克牌
void FillCard(CARD card[])
{
char *face[] = {"A","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
char *suit[] = {"Spades","Hearts","Clubs","Diamonds"};
int i = 0;
for(;i < 13;i ++)
{
strcpy(card[i].face,face[i % 13]);
strcpy(card[i].suit,suit[i / 13]);
}
}
#include
#include
int main(){
int a = rand();
printf("%d\n",a);
return 0;
}
#include
#include
#include
int main() {
int a;
srand((unsigned)time(NULL));
a = rand();
printf("%d\n", a);
return 0;
}
//生成无规则的乱序的洗牌数组
void Wash(int result[])
{
int randnum = 0 ,temp = 0;
//对所有的数组元素进行初始化
for(int i = 0;i < 52; i++)
{
result[i] = i;
}
//生成随机的是数列
for(int i = 0; i< 52;i ++)
{
randnum = rand() % 52;
temp = result[randnum];
result[randnum] = result[i];
result[i] = temp;
}
return ;
}
//发牌,传入对应的结构体的数组和随机打乱的数组序列
void Deal(CARD card[],int result[])
{
for(int i = 0;i < 52;i ++)
{
printf("%10s %7s \n",card[result[i]].face,card[result[i]].suit);
}
}
int main()
{
int result[52];
//模拟洗发牌
CARD card[52];
FillCard(card);
Wash(result);
Deal(card,result);
}
#include
#include
#include
#include
typedef struct
{
char suit[10]; //花色
char face[10]; //牌面
}CARD;
void FillCard(CARD card[]); //生成牌
void Deal(CARD card[], int result[]); //发牌的应用
void Wash(int result[]);
int main()
{
int result[52];
//模拟洗发牌
CARD card[52];
FillCard(card);
Wash(result);
Deal(card,result);
return 0;
}
//生成无规则的乱序的洗牌数组
void Wash(int result[])
{
srand((unsigned)time(NULL));
int randnum = 0 ,temp = 0;
//对所有的数组元素进行初始化
for(int i = 0;i < 52; i++)
{
result[i] = i;
}
//生成随机的是数列
for(int i = 0; i< 52;i ++)
{
randnum = rand() % 52;
temp = result[randnum];
result[randnum] = result[i];
result[i] = temp;
}
return ;
}
//发牌,传入对应的结构体的数组和随机打乱的数组序列
void Deal(CARD card[],int result[])
{
for(int i = 0;i < 52;i ++)
{
printf("%10s %7s \n",card[result[i]].face,card[result[i]].suit);
}
}
//构成扑克牌
void FillCard(CARD card[])
{
char *face[] = {"A","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
char *suit[] = {"Spades","Hearts","Clubs","Diamonds"};
int i = 0;
for(;i < 52;i ++)
{
strcpy(card[i].face,face[i % 13]);
strcpy(card[i].suit,suit[i / 13]);
}
}