#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);
int print_card(int i,char name,int people,char class_card)
{
printf("**************%d*************\n",i);
printf("the card of %s is:\n", name);
for (int i =1; i < card+1; i++)
{
if (people[i] < card-1) //小于13
{
printf("%s%d ",class_card[0],i); //0~14给第一个字符
}
else if(card *2-1 > people[i] >= card)
{
printf("%s%d ",class_card[1],i);
}
else if(card *3 - 1 > people[i] >= card *2)
{
printf("%s%d ",class_card[2],i);
}
else if(52 > people[i] >= 39)
{
printf("%s%d ",class_card[3],i);
}
else if(people[i] == 53)
{
printf("%s ",class_card[4]);
}
else
{
printf("%s ",class_card[5]);
}
}
}
char **get_name(int num)
{
char **ptr = NULL;
int i;
ptr = (char **)malloc(num *sizeof(char*)); //*name[3];这种需要用到动态存储
for(i = 0;i <num;i++)
{
ptr[i] = (char*)malloc(100*sizeof(char));
printf("enter %d member's name :",i);
fgets(ptr[i],100,stdin);
// printf("the member's name is: %s'",ptr[i]);
}
return ptr;
}
int *distribution(int *array,int member)
{
static int someone[14];
for(int i =0; i<card; i++)
{
someone[i] = array[card * (member-1) + i];
}
return someone;
}
int main()
{
//输入玩家的名子
int i;
char **name = NULL;
name = get_name(number_of_game);
for(i = 0; i < number_of_game; i++)
{
printf("the %d member's name is: %s'",i,name[i]);
}
//定义纸牌
//char *num[13] = {'J','Q','K'};
char num[13] = {'1','2','3','4','5','6','7','8','9','10','J','Q','K'};
char *class_card[7] = {"方","梅","红","黑","大王","小王"};
srand(time(NULL));
// printf("fang = %s\n",class_card[0]);
//产生54乱序的数,0-30,+大小王 = 42,42/3 = 14
for (int i = 0; i < 54; i++)
{
people[i] = rand()%54;
}
printf("\n");
//分配54张牌
int people[54],*person1,*person2,*person3;
for(int i =0;i<54;i++)
{
people[i] = rand() % 54;
}
person1 = distribution(people,1);
person2 = distribution(people,2);
person3 = distribution(people,3);
// for(int i =0;i<14;i++)
// {
// printf("%d ",person1[i]);
// }
//打印3个人分到的牌
print_card(1,name[0],*person1,class_card);
print_card(2,name[1],*person2,class_card);
print_card(3,name[2],*person3,class_card);
free(name);
free(person1);
free(person2);
free(person3);
return 0;
}