大家看看这个随机发牌的小程序,给出点建议,或者挑出点瑕疵,一起来讨论一下

3 篇文章 0 订阅

 

 

主要功能介绍:扑克牌共54张,但便于平均分配,所以就排除大小王,剩下52张,每人13张。

                         使这些牌随机分配给4个玩家player1-4、  以及给出牌的花色及大小、

 

 

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//初始化扑克牌列表,使52张扑克牌均为未发状态
int puker[52]={0};
int main()
{
    int cnt,p;
    int send[52] = {0},i;
    int player1[13] = {0};
    int player2[13] = {0};
    int player3[13] = {0};
    int player4[13] = {0};
    //设定随机化种子
    srand(time(NULL));
    //通过循环发牌
    for(cnt = 0;cnt<52;)
    {

        p = rand()%52; //产生一个0-52之间的数
        if(puker[p]==0)
        {

            puker[p] = 1;
            send[i++]=p;
            cnt++;
        }
    }
    //测试发牌是否成功
    //  for(i=0;i<52;i++)printf("%d->%d\n",i,send[i]);


    //将牌发给四个游戏玩家
    for(cnt = 0,i = 0;cnt<52;)
    {

        player1[i] = send[cnt++];
        player2[i] = send[cnt++];
        player3[i] = send[cnt++];
        player4[i] = send[cnt++];
        i++;
    }
    //分别打印四个玩家的牌
    //第一个玩家
    for(i=0;i<13;i++)
    {

        printf("player1:第A%i张牌(%d)->",i,player1[i]);
        if(player1[i]/13 == 0)printf("红桃");
        else if(player1[i]/13 == 1)printf("方块");
        else if(player1[i]/13 == 2)printf("梅花");
        else if(player1[i]/13 == 3)printf("黑桃");

        if(player1[i]%13 == 11)printf("J");
        else if(player1[i]%13 == 12)printf("Q");
        else if(player1[i]%13 == 0)printf("K");
        else if(player1[i]%13 == 1)printf("A");
        else printf("%d",player1[i]%13);

        printf("\n");
    }
    printf("\n");
    //第二个玩家
    for(i = 0;i<13;i++)
    {

        printf("player1:第A%i张牌(%d)->",i,player2[i]);
        if(player2[i]/13 == 0)printf("红桃");
        else if(player2[i]/13 == 1)printf("方块");
        else if(player2[i]/13 == 2)printf("梅花");
        else if(player2[i]/13 == 3)printf("黑桃");

        if(player2[i]%13 == 11)printf("J");
        else if(player2[i]%13 == 12)printf("Q");
        else if(player2[i]%13 == 0)printf("K");
        else if(player2[i]%13 == 1)printf("A");
        else printf("%d",player2[i]%13);

        printf("\n");


    }
    printf("\n");
    //第三个玩家
    for(i = 0;i<13;i++)
    {

        printf("player3:第A%i张牌(%d)->",i,player3[i]);
        if(player3[i]/13 == 0)printf("红桃");
        else if(player3[i]/13 == 1)printf("方块");
        else if(player3[i]/13 == 2)printf("梅花");
        else if(player3[i]/13 == 3)printf("黑桃");

        if(player3[i]%13 == 11)printf("J");
        else if(player3[i]%13 == 12)printf("Q");
        else if(player3[i]%13 == 0)printf("K");
        else if(player3[i]%13 == 1)printf("A");
        else printf("%d",player3[i]%13);

        printf("\n");


    }
    printf("\n");

    //第四个玩家
    for(i = 0;i<13;i++)
    {

        printf("player4:第A%i张牌(%d)->",i,player4[i]);
        if(player4[i]/13 == 0)printf("红桃");
        else if(player4[i]/13 == 1)printf("方块");
        else if(player4[i]/13 == 2)printf("梅花");
        else if(player4[i]/13 == 3)printf("黑桃");

        if(player4[i]%13 == 11)printf("J");
        else if(player4[i]%13 == 12)printf("Q");
        else if(player4[i]%13 == 0)printf("K");
        else if(player4[i]%13 == 1)printf("A");
        else printf("%d",player4[i]%13);

        printf("\n");


    }
    printf("\n");

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值