|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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[0]); array是数组形式,但实际上是一个指针,这样无法计算数组的长度
for(int i =0;i<length;i++)
{
if (array[i] == 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[i] = number;
}
else
{
label = compare(total_card,number,i);
// printf("label = %d\n",label) ;
if (label == 0)
{
//printf("label = 0,未出现重复,程序继续") ;
total_card[i] = 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[i] = number;
}
}
}
return total_card;
}
void print_card(int i , char name[] ,int people[13],char class_card[][7])
{
// 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[i]);
// printf("in the function%d \n",people[i]);
}
for (int i = 0; i < card; i++)
{
if(people[i] < card - 1) //小于13
{
printf("<13 people[i] = %d\n",people[i]);
printf("%s%d ",class_card[0],i); //0~14给第一个字符
}
else if(card *2 > people[i] >= card)
{
printf("26> >13 people[i] = %d\n",people[i]);
printf("%s%d ",class_card[1],i);
}
else if(card *3 > people[i] >= card *2)
{
printf("39> >26 people[i] = %d\n",people[i]);
printf("%s%d ",class_card[2],i);
}
else if(52 > people[i] >= 39)
{
printf("52> >39 people[i] = %d\n",people[i]);
printf("%s%d ",class_card[3],i);
}
else if(people[i] == 53)
{
printf("%s ",class_card[4]);
}
else if(people[i] == 54)
{
printf("%s ",class_card[5]);
}
}
}
void get_name(char name[][64] , int num)
{
int i ;
for(i = 0 ; i < number_of_game ; i ++)
{
printf("enter %d member's name :",i) ;
fgets(name[i] , 64 , stdin) ;
}
}
int * distribution(int *array,int member)
{
int * someone ;
someone = (int *) malloc(sizeof(int) * 14) ;
for(int i = 0 ; i < card ; i ++)
{
someone[i] = array[card * (member - 1) + i] ;
}
return someone ;
}
int main()
{
//输入玩家的名子
int i;
char name[3][64] ;
get_name(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][4] = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"} ;
char class_card[][7] = {"方","梅","红","黑","大王","小王"};
//分配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[0],person1,class_card);
print_card(2,name[1],person2,class_card);
print_card(3,name[2],person3,class_card);
free(person1);
free(person2);
free(person3);
return 0;
}
我这里之前回复过的一洗牌、发牌程序,供你参考一下 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(int array[], int length);
void deal(int array[], int player[3][14]);
void show(char names[3][20], int player[3][14]);
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]; // 将原值(第 i 张牌)赋给临时变量
array[i] = array[index]; // 将第 index 张牌替换原值
array[index] = temp; // 将第 i 张牌换到第 index 张牌的位置
}
}
}
void deal(int array[], int player[3][14])
{
int i, j, k = 0;
// 模拟发牌:拿起一副无序的扑克每人轮流发一张牌
for (i = 0; i < 14; i++)
{
for (j = 0; j < 3; j++)
{
player[j][i] = array[k++];
}
}
}
void show(char names[3][20], int player[3][14])
{
int i, j, poker;
printf("\n方=方角,梅=梅花,红=红桃,黑=黑桃\n\n");
for (i = 0; i < 3; i++)
{
printf("%s手上的牌是:", names[i]);
for (j = 0; j < 14; j++)
{
poker = player[i][j];
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[54];
int player[3][14];
int i,ch;
char names[3][20];
// 初始化扑克牌
// 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] = i + 1;
}
for (i = 0; i < 3; i++)
{
printf("\n请输入%d号玩家的名字:", i+1);
scanf("%s",names[i]);
}
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;
}
|
|