洗牌程序,打印纸牌名字永远只有第一个花色‘方’,什么情况
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <time.h>
//**************************************************************************
//实现一个洗牌程序。所谓洗牌程序,就是将所有的扑克牌放入一个数组中,
//要求把数组内的扑克牌顺序随机打乱,然后输出
//每个人获得14张牌 ,我分牌的思路是牌在数组里面随机生成0~54的不重复的54个数,规定自然数0~12为 {"方","梅","红","黑","大王","小王"}里面的方,
//13-26w为梅有,依次类推,最后剩余的52,53两个数分别对应大小王,对i循环从1-14,依次对应纸牌里面的1-13,然后成对输出
int number_of_game = 3;
int card = 13;
//int print_card(int i,char name);
//int distribution(int array,int member);
//char **get_name(int num);
//***********************生成54个0~54的整数,乱序***********************************
//确保新生成的数组里面没有
//返回1表示这个数在数组里面已经存在了
int compare(int array[],int number,int length)
{
int i;
int sign = 0;
// length = sizeof(array) / sizeof(array); array是数组形式,但实际上是一个指针,这样无法计算数组的长度
for(int i =0;i<length;i++)
{
if (array == number)
{
sign = 1;
break;
}
}
return sign;
}
int *get_54_number_no_repeat(int num)
{
int *total_card;
int i,number,label;
total_card = (int *) malloc(sizeof(int) * num);
for(int i =0;i<num;i++)
{
number = rand() % 54;
if (i == 0)
{
total_card = number;
}
else
{
label = compare(total_card,number,i);
// printf("label = %d\n",label) ;
if (label == 0)
{
//printf("label = 0,未出现重复,程序继续") ;
total_card = number;
}
else
{
//printf("label = 1,出现重复,重新获取随机数") ;
do
{
number = rand() % 54;
label = compare(total_card,number,i);
//printf("label = %d\n",label) ;
}while(label == 1);
total_card = number;
}
}
}
return total_card;
}
void print_card(int i , char name[] ,int people,char class_card[])
{
// printf("**************%d*************\n",i);
int card = 13;
printf("the card of %s is:\n", name);
for(int i =0;i<13;i++)
{
printf("in the function%s \n",class_card);
// printf("in the function%d \n",people);
}
for (int i = 0; i < card; i++)
{
if(people < card - 1)//小于13
{
printf("<13 people = %d\n",people);
printf("%s%d ",class_card,i); //0~14给第一个字符
}
else if(card *2 > people >= card)
{
printf("26> >13 people = %d\n",people);
printf("%s%d ",class_card,i);
}
else if(card *3> people >= card *2)
{
printf("39> >26 people = %d\n",people);
printf("%s%d ",class_card,i);
}
else if(52 > people >= 39)
{
printf("52> >39 people = %d\n",people);
printf("%s%d ",class_card,i);
}
else if(people == 53)
{
printf("%s ",class_card);
}
else if(people == 54)
{
printf("%s ",class_card);
}
}
}
void get_name(char name[] , int num)
{
int i ;
for(i = 0 ; i < number_of_game ; i ++)
{
printf("enter %d member's name :",i) ;
fgets(name , 64 , stdin) ;
}
}
int * distribution(int *array,int member)
{
int * someone ;
someone = (int *) malloc(sizeof(int) * 14) ;
for(int i = 0 ; i < card ; i ++)
{
someone = array ;
}
return someone ;
}
int main()
{
//输入玩家的名子
int i;
char name ;
get_name(name , number_of_game) ;
// for(i = 0; i < number_of_game; i++)
// {
// printf("the %d member's name is: %s'",i,name);
// }
//定义纸牌
//char *num = {'J','Q','K'};
char num = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"} ;
char class_card[] = {"方","梅","红","黑","大王","小王"};
//分配54张牌
int *person1,*person2,*person3;
int *people;
people = get_54_number_no_repeat(54);
person1 = distribution(people,1);
person2 = distribution(people,2);
person3 = distribution(people,3);
//打印3个人分到的牌
print_card(1,name,person1,class_card);
print_card(2,name,person2,class_card);
print_card(3,name,person3,class_card);
free(person1);
free(person2);
free(person3);
return 0;
} 这个应该是生成的只有伪随机数,首次编译得到的字节码完全文件决定了后面会出现的随机数 倒戈卸甲 发表于 2020-11-26 20:54
这个应该是生成的只有伪随机数,首次编译得到的字节码完全文件决定了后面会出现的随机数
我打印传进print_card里面的数组,确实是54个不同的随机数, 我这里之前回复过的一洗牌、发牌程序,供你参考一下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(int array[], int length);
void deal(int array[], int player);
void show(char names, int player);
void shuffle(int array[], int length) // 调用函数 shuffle 进行洗牌
{
int index, temp, i;
srand(time(NULL)); // 使用当前时间进行随机数发生器的初始化
for (i = 0; i < length; i++)
{
index = rand() % (length - i + 1) + i; // 产生一个随机数
if (index != i) // 如果 index 不等于 i,这是为了充分打乱 array 中的原值,即109行所赋的值
{
temp = array; // 将原值(第 i 张牌)赋给临时变量
array = array; // 将第 index 张牌替换原值
array = temp; // 将第 i 张牌换到第 index 张牌的位置
}
}
}
void deal(int array[], int player)
{
int i, j, k = 0;
// 模拟发牌:拿起一副无序的扑克每人轮流发一张牌
for (i = 0; i < 14; i++)
{
for (j = 0; j < 3; j++)
{
player = array;
}
}
}
void show(char names, int player)
{
int i, j, poker;
printf("\n方=方角,梅=梅花,红=红桃,黑=黑桃\n\n");
for (i = 0; i < 3; i++)
{
printf("%s手上的牌是:", names);
for (j = 0; j < 14; j++)
{
poker = player;
if (poker < 11 && 0 < poker)
{
printf("方%d ", poker);
}
else if (poker < 21 && 10 < poker)
{
printf("梅%d ", poker-10);
}
else if (poker < 31 && 20 < poker)
{
printf("红%d ", poker-20);
}
else if (poker < 41 && 30 < poker)
{
printf("黑%d ", poker-30);
}
else
{
switch (poker)
{
case 41: printf("方J "); break;
case 42: printf("方Q "); break;
case 43: printf("方K "); break;
case 44: printf("梅J "); break;
case 45: printf("梅Q "); break;
case 46: printf("梅K "); break;
case 47: printf("红J "); break;
case 48: printf("红Q "); break;
case 49: printf("红K "); break;
case 50: printf("黑J "); break;
case 51: printf("黑Q "); break;
case 52: printf("黑K "); break;
case 53: printf("小王 "); break;
case 54: printf("大王 "); break;
}
}
}
printf("\n\n");
}
}
int main(void)
{
int array;
int player;
int i,ch;
char names;
// 初始化扑克牌
// 1~10代表方(角)1~10, 41、42、43代表方(角)J、Q、K
// 11~20代表梅(花)1~10, 44、45、46代表梅(花)J、Q、K
// 21~30代表红(桃)1~10, 47、48、49代表红(桃)J、Q、K
// 31~40代表黑(桃)1~10, 50、51、52代表黑(桃)J、Q、K
// 53、54当然就代表小王大王啦~
for (i = 0; i < 54; i++)
{
array = i + 1;
}
for (i = 0; i < 3; i++)
{
printf("\n请输入%d号玩家的名字:", i+1);
scanf("%s",names);
}
do
{
shuffle(array, 54);
deal(array, player);
show(names, player);
printf("重新洗牌(Y/N)?");
do
{
ch = getchar(); // 过滤输入缓冲区的其他字符
} while (ch != 'Y' && ch != 'N');
} while (ch == 'Y');
return 0;
}
页:
[1]