求一个模拟人工洗牌的程序,用c++编写,谢谢!!!
编写一个模拟人工洗牌的程序,将洗好的牌分别发给四个人。使用结构card来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。对每个人的牌要...
编写一个模拟人工洗牌的程序,将洗好的牌分别发给四个人。
使用结构card 来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。
对每个人的牌要按桥牌的规则输出。即一个人的牌要先按牌的花色(顺序为梅花、方块、红心和黑桃)进行分类,同一类的牌要再按A、K、Q、J、…、3、2牌的大小顺序排列。另发牌应按四个人的顺序依次分发。 展开
使用结构card 来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。
对每个人的牌要按桥牌的规则输出。即一个人的牌要先按牌的花色(顺序为梅花、方块、红心和黑桃)进行分类,同一类的牌要再按A、K、Q、J、…、3、2牌的大小顺序排列。另发牌应按四个人的顺序依次分发。 展开
3个回答
展开全部
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
struct tagCard{
int nSide;//花色,0-3分别是梅花、方块、红心和黑桃
int nValue;//牌面,2 3 4 5 6 7 8 9 10 11(J) 12(Q) 13(K) 14(A)
};
//排序回调函数
bool UDgreater ( tagCard elem1, tagCard elem2 )
{
if (elem1.nSide < elem2.nSide)
return true;
else if (elem1.nSide>elem2.nSide)
return false;
else
return elem1.nValue>elem2.nValue?true:false;
}
int main(int args,char *argv[])
{
//总共52张牌,1-52,产生1-52的乱序
const char * pstrSide[] = {"梅花","方块","红心","黑桃"};
const char * pstrVal[] = {"0","0","2","3","4","5","6","7","8","9","10","J","Q","K","A"};
std::vector<tagCard> card;
std::vector<tagCard>::iterator it_Users[4][2];
int i;
//乱序
srand((unsigned int)time(0));
for (i=0;i<52;i++)
{
tagCard item;
item.nSide = i/13;
item.nValue = i%13+2;
card.push_back(item);
}
//洗牌,默认1000次
for (i=0;i<1000;i++)
{
int p = rand()%52;
int q = rand()%52;
tagCard tmp = card[p];
card[p] = card[q];
card[q] = tmp;
}
//发牌已经在洗牌同时完成了,洗好的0-51号位置,0-12是用户1,13-25为用户2, 26-38是用户3 39-51是用户4
std::vector<tagCard>::iterator it = card.begin();
//排序
for (int i=0;i<52;i++)
{
if (i%13==0)
{
it_Users[i/13][0] = it;
if (i>12)
it_Users[i/13-1][1] = it;
}
it++;
}
it_Users[3][1] = card.end();
sort(it_Users[0][0],it_Users[0][1],UDgreater);//用户1
sort(it_Users[1][0],it_Users[1][1],UDgreater);//用户2
sort(it_Users[2][0],it_Users[2][1],UDgreater);//用户3
sort(it_Users[3][0],it_Users[3][1],UDgreater);//用户4
//打印
printf("用户1\t用户2\t用户3\t用户4\n");
for (i=0;i<13;i++)
{
printf("%2s%2s\t%2s%2s\t%2s%2s\t%2s%2s\n",
pstrSide[card[i].nSide],pstrVal[card[i].nValue],
pstrSide[card[i+13].nSide],pstrVal[card[i+13].nValue],
pstrSide[card[i+13*2].nSide],pstrVal[card[i+13*2].nValue],
pstrSide[card[i+13*3].nSide],pstrVal[card[i+13*3].nValue]);
}
return 0;
}
VC2005/2008下控制台程序编译,
运行结果
用户1 用户2 用户3 用户4
梅花 A 梅花 K 梅花 3 梅花 Q
梅花 J 梅花10 方块 J 梅花 7
梅花 9 梅花 2 方块10 梅花 6
梅花 8 方块 4 方块 9 梅花 5
方块 K 方块 3 方块 8 梅花 4
方块 Q 方块 2 方块 6 方块 A
方块 7 红心 K 方块 5 红心 Q
红心 J 红心10 红心 A 红心 5
红心 4 红心 2 红心 9 黑桃 K
红心 3 黑桃 Q 红心 8 黑桃10
黑桃 A 黑桃 J 红心 7 黑桃 7
黑桃 8 黑桃 9 红心 6 黑桃 4
黑桃 6 黑桃 3 黑桃 5 黑桃 2
请按任意键继续. . .
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
struct tagCard{
int nSide;//花色,0-3分别是梅花、方块、红心和黑桃
int nValue;//牌面,2 3 4 5 6 7 8 9 10 11(J) 12(Q) 13(K) 14(A)
};
//排序回调函数
bool UDgreater ( tagCard elem1, tagCard elem2 )
{
if (elem1.nSide < elem2.nSide)
return true;
else if (elem1.nSide>elem2.nSide)
return false;
else
return elem1.nValue>elem2.nValue?true:false;
}
int main(int args,char *argv[])
{
//总共52张牌,1-52,产生1-52的乱序
const char * pstrSide[] = {"梅花","方块","红心","黑桃"};
const char * pstrVal[] = {"0","0","2","3","4","5","6","7","8","9","10","J","Q","K","A"};
std::vector<tagCard> card;
std::vector<tagCard>::iterator it_Users[4][2];
int i;
//乱序
srand((unsigned int)time(0));
for (i=0;i<52;i++)
{
tagCard item;
item.nSide = i/13;
item.nValue = i%13+2;
card.push_back(item);
}
//洗牌,默认1000次
for (i=0;i<1000;i++)
{
int p = rand()%52;
int q = rand()%52;
tagCard tmp = card[p];
card[p] = card[q];
card[q] = tmp;
}
//发牌已经在洗牌同时完成了,洗好的0-51号位置,0-12是用户1,13-25为用户2, 26-38是用户3 39-51是用户4
std::vector<tagCard>::iterator it = card.begin();
//排序
for (int i=0;i<52;i++)
{
if (i%13==0)
{
it_Users[i/13][0] = it;
if (i>12)
it_Users[i/13-1][1] = it;
}
it++;
}
it_Users[3][1] = card.end();
sort(it_Users[0][0],it_Users[0][1],UDgreater);//用户1
sort(it_Users[1][0],it_Users[1][1],UDgreater);//用户2
sort(it_Users[2][0],it_Users[2][1],UDgreater);//用户3
sort(it_Users[3][0],it_Users[3][1],UDgreater);//用户4
//打印
printf("用户1\t用户2\t用户3\t用户4\n");
for (i=0;i<13;i++)
{
printf("%2s%2s\t%2s%2s\t%2s%2s\t%2s%2s\n",
pstrSide[card[i].nSide],pstrVal[card[i].nValue],
pstrSide[card[i+13].nSide],pstrVal[card[i+13].nValue],
pstrSide[card[i+13*2].nSide],pstrVal[card[i+13*2].nValue],
pstrSide[card[i+13*3].nSide],pstrVal[card[i+13*3].nValue]);
}
return 0;
}
VC2005/2008下控制台程序编译,
运行结果
用户1 用户2 用户3 用户4
梅花 A 梅花 K 梅花 3 梅花 Q
梅花 J 梅花10 方块 J 梅花 7
梅花 9 梅花 2 方块10 梅花 6
梅花 8 方块 4 方块 9 梅花 5
方块 K 方块 3 方块 8 梅花 4
方块 Q 方块 2 方块 6 方块 A
方块 7 红心 K 方块 5 红心 Q
红心 J 红心10 红心 A 红心 5
红心 4 红心 2 红心 9 黑桃 K
红心 3 黑桃 Q 红心 8 黑桃10
黑桃 A 黑桃 J 红心 7 黑桃 7
黑桃 8 黑桃 9 红心 6 黑桃 4
黑桃 6 黑桃 3 黑桃 5 黑桃 2
请按任意键继续. . .
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card {
int cdhs; //1,2,3,4
int point; //1,2,..13;
} CARD;
CARD cards[52];
void main()
{
int i,j,k,n;
int x[52],y;
int flag[52];
char p[13][5]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
char h[4][5] = {"S","H","D","C"};
for (j=0;j<4;j++){
for (i=0;i<13;i++){
cards[j*13+i].cdhs= j %4 ;
cards[j*13+i].point= i %13;
}};
for (i=0;i<52;i++) flag[i]= -1;
srand((unsigned)time(NULL));
n=0;
while(1) {
y = (int) ( (float)rand() / ((float)(RAND_MAX)) * 53.0);
if (y < 52 && y >= 0) { if ( flag[y] == -1)
{ x[n]=y; flag[y]=1;n++; if (n>=52)break; };
}; // end if
};
for (k=0;k<4;k++)
for (i=0;i<12;i++)
for(j=i;j<13;j++){
if (x[i+k*13]< x[j+k*13]){y=x[i+k*13];x[i+k*13]=x[j+k*13];x[j+k*13]=y;};
}
printf("Person-1: ");
for (i=0;i<13;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-2: ");
for (i=13;i<26;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-3: ");
for (i=26;i<39;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-4: ");
for (i=39;i<52;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\n");
system("pause");
}
#include <stdlib.h>
#include <time.h>
typedef struct card {
int cdhs; //1,2,3,4
int point; //1,2,..13;
} CARD;
CARD cards[52];
void main()
{
int i,j,k,n;
int x[52],y;
int flag[52];
char p[13][5]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
char h[4][5] = {"S","H","D","C"};
for (j=0;j<4;j++){
for (i=0;i<13;i++){
cards[j*13+i].cdhs= j %4 ;
cards[j*13+i].point= i %13;
}};
for (i=0;i<52;i++) flag[i]= -1;
srand((unsigned)time(NULL));
n=0;
while(1) {
y = (int) ( (float)rand() / ((float)(RAND_MAX)) * 53.0);
if (y < 52 && y >= 0) { if ( flag[y] == -1)
{ x[n]=y; flag[y]=1;n++; if (n>=52)break; };
}; // end if
};
for (k=0;k<4;k++)
for (i=0;i<12;i++)
for(j=i;j<13;j++){
if (x[i+k*13]< x[j+k*13]){y=x[i+k*13];x[i+k*13]=x[j+k*13];x[j+k*13]=y;};
}
printf("Person-1: ");
for (i=0;i<13;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-2: ");
for (i=13;i<26;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-3: ");
for (i=26;i<39;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-4: ");
for (i=39;i<52;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\n");
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <memory.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int NUM = 52;
const int DECK = 13;
enum SortType {SUIT, VALUE};
typedef struct CARD
{
int suit; // 取值0 - 3,分别表示梅花、方块、红心和黑桃
int value; // 取值2 - 10、J(11)、Q(12)、K(13)、A(14)
} Card;
void suffle(Card *cards)
{
int i, j, t;
Card c;
srand(time(NULL));
for (i = 0; i < NUM; ++i)
for (j = 0; j < NUM; ++j)
{
t = rand() % NUM;
if (j != t)
{
memcpy(&c, &cards[t], sizeof(Card));
memcpy(&cards[t], &cards[j], sizeof(Card));
memcpy(&cards[j], &c, sizeof(Card));
}
}
}
void sort(Card *cards, int size, SortType stype)
{
int i, j, k;
Card t;
for (i = 0; i < size-1; ++i)
{
k = i;
for (j = i + 1; j < size; ++j)
{
if ((stype == SUIT) && (cards[k].suit > cards[j].suit))
k = j;
else if ((stype == VALUE) && cards[k].value < cards[j].value)
k = j;
}
if (k != i)
{
memcpy(&t, &cards[i], sizeof(Card));
memcpy(&cards[i], &cards[k], sizeof(Card));
memcpy(&cards[k], &t, sizeof(Card));
}
}
}
void show(int suit, int value)
{
static char values[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
switch (suit)
{
case 0:
cout << "梅花 ";
break;
case 1:
cout << "方块 ";
break;
case 2:
cout << "红心 ";
break;
case 3:
cout << "黑桃 ";
break;
}
if (value == 10)
cout << "10";
else
cout << values[value-2];
cout << "\t\t";
}
void sortValue(Card *cards)
{
int p[4] = {0, 0, 0, 0};
int i, j;
for (i = 0; i < DECK; ++i)
++p[cards[i].suit];
j = 0;
for (i = 0; i < 4; ++i)
{
sort(cards + j, p[i], VALUE);
j += p[i];
}
}
void main(void)
{
Card cards[NUM];
Card A[DECK], B[DECK], C[DECK], D[DECK];
int i, j, s = 0, v = 2;
for (i = 0; i < NUM; ++i)
{
cards[i].suit = s;
cards[i].value = v;
++v;
if ((i+1) % 13 == 0)
{
++s;
v = 2;
}
}
suffle(cards);
j = 0;
for (i = 0; i < NUM; i += 4)
{
memcpy(&A[j], &cards[i], sizeof(Card));
memcpy(&B[j], &cards[i+1], sizeof(Card));
memcpy(&C[j], &cards[i+2], sizeof(Card));
memcpy(&D[j], &cards[i+3], sizeof(Card));
++j;
}
sort(A, DECK, SUIT);
sortValue(A);
sort(B, DECK, SUIT);
sortValue(B);
sort(C, DECK, SUIT);
sortValue(C);
sort(D, DECK, SUIT);
sortValue(D);
cout << "PLAYER 1" << "\t" << "PLAYER 2" << "\t";
cout << "PLAYER 3" << "\t" << "PLAYER 4" << endl;
for (i = 0; i < DECK; ++i)
{
show(A[i].suit, A[i].value);
show(B[i].suit, B[i].value);
show(C[i].suit, C[i].value);
show(D[i].suit, D[i].value);
cout << endl;
}
system("PAUSE");
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int NUM = 52;
const int DECK = 13;
enum SortType {SUIT, VALUE};
typedef struct CARD
{
int suit; // 取值0 - 3,分别表示梅花、方块、红心和黑桃
int value; // 取值2 - 10、J(11)、Q(12)、K(13)、A(14)
} Card;
void suffle(Card *cards)
{
int i, j, t;
Card c;
srand(time(NULL));
for (i = 0; i < NUM; ++i)
for (j = 0; j < NUM; ++j)
{
t = rand() % NUM;
if (j != t)
{
memcpy(&c, &cards[t], sizeof(Card));
memcpy(&cards[t], &cards[j], sizeof(Card));
memcpy(&cards[j], &c, sizeof(Card));
}
}
}
void sort(Card *cards, int size, SortType stype)
{
int i, j, k;
Card t;
for (i = 0; i < size-1; ++i)
{
k = i;
for (j = i + 1; j < size; ++j)
{
if ((stype == SUIT) && (cards[k].suit > cards[j].suit))
k = j;
else if ((stype == VALUE) && cards[k].value < cards[j].value)
k = j;
}
if (k != i)
{
memcpy(&t, &cards[i], sizeof(Card));
memcpy(&cards[i], &cards[k], sizeof(Card));
memcpy(&cards[k], &t, sizeof(Card));
}
}
}
void show(int suit, int value)
{
static char values[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
switch (suit)
{
case 0:
cout << "梅花 ";
break;
case 1:
cout << "方块 ";
break;
case 2:
cout << "红心 ";
break;
case 3:
cout << "黑桃 ";
break;
}
if (value == 10)
cout << "10";
else
cout << values[value-2];
cout << "\t\t";
}
void sortValue(Card *cards)
{
int p[4] = {0, 0, 0, 0};
int i, j;
for (i = 0; i < DECK; ++i)
++p[cards[i].suit];
j = 0;
for (i = 0; i < 4; ++i)
{
sort(cards + j, p[i], VALUE);
j += p[i];
}
}
void main(void)
{
Card cards[NUM];
Card A[DECK], B[DECK], C[DECK], D[DECK];
int i, j, s = 0, v = 2;
for (i = 0; i < NUM; ++i)
{
cards[i].suit = s;
cards[i].value = v;
++v;
if ((i+1) % 13 == 0)
{
++s;
v = 2;
}
}
suffle(cards);
j = 0;
for (i = 0; i < NUM; i += 4)
{
memcpy(&A[j], &cards[i], sizeof(Card));
memcpy(&B[j], &cards[i+1], sizeof(Card));
memcpy(&C[j], &cards[i+2], sizeof(Card));
memcpy(&D[j], &cards[i+3], sizeof(Card));
++j;
}
sort(A, DECK, SUIT);
sortValue(A);
sort(B, DECK, SUIT);
sortValue(B);
sort(C, DECK, SUIT);
sortValue(C);
sort(D, DECK, SUIT);
sortValue(D);
cout << "PLAYER 1" << "\t" << "PLAYER 2" << "\t";
cout << "PLAYER 3" << "\t" << "PLAYER 4" << endl;
for (i = 0; i < DECK; ++i)
{
show(A[i].suit, A[i].value);
show(B[i].suit, B[i].value);
show(C[i].suit, C[i].value);
show(D[i].suit, D[i].value);
cout << endl;
}
system("PAUSE");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询