C语言 发牌与洗牌的模拟

#include
#include
#include

void shuffle(int Deck[][13])
{
int i;
for(i=1;i<=52;i++)
{
int row = rand() % 4;
int column =rand() %13;
if(Deck[row][column]==0)
Deck[row][column]=i;
}

}

void deal(int Deck[][13],char *suit[],char *value[])
{
int i,column,row;
printf("玩家1\t玩家2\t玩家3\t玩家4\t");
for(i=1;i<=52;i++)
{
row=0;
if(row<=3)
{
column=0;
if(column<=12)
{
printf(" %c%2c",row,column);

        }
        if(i%4==0) printf("\n");
        column++;
    }
    row++;
}
return 0;

}
int main()
{
char *suit[4]={"红桃","方片","草花","黑桃"};
char *value[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

int Deck[4][13];
srand(time(NULL));
shuffle(Deck);
deal(Deck,suit,value);
return 0;

}

图片说明

运行结果不对,哪位大神能看一看是哪里出错了吗?谢谢

1个回答

首先,那个viod型是不需要返回值return 0去掉
其次你这个是改错题还是编程题,我有两种办法,第一种是在你的基础上改代码
还有一种是用另一种方法只要一个自定义函数求解
第一个是在你的基础上改的:

#include <stdio.h>
void shuffle(int Deck[][13])
{
    int i;
    for(i=1;i<=52;)
    {
        int row = rand() % 4;
        int column =rand() %13;
        if(Deck[row][column]==0)
        {
            Deck[row][column]=i;
            i++;
        }
    }
}


void deal(int Deck[][13],char *suit[],char *value[])
{
    int i=0,column,row,flag=0;
    printf("玩家1\t玩家2\t玩家3\t玩家4\n");
    for(i=1;i<=52;i++)
    {
        flag=0;
        for(row=0;row<=3;row++)
        {
            for(column=0;column<=12;column++)
            {
                if(Deck[row][column]==i){
                    flag=1;
                    break;
                }
            }
            if(flag==1) break;
        }
        printf("%s%2s\t",suit[row],value[column]);
        if(i%4==0) printf("\n");
    }

}


int main()
{
    char *suit[4]={"红桃","方片","草花","黑桃"};//指针数组
    char *value[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    int Deck[4][13]={0};//二维数组
    srand(time(NULL));


    shuffle(Deck);

    deal(Deck,suit,value);//发牌
    return 0;
}

第二个是我的想法:

#include <stdio.h>
void deal(char *suit[],char *value[]);
int main()
{
    char *suit[4]={"红桃","方片","草花","黑桃"};//指针数组
    char *value[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    srand(time(NULL));
    deal(suit,value);//发牌
    return 0;
}
void deal(char *suit[],char *value[])
{
    int i,column,row;
    int cun[52]={0},j,a,b;
    printf("玩家1\t玩家2\t玩家3\t玩家4\n");
    for(i=1;i<=52;i++)
    {
        j=rand() % 52;
        a=j/13;
        b=j%13;

        if(cun[j]==0)
        {
            cun[j]=60;  
            printf("%s%2s\t",suit[a],value[b]);
            if(i%4==0)
                printf("\n");
        }
        else
            i-=1;
    }

}

如有错误也请帮忙指正,谢谢

CYL070100
陈陈陈大老板 没有什么错误,但是调用函数的时候没有写上<time.h>和<stdlib.h>,还是谢谢大神!!
4 个月之前 回复
CYL070100
陈陈陈大老板 谢谢大神!!!
4 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐