❶ 用c语言编写一个52张扑克牌随即洗牌的程序 并写上注释
定义一个int p[52],里面的元素就是1~52,然后,直接 for循环i:1~52,每次随机一个数 m=rand()%52 ,swap(p[i],p[m]),就ok了
❷ C语言编程——发牌洗牌模拟,求帮助
实现了2副牌的发牌,和每个人的牌和底牌
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD //牌
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
enum { posA, posB, posC, posD};//定义好每个人的位置
struct Postion
{
struct CARD getcard[25];//每人获得的牌
};
struct Postion postion[4];//分配四个位置
struct CARD leftCard[8]; //底牌
struct CARD card[54]; //54张牌
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","jack","Queen","King"};
/* 函数功能:将52张牌的顺序打乱,
函数参数:结构体数组wCard,表示52张牌
函数返回值:无
*/
void Shuffle(struct CARD *wCard)
{
int i,j;
struct CARD temp;
for (i=0; i<54; i++)
{
j=rand()%54;
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
/*函数功能:发牌结果
函数参数:结构体数组wCard,表示有54张牌
函数返回值:无
*/
void Deal(struct CARD *wCard)
{
int i,aidx=0,bidx=0,cidx=0,didx=0;
Shuffle(card);//将牌打乱
/*************发第一副牌,只发50张,分别分给A,B,C,D四个位置 4张留底**************/
// 第一次发完50张后,A,B多一张,所以下面第二次让C,D排在前面,两次发完刚好各40张 */
for (i=0; i<50; i++)//发牌数
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==1)
postion[posB].getcard[bidx++]=wCard[i];
else if(i%4==2)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==3)
postion[posD].getcard[didx++]=wCard[i];
}
/**********剩下的四张作为底牌*********/
leftCard[0]=wCard[i++];
leftCard[1]=wCard[i++];
leftCard[2]=wCard[i++];
leftCard[3]=wCard[i++];
Shuffle(card);//再次将牌打乱
/*************发第二副牌,也只发50张,分别分给A,B,C,D四个位置,4张留底,一共8张底**************/
for (i=0; i<50; i++)//发牌数
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==1)
postion[posD].getcard[didx++]=wCard[i];
else if(i%4==2)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==3)
postion[posB].getcard[bidx++]=wCard[i];
}
/**********剩下的四张作为底牌,这样就一共为8张底牌*********/
leftCard[4]=wCard[i++];
leftCard[5]=wCard[i++];
leftCard[6]=wCard[i++];
leftCard[7]=wCard[i++];
}
/* 函数功能:将52张牌按黑桃、红桃、草花、方块花色顺序,面值按A~K顺序排列
函数参数:结构体数组wCard,表示不同花色和面值的52张牌
指针数组wFace,指向面值字符串
指针数组wSuit,指向花色字符串
函数返回值:无
*/
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i/13]);
strcpy(wCard[i].face, wFace[i%13]);
}
// wCard[53].face="Big"; //大小王
strcpy(wCard[52].suit, "Small");
strcpy(wCard[52].face, "ghost");
strcpy(wCard[53].suit, "Big");
strcpy(wCard[53].face, "ghost");
}
void print(char ch)//输出牌
{
int i;
switch(ch)
{
case 'A': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posA].getcard[i].suit, postion[posA].getcard[i].face);
}
break;
case 'B': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posB].getcard[i].suit, postion[posB].getcard[i].face);
}
break;
case 'C': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posC].getcard[i].suit, postion[posC].getcard[i].face);
}
break;
case 'D': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posD].getcard[i].suit, postion[posD].getcard[i].face);
}
break;
}
}
void outputLeftCard()//输出底牌
{
int i;
for(i=0; i<8; i++)
printf("%10s %5s\n", leftCard[i].suit, leftCard[i].face);
}
int main()
{
char pos;
srand(time(NULL));
FillCard(card,suit,face);
//Shuffle(card);
Deal(card);
printf("Please choose your position(A、B、C、D):");
scanf("%c", &pos);
print(pos);//输出你所在位置的牌
/**********下面输出的是,除了你之外其他人的牌**********/
if(pos !='A')
{
printf("A:\n");
print('A');
}
if(pos !='B')
{
printf("B:\n");
print('B');
}
if(pos !='C')
{
printf("C:\n");
print('C');
}
if(pos !='D')
{
printf("D:\n");
print('D');
}
printf("底牌为:\n");
outputLeftCard();//输出底牌
return 0;
}
❸ C语言完全洗牌程式
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intn,a[20],b[20],c[20],n2;
voidinit()
{inti,j,k,t;
for(i=0;i<n2;i++)
a[i]=i+1;
srand(time(0));
for(i=0,j=n2;i<j;j--)
{k=rand()%j;
t=a[j-1];a[j-1]=a[k];a[k]=t;
}
for(i=0;i<n2;i++)
c[i]=a[i];
}
voidprt()
{for(inti=0;i<n2;i++)
{printf("%3d",a[i]);
if(i==n-1)printf("||");
}
printf("
");
}
voidwork()
{inti,t;
for(i=1;i<n2-1;i++)
{t=i<n?i+i:(i+i)%n2+1;
b[t]=a[i];
//printf("b[%d]=a[%d]
",(i+i)%n2+1,i);
}
for(i=1;i<n2-1;i++)
a[i]=b[i];
}
booldone()
{for(inti=1;i<n2;i++)
if(a[i]!=c[i])returnfalse;
returntrue;
}
intmain()
{scanf("%d",&n);
n2=n+n;
init();
prt();
do
{work();
prt();
}while(!done());
return0;
}
❹ 编写一个C语言程序,模拟洗牌和发牌的过程
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
structPOKER
{
intnum;//A:1,J:11,Q:12,K:13
intcolor;枣郑//黑1,红2,梅档岩漏3,方4
}s[52];
intmain()
{
inti,num,color;
memset(s,0,sizeof(structPOKER)*52);
srand((unsigned)time(0));
intcount=0;
while(count<52)
{
num=rand()%13+1;
color=rand()%4+1;
intflag_skip=0,flag_end=0;
for(i=0;i<52;i++)
{
if(s[i].num==num&&s[i].color==color)
{
flag_skip=1;
break;
}
}
if(flag_skip==1)
{
continue;
}
while(1)
{
i=rand()%52;
if(s[i].num==0)
{
s[i].num=num;
s[i].color=color;
count++;
break;
}
else
{
continue;
}
}
}
for(i=0;i<52;i++)
{
if(i%13==0)
{
printf(" 玩家%d:",i/13+1);
}
switch(s[i].num)
{
case行烂1:putchar('A');break;
case11:putchar('J');break;
case12:putchar('Q');break;
case13:putchar('K');break;
default:printf("%d",s[i].num);
}
switch(s[i].color)
{
case1: putchar(6);break;
case2:putchar(3);break;
case3:putchar(5);break;
case4:putchar(4);break;
default:printf("");
}
printf("");
}
return0;
}
❺ 用c语言创建一个数组用来表示52张扑克牌,然后洗牌后输出,要求每行输出13张
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<ctime>
#include<cstdlib>;
usingnamespacestd;
inta[55];
voidpt(intid){//printacardbyid
//printcolor
if(id%4==0)printf("A-");
if(id%4==1)printf("B-");
if(id%4==2)printf("C-");
if(id%4==3)printf("D-");
//printnumber
if(id/4==0)printf("A");
elseif(id/4+1<=10)printf("%d",id/4+1);
if(id/4==10)printf("J");
if(id/4==11)printf("Q");
if(id/4==12)printf("K");
}
voidshuffle(){
for(inti=0;i<52;i++)
swap(a[i],a[rand()%(i+1)]);//randomshuffle
}
intmain(){
srand((unsignedint)time(NULL));
for(inti=0;i<52;i++)
a[i]=i;
shuffle();
for(inti=0;i<52;i++){
pt(a[i]);
if(i%13==12)printf(" ");
}
return0;
}
为每张牌定义id
随机洗牌