#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define PLAYER 4//玩家人数
#define NUM 13//玩家拿牌数
#define SIZE 52//所有牌数
//声明函数
void PokerRand(int *pokerRand)
void Palyer(int *pokerRand)
void Process(int *countA, int *countB, int *countC, int *countD)
void Output(int *poker, int *countA, int *countB, int *countC, int *countD)
struct PokerGame
{
int A[NUM]//记录玩家手中的黑桃牌
int B[NUM]//记录玩家手中的红桃牌
int C[NUM]//记录玩家手中的梅花牌
int D[NUM]//记录玩家手中的方片牌
int manNum[NUM]//记录玩家手里所有的牌
}man[PLAYER]
//随机产生52张牌
void PokerRand(int *pokerRand)
{
int i, j
srand((unsigned)time(NULL))
for (i=0i<SIZEi++)
{
MARK:pokerRand[i] = rand()%52
for (j=0j<ij++)
{
if (pokerRand[i] == pokerRand[j])
{
goto MARK
}
}
}
}
//给4个玩家发牌
void Palyer(int *pokerRand)
{
int i, j
int count = 0
for (j=0j<NUMj++)
{
for (i=0i<PLAYERi++)//轮流发牌
{
man[i].manNum[j] = pokerRand[count++]
}
}
}
//统计玩家手中的牌
void Process(int *countA, int *countB, int *countC, int *countD)
{
int i, j
for (i=0i<PLAYERi++)
{
countA[i] = 0
countB[i] = 0
countC[i] = 0
countD[i] = 0
for (j=0j<NUMj++)//统计四个玩家手中的牌
{
if ((man[i].manNum[j] >= 0) &&(man[i].manNum[j] <13))//黑桃
{
man[i].A[ countA[i]++ ] = man[i].manNum[j]
}
else if (man[i].manNum[j] <26)//红桃
{
man[i].B[ countB[i]++ ] = man[i].manNum[j]
}
else if (man[i].manNum[j] <39)//梅花
{
man[i].C[ countC[i]++ ] = man[i].manNum[j]
}
else//方片
{
man[i].D[ countD[i]++ ] = man[i].manNum[j]
}
}
}
}
//输出
void Output(int *poker, int *countA, int *countB, int *countC, int *countD)
{
int i, j
printf("扑克牌自动发牌 %c(黑) %c(红) %c(梅) %c(方):\n", 6, 3, 5, 4)
for (i=0i<PLAYERi++)
{
printf("\n第%d人 :\n", i+1)//开始输出第i个玩家
printf("%c:\t", 6)//输出第i个玩家的黑桃牌
for (j=0j<countA[i]j++)
{
if (poker[ man[i].A[j] ] == 10)//假如等于10,以%d格式输出
{
printf("%4d", poker[ man[i].A[j] ])
}
else//否则以%c格式输出
{
printf("%4c", poker[ man[i].A[j] ])
}
}
printf("\n")
printf("%c:\t", 3)//输出第i个玩家的红桃牌
for (j=0j<countB[i]j++)
{
if (poker[ man[i].B[j] ] == 10)
{
printf("%4d", poker[ man[i].B[j] ])
}
else
{
printf("%4c", poker[ man[i].B[j] ])
}
}
printf("\n")
printf("%c:\t", 5)//输出第i个玩家的梅花牌
for (j=0j<countC[i]j++)
{
if (poker[ man[i].C[j] ] == 10)
{
printf("%4d", poker[ man[i].C[j] ])
}
else
{
printf("%4c", poker[ man[i].C[j] ])
}
}
printf("\n")
printf("%c:\t", 4)//输出第i个玩家的方片牌
for (j=0j<countD[i]j++)
{
if (poker[ man[i].D[j] ] == 10)
{
printf("%4d", poker[ man[i].D[j] ])
}
else
{
printf("%4c", poker[ man[i].D[j] ])
}
}
printf("\n")
}
}
void main(void)
{
int countA[PLAYER] = { 0 }//记录4个玩家持黑桃牌数
int countB[PLAYER] = { 0 }//记录4个玩家持红桃牌数
int countC[PLAYER] = { 0 }//记录4个玩家持梅花牌数
int countD[PLAYER] = { 0 }//记录4个玩家持方片牌数
int pokerRand[SIZE] = { 0 }//存放随机产生52张牌
int poker[SIZE] = {65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,}
PokerRand(pokerRand)//洗牌
Palyer(pokerRand)//发牌
Process(countA, countB, countC, countD)//整牌
Output(poker, countA, countB, countC, countD)//亮牌
printf("\n\n\n")
system("pause")
}
#include <stdio.h>#include <stdlib.h>
#include <time.h>
struct POKER
{
int num // A:1 , J:11 , Q:12 , K:13
int color //黑1,红2,梅3,方4
}s[52]
int main()
{
int i,num,color
memset(s,0,sizeof(struct POKER)*52)
srand((unsigned)time(0))
int count=0
while(count<52)
{
num=rand()%13+1
color=rand()%4+1
int flag_skip=0,flag_end=0
for(i=0i<52i++)
{
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=0i<52i++)
{
if(i%13==0)
{
printf("\n玩家%d : ",i/13+1)
}
switch(s[i].num)
{
case 1: putchar('A')break
case 11: putchar('J')break
case 12: putchar('Q')break
case 13: putchar('K')break
default: printf("%d",s[i].num)
}
switch(s[i].color)
{
case 1: putchar(6)break
case 2: putchar(3)break
case 3: putchar(5)break
case 4: putchar(4)break
default: printf(" ")
}
printf(" ")
}
return 0
}
实现了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)
{
inti,j
struct CARD temp
for (i=0i<54i++)
{
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=0i<50i++)//发牌数
{
// 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=0i<50i++)//发牌数
{
// 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=0i<52i++)
{
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=0i<25i++)
{
printf("%10s %5s\n", postion[posA].getcard[i].suit, postion[posA].getcard[i].face)
}
break
case 'B': for(i=0i<25i++)
{
printf("%10s %5s\n", postion[posB].getcard[i].suit, postion[posB].getcard[i].face)
}
break
case 'C': for(i=0i<25i++)
{
printf("%10s %5s\n", postion[posC].getcard[i].suit, postion[posC].getcard[i].face)
}
break
case 'D': for(i=0i<25i++)
{
printf("%10s %5s\n", postion[posD].getcard[i].suit, postion[posD].getcard[i].face)
}
break
}
}
void outputLeftCard()//输出底牌
{
int i
for(i=0i<8i++)
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
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)