本文主要是介绍typedef结合结构做的洗牌发牌,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//高性能洗牌发牌游戏算法。利用结构类型
#include
#include
#include
struct card
{
const char *face;
const char *suit;
};
typedef struct card Card;//终于明白他的意思了,原来如果你不这样写的话,后面的必须用struct card这样多麻烦阿。所以这么声明就可以用Card直接代替了!!!
void fillDeck( Card * const wDeck,const char * wFace[],const char * wSuit[] );
void shuffle( Card * const wDeck );
void deal( const Card * const wDeck);
int main()
{
Card deck[52];
const char *face[]={"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
const char *suit[]={"Hearts","Diamonds","Clubs","Spades"};
srand( time( NULL) );
fillDeck( deck, face, suit );
shuffle( deck );
deal( deck );
return 0;
}
void fillDeck( Card * const wDeck,const char * wFace[],const char * wSuit[] )
{
int i;
for( i = 0; i<52; i++)
{
wDeck[ i ].face = wFace[ i % 13 ];
wDeck[ i ].suit = wSuit[ i / 13 ];
}
}
void shuffle( Card * const wDeck )
{
int i,j;
Card temp;
for( i = 0; i <52; i++)
{
j = rand() % 52;
temp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = temp;
}
}
void deal( const Card * const wDeck)
{
int i;
for(i = 0; i<52; i++)
{ printf("%5s of %-8s%c",wDeck[i].face,wDeck[i].suit,(i+1) % 2 ? '/t' : '/n');}
}
这篇关于typedef结合结构做的洗牌发牌的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!