C语言编程——发牌洗牌模拟,求帮助
四人(分别用A,B,C,D)参与打80分(两副牌,并且包含大小鬼)需要大家编程模拟给四人发牌工作,要求每次发牌后结果各不相同。–发牌规则是最后8张牌不发,作为扣底要求发牌...
四人(分别用A,B,C,D)参与打80分(两副牌,并且包含大小鬼)
需要大家编程模拟给四人发牌工作,要求每次发牌后结果各不相同。
– 发牌规则是最后8张牌不发,作为扣底
要求发牌完毕后,输入四人名字( A,B,C,D )即可看到他所持有的牌。
我的程序如下(摘用了一些老师给的源代码)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct CARD
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
struct CARD card[56];
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<56; i++)
{
j=rand()%56;
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
/*函数功能:输出发牌结果
函数参数:结构体数组wCard,表示有52张牌
函数返回值:无
*/
void Deal(struct CARD *wCard)
{
int i;
for (i=0; i<12; i++)
{
printf("%10s %5s\n",
wCard[i].suit,
wCard[i].face);
}
}
/* 函数功能:将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";
wCard[54].face="Small";
wCard[53].face="ghost";
wCard[54].face="ghost";
wCard[55].face="Big";
wCard[56].face="Small";
wCard[55].face="ghost";
wCard[56].face="ghost";
}
int main()
{
srand(time(NULL));
printf("Please choose your position(A、B、C、D):");
scanf("")
FillCard(card,suit,face);
Shuffle(card);
Deal(card);
printf
这几天生病,头脑很沉,所以程序没有编完,所以想走捷径,希望有大牛帮我看看这个程序,然后完成下,最好再做些注释,好让我看懂。
若能解答,感激不尽。 展开
需要大家编程模拟给四人发牌工作,要求每次发牌后结果各不相同。
– 发牌规则是最后8张牌不发,作为扣底
要求发牌完毕后,输入四人名字( A,B,C,D )即可看到他所持有的牌。
我的程序如下(摘用了一些老师给的源代码)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct CARD
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
struct CARD card[56];
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<56; i++)
{
j=rand()%56;
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
/*函数功能:输出发牌结果
函数参数:结构体数组wCard,表示有52张牌
函数返回值:无
*/
void Deal(struct CARD *wCard)
{
int i;
for (i=0; i<12; i++)
{
printf("%10s %5s\n",
wCard[i].suit,
wCard[i].face);
}
}
/* 函数功能:将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";
wCard[54].face="Small";
wCard[53].face="ghost";
wCard[54].face="ghost";
wCard[55].face="Big";
wCard[56].face="Small";
wCard[55].face="ghost";
wCard[56].face="ghost";
}
int main()
{
srand(time(NULL));
printf("Please choose your position(A、B、C、D):");
scanf("")
FillCard(card,suit,face);
Shuffle(card);
Deal(card);
printf
这几天生病,头脑很沉,所以程序没有编完,所以想走捷径,希望有大牛帮我看看这个程序,然后完成下,最好再做些注释,好让我看懂。
若能解答,感激不尽。 展开
4个回答
展开全部
实现了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;
}
#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;
}
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SUIT 52
#define HALF 26
#define COLOR ((char)13)
// 扑克牌的编码是一组13进制的数据,从0编到51
// 函数tell用于解释编码的内容
int tell(char card, char * description)
{
char i=0, j=0;
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","Jack","Queen","King"};
//题干比较奇怪,要求扣8张底牌,但是含大小王。正确的规则应该是不含大小王的情况下扣8张牌,每人11张牌就对了
//否则应该扣10张牌或者6张牌,否则每人发牌得到的牌数不相同(不是4的倍数)
//如需要,可自行定义JB=52,JA=53,SUIT=54,HALF=27即可。
//if (card > JB || card < 0 || !description) return 0;
//if (card == JB) { sprintf(description, "Big Joker"); return 1; }
//if (card == JA) { sprintf(description, "Little Joker"); return 1; }
i = card / COLOR; j = card % COLOR;
sprintf(description, "%s %s", suit[i], face[j]);
return 1;
}
//初始化牌,形成一个顺序牌
int init(char *poker)
{
char i = 0;
if (!poker) return 0;
for (i=0; i<SUIT; i++) poker[i]=i;
srand((unsigned)time(0));
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是左手先交换法
int wash_left_hand(char *poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i * 2; //上半组
boker[j] = poker[i];
k = i + HALF; //下半组
boker[j+1]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是右手先交换法
//注意左右手先后顺序的区别在于变量j的奇偶变化
int wash_right_hand(char *poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i * 2; //上半组
boker[j+1] = poker[i];
k = i + HALF; //下半组
boker[j]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//在当前牌的基础上洗一次牌,主要用随机函数的奇偶性确定用左手洗还是右手洗
int wash_once(char *poker)
{
return rand()%2?wash_left_hand(poker):wash_right_hand(poker);
}
//在当前牌的基础上随机洗若干次牌,至少8次,最多18次
int wash_full(char *poker)
{
int i = 0, j = 0, k = 0;
i = rand() % 10 + 8;
printf("\nTry to wash %d times.\n", i);
for (j=0; j < i ; j++) k+=wash_once(poker);
return k;
}
int main(void)
{
char choice = 0, i = 0, j = 0; char poker[SUIT], card[16];
init(poker);
do {
printf("\n\tWelcome to My Poker Game!\n");
printf("\t0-Wash.\n");
printf("\t1-Show A.\n");
printf("\t2-Show B.\n");
printf("\t3-Show C.\n");
printf("\t4-Show D.\n");
printf("\tany other number-quit.\n");
printf("Your Choice?");
scanf("%d", &choice);
if(choice <0 || choice >4) break;
if(choice ==0) {
wash_full(poker);
continue;
}//end if
i = (choice - 1) * 11; //注意用11张牌,扣8张底牌
printf("His card is...\n");
for(j = i; j<i+11; j++) {
tell(poker[j], card);
printf("%s\t", card);
}//next
printf("\n");
}while(1);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SUIT 52
#define HALF 26
#define COLOR ((char)13)
// 扑克牌的编码是一组13进制的数据,从0编到51
// 函数tell用于解释编码的内容
int tell(char card, char * description)
{
char i=0, j=0;
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","Jack","Queen","King"};
//题干比较奇怪,要求扣8张底牌,但是含大小王。正确的规则应该是不含大小王的情况下扣8张牌,每人11张牌就对了
//否则应该扣10张牌或者6张牌,否则每人发牌得到的牌数不相同(不是4的倍数)
//如需要,可自行定义JB=52,JA=53,SUIT=54,HALF=27即可。
//if (card > JB || card < 0 || !description) return 0;
//if (card == JB) { sprintf(description, "Big Joker"); return 1; }
//if (card == JA) { sprintf(description, "Little Joker"); return 1; }
i = card / COLOR; j = card % COLOR;
sprintf(description, "%s %s", suit[i], face[j]);
return 1;
}
//初始化牌,形成一个顺序牌
int init(char *poker)
{
char i = 0;
if (!poker) return 0;
for (i=0; i<SUIT; i++) poker[i]=i;
srand((unsigned)time(0));
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是左手先交换法
int wash_left_hand(char *poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i * 2; //上半组
boker[j] = poker[i];
k = i + HALF; //下半组
boker[j+1]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是右手先交换法
//注意左右手先后顺序的区别在于变量j的奇偶变化
int wash_right_hand(char *poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i * 2; //上半组
boker[j+1] = poker[i];
k = i + HALF; //下半组
boker[j]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//在当前牌的基础上洗一次牌,主要用随机函数的奇偶性确定用左手洗还是右手洗
int wash_once(char *poker)
{
return rand()%2?wash_left_hand(poker):wash_right_hand(poker);
}
//在当前牌的基础上随机洗若干次牌,至少8次,最多18次
int wash_full(char *poker)
{
int i = 0, j = 0, k = 0;
i = rand() % 10 + 8;
printf("\nTry to wash %d times.\n", i);
for (j=0; j < i ; j++) k+=wash_once(poker);
return k;
}
int main(void)
{
char choice = 0, i = 0, j = 0; char poker[SUIT], card[16];
init(poker);
do {
printf("\n\tWelcome to My Poker Game!\n");
printf("\t0-Wash.\n");
printf("\t1-Show A.\n");
printf("\t2-Show B.\n");
printf("\t3-Show C.\n");
printf("\t4-Show D.\n");
printf("\tany other number-quit.\n");
printf("Your Choice?");
scanf("%d", &choice);
if(choice <0 || choice >4) break;
if(choice ==0) {
wash_full(poker);
continue;
}//end if
i = (choice - 1) * 11; //注意用11张牌,扣8张底牌
printf("His card is...\n");
for(j = i; j<i+11; j++) {
tell(poker[j], card);
printf("%s\t", card);
}//next
printf("\n");
}while(1);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//很想知道:为什么两副牌只有56张??
//其实你给的程序已经差不多了
//下面我对你的程序做了一点修改;我只发了一副牌;要发两副牌只需要做简单的修改就可以了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctime>
struct CARD
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
struct CARD card[54]; //一副牌只有54张
struct CARD ghost[2]={{"ghost","Big"},{"ghost","Small"}};
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","jack","Queen","King"};
/* 函数功能:将54张牌的顺序打乱,
函数参数:结构体数组wCard,表示52张牌
函数返回值:无
*/
void Shuffle(char wname,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,表示有52张牌
函数返回值:无
*/
void Deal(char wname,struct CARD *wCard)
{
int i=0,j=13;
switch(wname)
{
case'A':j=15;
break;
case'B':i+=15;
break;
case'C':i+=28;
break;
case'D':i+=31;
break;
default:break;
}
for (i; j>0; i++,j--)
{
printf("%10s %5s\n", wCard[i].suit,wCard[i].face);
}
}
/* 函数功能:将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[52]=ghost[0];
wCard[52]=ghost[1];
/* wCard[55].face="Big";
wCard[56].face="Small";
wCard[55].face="ghost";
wCard[56].face="ghost";*/
}
int main()
{
char name;
srand(time(NULL));
printf("Please choose your position(A、B、C、D):");
scanf("%c",&name);
FillCard(card,suit,face);
Shuffle(name,card);
Deal(name,card);
return 0;
}
//其实你给的程序已经差不多了
//下面我对你的程序做了一点修改;我只发了一副牌;要发两副牌只需要做简单的修改就可以了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctime>
struct CARD
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
struct CARD card[54]; //一副牌只有54张
struct CARD ghost[2]={{"ghost","Big"},{"ghost","Small"}};
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","jack","Queen","King"};
/* 函数功能:将54张牌的顺序打乱,
函数参数:结构体数组wCard,表示52张牌
函数返回值:无
*/
void Shuffle(char wname,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,表示有52张牌
函数返回值:无
*/
void Deal(char wname,struct CARD *wCard)
{
int i=0,j=13;
switch(wname)
{
case'A':j=15;
break;
case'B':i+=15;
break;
case'C':i+=28;
break;
case'D':i+=31;
break;
default:break;
}
for (i; j>0; i++,j--)
{
printf("%10s %5s\n", wCard[i].suit,wCard[i].face);
}
}
/* 函数功能:将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[52]=ghost[0];
wCard[52]=ghost[1];
/* wCard[55].face="Big";
wCard[56].face="Small";
wCard[55].face="ghost";
wCard[56].face="ghost";*/
}
int main()
{
char name;
srand(time(NULL));
printf("Please choose your position(A、B、C、D):");
scanf("%c",&name);
FillCard(card,suit,face);
Shuffle(name,card);
Deal(name,card);
return 0;
}
追问
我搞错了。是两副牌。。。谢谢你。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询