當前位置:編程學習大全網 - 編程語言 - c++ 撲克牌洗牌發牌模擬

c++ 撲克牌洗牌發牌模擬

Hi,這裏給以壹份新鮮出爐的實例代碼。

Poker.h

#ifndef?__POKER_H__

#define?__POKER_H__

#include?<random>

#include?<time.h>

#define?SHOW_10()? printf("10?")

const?int_SIZE?=?13;

const?intPOKER_SIZE=?4*_SIZE?;

const?intSHUFFLE_TIMES?=?POKER_SIZE;

static?char?DEF_POKER[POKER_SIZE]?=?{

'2','3','4','5','6','7','8','9','1','J','Q','K','A'?,

'2','3','4','5','6','7','8','9','1','J','Q','K','A'?,

'2','3','4','5','6','7','8','9','1','J','Q','K','A'?,

'2','3','4','5','6','7','8','9','1','J','Q','K','A'?

};

class?Poker

{

public:

Poker(void)

{

srand((int)time(0));

memcpy(?_curPoker,DEF_POKER,POKER_SIZE?);

}

~Poker(void);

void?Shuffle();

void?ShowPoker();

void?ShowPoker(?const?char*?pokerBuff,int?size);

//?Deal?Cards?To?The?Player

//?@playerPokerBuf?the?buffer?of?the?playing?player

//?@lenthe?count?of?the?player

//?@lastLenthe?number?of?cards?for?leaving

void?DealCards(?char*?playerPokerBuf[],int?len,int?lastLen?);

void?DealCardsTo4Player()?;

inline?void?Swap(?char&?a,char&?b)

{

char?t?=?a;

a?=?b;

b?=?t;

}

private:

char?_curPoker[POKER_SIZE];

};

#endif

Poker.cpp

#include?"Poker.h"

void?Poker::Shuffle()

{

int?r?=?0;

for(?int?i?=?0;i<SHUFFLE_TIMES;++i?)

{

r?=?rand()%POKER_SIZE;

Swap(?_curPoker[i],_curPoker[r]?);

}

}

void?Poker::ShowPoker()

{

int?r?=?0;

for(?int?i?=?0;i<POKER_SIZE;++i?)

{

if(?i?%?13?==0?)

{

printf("\n\t");

}

if(?_curPoker[i]=='1'?)

{

SHOW_10();

}

else

{

printf("%c?",_curPoker[i]);

}

}

}

void?Poker::ShowPoker(?const?char*?pokerBuff,int?size)

{

for(?int?i=0;i<size;++i?)

{

if(?*(pokerBuff+i)=='1'?)

{

SHOW_10();

}

else

{

printf("%c?",*(pokerBuff+i));

}

}

}

//?Deal?Cards?To?The?Player

//?@playerPokerBuf?the?buffer?of?the?playing?player

//?@lenthe?count?of?the?player

//?@lastLenthe?number?of?cards?for?leaving

void?Poker::DealCards(?char*?playerPokerBuf[],int?len,int?lastLen?)

{

int?r?=?-1;

int?i=0,j=0;

for(?i?=?lastLen;i<POKER_SIZE;++i?)

{

if(?i%len?==?0?)

{

r++;

}

playerPokerBuf[j++%len][r]?=?_curPoker[i];

}

}

void?Poker::DealCardsTo4Player()?

{

char?buf1[_SIZE]={0};

char?buf2[_SIZE]={0};

char?buf3[_SIZE]={0};

char?buf4[_SIZE]={0};

char?*player[4]={buf1,buf2,buf3,buf4};

DealCards(?player,4,0);

for(?int?i=0;i<?4;++i?)

{

printf("?\n\t%d?player's?Cards:",i+1);

ShowPoker(?player[i],_SIZE?);

}

printf("\n");

}

測試代碼

#include?"Poker.h"

int?main(?int?argc,?char**?argv?)

{

Poker?*p?=?new?Poker();

p->Shuffle();

//p->ShowPoker();

p->DealCardsTo4Player();

return?0;

}

測試如下:

如果需要源文件可以聯系我!

  • 上一篇:溫州夜校有哪些專業?
  • 下一篇:有哪些值得推薦的微信背景圖?
  • copyright 2024編程學習大全網