c/c++实现模拟洗牌发牌

学习了博主nkmnkm的实现, http://blog.csdn.net/niu_gao/article/details/51458721,自己重写了一下
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;
enum Suit{
	 heart,
    spade,
    diamond,
    club,
    joker1,
    joker2
};
#define CARD_COUNT 54

typedef struct Card//一副牌有花色和大小
{
	int value;
	enum Suit suit;
}Card;

typedef struct Player
{
	char name[64];
	Card* cards;//因为玩家数量不确定,所以在这里没有分配确定的内存,而是一个指针
	int cardsCount;
}Player;

typedef int (* COMPAER)(Card *,Card *);//定义函数指针

char* getCardName(const Card *);
int shuffle(Card *cards,Card** p_card);
void dispatchCards(Player *player,int number,Card* card);
void sort(Card**,int,COMPAER);
int compare1(Card* ,Card*);
int compare2(Card* ,Card*);
void initOnePack();

Card pokers[CARD_COUNT];

void main()
{
	int ret=0;
	initOnePack();

	Card* cards=NULL;
	ret=shuffle(pokers,&cards);
	/*for(int i=0;ivalue>b->value)
		return 1;
	else if(a->valuevalue)
		return 0;
	else
	{
		if(a->suit>b->suit)
			return 1;
		else 
			return 0;
	}
}
char* getCardName(const Card* card){
    //存放花色名字
    char suitStr[16]={0};//0=='\0'
    switch (card->suit) {
    case heart:
        strcpy(suitStr,"红桃");
        break;
    case spade:
        strcpy(suitStr,"黑桃");
        break;
    case diamond:
        strcpy(suitStr,"方块");
        break;
    case club:
        strcpy(suitStr,"梅花");
        break;
    }
    //存放点数名字
    char valueStr[16];
    switch(card->value){
    case 1:
        strcpy(valueStr,"A");
        break;
    case 11:
        strcpy(valueStr,"J");
        break;
    case 12:
        strcpy(valueStr,"Q");
        break;
    case 13:
        strcpy(valueStr,"K");
        break;
    case 14:
        strcpy(valueStr,"小王");
        break;
    case 15:
        strcpy(valueStr,"大王");
        break;
    default:
        sprintf(valueStr,"%d",card->value);
        break;
    }
    //动态分配足够的空间
    char * ret =(char*) malloc(16);
    //将两个名字合并到ret中
    sprintf(ret,"%s%s",suitStr,valueStr);
    return ret;
}



你可能感兴趣的:(其他)