c++ 洗牌发牌模拟 怎么排序输出?
输出按黑桃梅花方片红桃K~2没有大小鬼代码如下#include<stdio.h>#include<stdlib.h>#include<time.h>#include<st...
输出按 黑桃梅花方片红桃 K~2 没有大小鬼
代码如下
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD //牌
{
char suit[10]; //花色
char face[10]; //点数
};
enum player{ posA, posB, posC, posD};//定义好每个人的位置
struct Postion
{
struct CARD getcard[13];//每人获得的牌
};
struct Postion postion[4];//分配四个位置
struct CARD card[52]; //总共52张牌
char *suit[]={"黑桃","梅花","方片","红桃"};
char *face[]={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
//洗牌 打乱52张牌的顺序
void Shuffle(struct CARD *wCard) //数据体数组wCard,表示 52张牌
{
int i,j;
struct CARD temp; //CARD 与 temp 相等
for (i=0; i<52; i++)
{
j=rand()%52; //生成随机数
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
void Deal(struct CARD *wCard)
{
int i,pera=0,perb=0,perc=0,perd=0;
Shuffle(card); //将牌打乱
for (i=0; i<52; i++) //发牌数
{
//printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posA].getcard[pera++]=wCard[i];
else if(i%4==1)
postion[posB].getcard[perb++]=wCard[i];
else if(i%4==2)
postion[posC].getcard[perc++]=wCard[i];
else if(i%4==3)
postion[posD].getcard[perd++]=wCard[i];
}
}
/* 函数功能:将52张牌按黑桃、梅花、方片、红桃花色顺序,面值按A~2顺序排列
函数参数:结构体数组wCard,表示不同花色和面值的52张牌
函数返回值:无
*/
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i/13]); //指针数组wSuit,指向花色字符串
strcpy(wCard[i].face, wFace[i%13]); //指针数组wFace,指向面值字符串
}
} 展开
代码如下
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD //牌
{
char suit[10]; //花色
char face[10]; //点数
};
enum player{ posA, posB, posC, posD};//定义好每个人的位置
struct Postion
{
struct CARD getcard[13];//每人获得的牌
};
struct Postion postion[4];//分配四个位置
struct CARD card[52]; //总共52张牌
char *suit[]={"黑桃","梅花","方片","红桃"};
char *face[]={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
//洗牌 打乱52张牌的顺序
void Shuffle(struct CARD *wCard) //数据体数组wCard,表示 52张牌
{
int i,j;
struct CARD temp; //CARD 与 temp 相等
for (i=0; i<52; i++)
{
j=rand()%52; //生成随机数
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
void Deal(struct CARD *wCard)
{
int i,pera=0,perb=0,perc=0,perd=0;
Shuffle(card); //将牌打乱
for (i=0; i<52; i++) //发牌数
{
//printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posA].getcard[pera++]=wCard[i];
else if(i%4==1)
postion[posB].getcard[perb++]=wCard[i];
else if(i%4==2)
postion[posC].getcard[perc++]=wCard[i];
else if(i%4==3)
postion[posD].getcard[perd++]=wCard[i];
}
}
/* 函数功能:将52张牌按黑桃、梅花、方片、红桃花色顺序,面值按A~2顺序排列
函数参数:结构体数组wCard,表示不同花色和面值的52张牌
函数返回值:无
*/
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i/13]); //指针数组wSuit,指向花色字符串
strcpy(wCard[i].face, wFace[i%13]); //指针数组wFace,指向面值字符串
}
} 展开
1个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD // 牌
{
char suit[10]; // 花色
char face[10]; // 点数
char pos; // 牌的顺序, 用来排序输出
};
enum player{ posA, posB, posC, posD}; // 定义好每个人的位置
struct Postion
{
struct CARD getcard[13]; // 每人获得的牌
char num; // 剩余牌数量
};
struct Postion postion[4]; // 分配四个位置
struct CARD card[52]; // 总共52张牌
char * suit[]= {"黑桃","梅花","方片","红桃"};
char * face[]= {"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
// 洗牌 打乱52张牌的顺序
void Shuffle(struct CARD * wCard) // 数据体数组wCard,表示 52张牌
{
int i,j;
struct CARD temp; // CARD 与 temp 相等
for (i=0; i<52; i++)
{
j = rand() % 52; // 生成随机数
temp = wCard[i];
wCard[i] = wCard[j];
wCard[j] = temp;
}
}
void Deal(struct CARD *wCard)
{
int i, pera=0, perb=0, perc=0, perd=0;
Shuffle(card); //将牌打乱
for (i=0; i<52; i++) //发牌数
{
//printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if (i % 4 == 0) postion[posA].getcard[pera++] = wCard[i];
else if(i % 4 == 1) postion[posB].getcard[perb++] = wCard[i];
else if(i % 4 == 2) postion[posC].getcard[perc++] = wCard[i];
else if(i % 4 == 3) postion[posD].getcard[perd++] = wCard[i];
}
postion[posA].num = 13;
postion[posB].num = 13;
postion[posC].num = 13;
postion[posD].num = 13;
}
void Sort() // 对发的牌进行排序
{
int i, j, k, pos, m;
struct CARD temp; // CARD 与 temp 相等
for(i=0; i<4; i++)
{
switch(i)
{
case 0: m = posA; break;
case 1: m = posB; break;
case 2: m = posC; break;
case 3: m = posD; break;
default: ;
}
for(j=0; j<13; j++)
{
pos = j;
for(k=j+1; k<13; k++)
if(postion[m].getcard[k].pos > postion[m].getcard[pos].pos) pos = k;
if(pos != j)
{
temp = postion[m].getcard[pos];
postion[m].getcard[pos] = postion[m].getcard[j];
postion[m].getcard[j] = temp;
}
}
}
}
void Display()
{
int i, j, m;
system("cls");
for(i=0; i<4; i++)
{
switch(i)
{
case 0: m = posA; printf("\nPlayer A Have %d Cards: \n----------\n", postion[m].num); break;
case 1: m = posB; printf("\nPlayer B Have %d Cards: \n----------\n", postion[m].num); break;
case 2: m = posC; printf("\nPlayer C Have %d Cards: \n----------\n", postion[m].num); break;
case 3: m = posD; printf("\nPlayer D Have %d Cards: \n----------\n", postion[m].num); break;
default: ;
}
for(j=0; j < postion[m].num; j++) printf("%s%s ", postion[m].getcard[j].suit, postion[m].getcard[j].face);
printf("\n");
}
}
// 函数功能:将52张牌按黑桃、梅花、方片、红桃花色顺序,面值按A~2顺序排列
// 函数参数:结构体数组wCard,表示不同花色和面值的52张牌
// 函数返回值:无
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i / 13]); //指针数组wSuit,指向花色字符串
strcpy(wCard[i].face, wFace[i % 13]); //指针数组wFace,指向面值字符串
wCard[i].pos = i;
}
}
void main()
{
srand(time(NULL));
FillCard(card, suit, face);
Shuffle(card);
while(1)
{
Deal(card);
Sort();
Display();
if(getchar() == 'Q') break;
}
}
追问
我想问一下 输出牌 顺序在哪里改 我想 把顺序改成 AKQJ1098765432
追答
Sort()中
if(postion[m].getcard[k].pos > postion[m].getcard[pos].pos) pos = k;
改成
if(postion[m].getcard[k].pos < postion[m].getcard[pos].pos) pos = k;
就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询