求高手帮忙写一个c语言发牌程序

求高手帮忙写一个c语言发牌程序,第1张

#include <time.h>

#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")

}

这是以随机的形式发牌的。希望能帮到你。

int main(){int a[60] int b[60] int w = 0 int wi,wj,wii int i,j,index,x=0,m for(i=0i<54i++)a[i]=i+1 srand((unsigned)time(0)) for(i=0i<54){index=rand()%54 if(a[index]!=0){b[i]=a[index] //printf("%d ",b[i]) a[index]=0 ++i }}printf("\n说明:\n") printf("*******************\n") printf("**红桃 %c ",3) printf("**\n") printf("**方块 %c ",4) printf("**\n") printf("**梅花 %c ",5) printf("**\n") printf("**黑桃 %c ",6) printf("**\n") printf("**大王 A **\n**小王 a **\n") printf("*******************\n") printf("\n请输入打牌人数:\n") scanf("%d",&w) wi = 54/w wj = 54%w //printf("%d,%d",wi,wj) for (j = 0j <wj++){m = 0 printf("\n\n第%d个人的牌\n",j+1) wj -- if (wj>=0)wii = wi + 1 elsewii = wi//红桃printf("红桃 %c ",3) printf(":") for(i = j*wiii<(j+1)*wiii++){if (b[i]<=13&&b[i]>0){m++ if (b[i]==11)printf("J ") else if (b[i]==12)printf("Q ") else if (b[i]==13)printf("K ") elseprintf("%d ",b[i]) }} //方块printf("\n方块 %c ",4) printf(":") for(i = j*wiii<(j+1)*wiii++){if (b[i]<=26&&b[i]>13){m++ b[i] = b[i]-13 if (b[i]==11)printf("J ") else if (b[i]==12)printf("Q ") else if (b[i]==13)printf("K ") elseprintf("%d ",b[i]) }} //梅花printf("\n梅花 %c ",5) printf(":") for(i = j*wiii<(j+1)*wiii++){ if (b[i]<=39&&b[i]>26){m++ b[i] = b[i]-26 if (b[i]==11)printf("J ") else if (b[i]==12)printf("Q ") else if (b[i]==13)printf("K ") elseprintf("%d ",b[i]) }}//黑桃printf("\n黑桃 %c ",6) printf(":") for(i = j*wiii<(j+1)*wiii++){if (b[i]<=52&&b[i]>39){m++ b[i] = b[i]-39 if (b[i]==11)printf("J ") else if (b[i]==12)printf("Q ") else if (b[i]==13)printf("K ") elseprintf("%d ",b[i]) }}//大小王printf("\n大小王 A or a :") for(i = j*wiii<=(j+1)*wiii++){if (b[i]==53){m++ printf("A ") }if (b[i]==54){m++ printf("a ") }}printf("\n总共:%d张牌!!!",m) } }

# include <stdio.h>

#include <time.h>

#include <stdlib.h>

int card[52]

void Display(int i)//将0~51之间的数映射成牌号和花色 

{

              switch(i%4)

              {

                               case 0:printf("红桃")break

                               case 1:printf("黑桃")break

                               case 2:printf("梅花")break

                               case 3:printf("方块")break

              }

              if(i/4+1<11)

                                printf("%d\t",i/4+1) 

              else if(i/4+1==11)

                  printf("%c\t",'J') 

              else if(i/4+1==12)

                   printf("%c\t",'Q') 

              else if(i/4+1==13)

                   printf("%c\t",'K') 

}

void InitCard()

{

     int i,j,temp

     srand((int)time(0))

     for(i=0 i<52 i++)//随机生成52个数 

     {

              temp = rand()%52

              for(j=0 j<i j++)

              {

                       if(card[j] == temp) break

              }

              if(j==i) card[i] = temp

              else i--

     }

     

     for(i=0 i<52 i++)//初始化输出 

     {

              if(i%5==0 && i!=0) printf("\n")

              Display(card[i])

     }

}

void deal()

{

     int i,j,temp

     srand((int)time(0))

     for(i=0 i<52 i++)

     {

              temp = rand()%52

              for(j=0 j<i j++)

              {

                       if(card[j] == temp) break

              }

              if(j==i) card[i] = temp

              else i--

     }

     

     for(i=0 i<52 i++)//发牌输出 

     {

              

              if(i%5==0 && i!=0) printf("\n")

              if(i%13==0 && i!=0) printf("\n\n")

              if(i%13==0)

                         printf("第%d个玩家的牌:\n",i/13+1)

                         

              Display(card[i])

     }

}

int main(void)

{

    int key=0 

    printf("================主菜单==============\n")//菜单 

    printf("=============1、 初始化扑克=========\n")

    printf("=============2、 洗牌、发牌=========\n")

    printf("=============3、 退出 ==============\n")

    

    while(key!=3)//选择菜单 

    {

                 printf("请输入菜单功能编号:")

                 scanf("%d",&key)

                 

                 switch(key)

                 {

                            case 1:InitCard()break//初始化 

                            case 2:deal()//发牌 

                            default: break

                 }

                 

                 printf("\n\n")

    }

    return 0

}

代码如上,测试结果:

基本思想就是,只要随机生成的数,发给谁都一样;所以初始化和发牌时候先随机生成52个不同的数0~51,初始化时,直接输出就可以了。发牌时按13一组一次分给一个人就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7811425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存