當前位置:首頁 » 編程語言 » c語言洗牌發牌程序
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言洗牌發牌程序

發布時間: 2023-07-09 21:50:40

❶ 用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

隨機洗牌