|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 顶级太阳 于 2022-12-25 20:59 编辑
s1e31动动手题目:
实现一个洗牌程序。所谓洗牌程序,就是将所有的扑克牌放入一个数组中,要求把数组内的扑克牌顺序随机打乱,然后输出。
要求实现的输出内容如下:
我写的代码如下://洗牌、发牌、显示分模块
#include<stdio.h>
#include<stdlib.h>
//洗牌:在54个元素的全为0数组里填入1-54的数字
void shuffle(int poker[])
{
int i,j,m;
printf("洗牌进行中。。。"); //测试程度进度用,临时测试用,现有程序没有运行到这一行。why?
for(i=0;i<54;i++)
{
pos_1: j=(rand()*54)/1;
for(m=0;m<i;m++)
{
if(j=poker[m])goto pos_1; //与已有值遍历进行比较,如果已经有填入,重新抽取随机数
}
poker[i]=j;
printf("%d",i+1); //显示进行到第几张扑克 ,临时测试用
}
}
//发牌deal:轮流将poket交给不同player的分发
void deal(int poket[],int player1[],int player2[],int player3[])
{
int i,j;
for(i=0;i<52;i++)
{
j=0;
switch(i%3)
{
case 0: player1[i]=poket[i];break;
case 1: player2[i]=poket[i];break;
case 2: player3[i]=poket[i];j++; break;
}
}
}
//显示show:显示每个人手里的扑克内容
void show(char name,int player[])
{
char poket[54][10]={"方1","方2","方3","方4","方5","方6","方7","方8","方9","方10","方J","方Q","方K","梅1","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10","梅J","梅Q","梅K","红1","红2","红3","红4","红5","红6","红7","红8","红9","红10","红J","红","红K","黑1","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10","黑J","黑Q","黑K","小王","大王"};
printf("%s手上的牌是:",&name);
for(int i=0;i<14;i++)
{
printf("%s",poket[player[i]]);
}
printf("\n");
}
int main()
{
int player1[14],player2[14],player3[14];
char name1,name2,name3,yorn;
int poker[54]={0};
printf("请输入1号玩家的名字:");
scanf("%s",&name1);
printf("请输入2号玩家的名字:");
scanf("%s",&name2);
printf("请输入3号玩家的名字:");
scanf("%s",&name3);
step: printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n"); //现在程序运行到了这,往下的洗牌函数不运行了,为什么?
shuffle(poker); //洗牌
deal(poker,player1,player2,player3); //发牌
show(name1,player1);//亮牌
show(name2,player2); //亮牌
show(name3,player3); //亮牌
printf("重新洗牌(Y/N)?");
if(getchar()=='Y') goto step;
return 0;
}
通过改错,已经编译成功,不报错了。然后尝试运行,现在屏幕显示运行到了主程序step这一步了,按照程序应该开始洗牌函数了。为了查看是不是开始洗牌,我在函数的第一行进行了输出,但是运行的时候没有出现这一个输出。运行程序长时间不退出,也没有反映。请教,这是什么原因,我错在哪里?
现在这个程序已经可以完全正常运行了: //洗牌、发牌、显示分模块
#include<stdio.h>
#include<stdlib.h>
//洗牌:在54个元素的全为0数组里填入1-54的数字
void shuffle(int poker[])
{
int i,j,m;
for (i=0;i<54;++i) //改了算法,我们应该要有初始的数组
poker[i]=i;
printf("洗牌进行中。。。\n"); //测试程度进度用,临时测试用,现有程序没有运行到这一行。why?
for(i=1;i<54;i++) //这种洗牌算法真的很简单,从前往后,随机选数,当前位置和随机选到的位置互换就好
{
j=rand()%i;
m=poker[i];
poker[i]=poker[j];
poker[j]=m;
}
}
//发牌deal:轮流将poket交给不同player的分发
void deal(int poket[],int player1[],int player2[],int player3[])
{
int i,j,p1=0,p2=0,p3=0; //p1p2p3分别发了多少牌,你这样做有问题
for(i=0;i<42;i++) //每人到底几张?
{
j=0;
switch(i%3)
{
case 0: player1[p1++]=poket[i];break;
case 1: player2[p2++]=poket[i];break;
case 2: player3[p3++]=poket[i];j++; break;
}
}
}
//显示show:显示每个人手里的扑克内容
void show(char name[],int player[]) //name应为数组
{
const char *poket[54]={"方1","方2","方3","方4","方5","方6","方7","方8","方9","方10","方J","方Q","方K","梅1","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10","梅J","梅Q","梅K","红1","红2","红3","红4","红5","红6","红7","红8","红9","红10","红J","红","红K","黑1","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10","黑J","黑Q","黑K","小王","大王"};
printf("%s手上的牌是:",name); //不需要 &
for(int i=0;i<14;i++)
{
printf("%s ",poket[player[i]]);
}
printf("\n");
}
int main()
{
int player1[14],player2[14],player3[14];
char name1[15]={},name2[15]={},name3[15]={},yorn; //这里
int poker[54]={0};
printf("请输入1号玩家的名字:");
scanf("%s",&name1);
printf("请输入2号玩家的名字:");
scanf("%s",&name2);
printf("请输入3号玩家的名字:");
scanf("%s",&name3);
step: printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n"); //现在程序运行到了这,往下的洗牌函数不运行了,为什么?
shuffle(poker); //洗牌
deal(poker,player1,player2,player3); //发牌
show(name1,player1); //亮牌
show(name2,player2); //亮牌
show(name3,player3); //亮牌
printf("重新洗牌(Y/N)?");
getchar(); //过滤 \n
if(getchar()=='Y') goto step;
return 0;
}
|
|