- 黑桃云
-
实现了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 ", 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 ", 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 ", postion[posA].getcard[i].suit, postion[posA].getcard[i].face);
}
break;
case "B": for(i=0; i<25; i++)
{
printf("%10s %5s ", postion[posB].getcard[i].suit, postion[posB].getcard[i].face);
}
break;
case "C": for(i=0; i<25; i++)
{
printf("%10s %5s ", postion[posC].getcard[i].suit, postion[posC].getcard[i].face);
}
break;
case "D": for(i=0; i<25; i++)
{
printf("%10s %5s ", postion[posD].getcard[i].suit, postion[posD].getcard[i].face);
}
break;
}
}
void outputLeftCard()//输出底牌
{
int i;
for(i=0; i<8; i++)
printf("%10s %5s ", 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: ");
print("A");
}
if(pos !="B")
{
printf("B: ");
print("B");
}
if(pos !="C")
{
printf("C: ");
print("C");
}
if(pos !="D")
{
printf("D: ");
print("D");
}
printf("底牌为: ");
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(" Try to wash %d times. ", 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(" Welcome to My Poker Game! ");
printf(" 0-Wash. ");
printf(" 1-Show A. ");
printf(" 2-Show B. ");
printf(" 3-Show C. ");
printf(" 4-Show D. ");
printf(" any other number-quit. ");
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... ");
for(j = i; j<i+11; j++) {
tell(poker[j], card);
printf("%s ", card);
}//next
printf(" ");
}while(1);
return 0;
}
- nicehost
-
我也想学啊
- 陶小凡
-
//很想知道:为什么两副牌只有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 ", 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;
}