自己写的模拟发牌程序 请批评指点
程序代码:
#include<iostream> #include<ctime> using std::cout; using std::endl; using std::ostream; //模拟发牌 class Card { public: Card(int n); //发牌 void draw_card(); private: //已发的牌数 int drawn_num; //要发的牌数 int card_num; //手中剩余的牌 int card_left; //工具数组 int array[4][13]; //工具函数 void card_rand(); //花色 static char* suit[4]; //面值 static char* face[13]; //重新洗牌 void draw_card_again(); }; char* Card::suit[4]={"Hearts","Diamonds","Clubs","Spades"}; char* Card::face[13]={"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; Card::Card(int n):card_num(n),card_left(52),drawn_num(0) { for(int i=0;i<4;i++) for(int j=0;j<13;j++) array[i][j]=0; } void Card::card_rand() { int row=rand()%4; int col=rand()%13; //如果已经发出去了 while(array[row][col]) { row=rand()%4; col=rand()%13; } array[row][col]=1; cout<<face[col]<<" of "<<suit[row]<<endl; } void Card::draw_card() { srand(time(NULL)); int& i=drawn_num; for(;i<card_num;i++,--card_left) { if(!card_left) { //重新洗牌 draw_card_again(); } else { card_rand();} } } void Card::draw_card_again() { cout<<"Reshuffling"<<endl; //全部清零 card_left=52; for(int k=0;k<4;k++) for(int j=0;j<13;j++) array[k][j]=0; int& i=drawn_num; for(;i<card_num;i++,--card_left) { //发牌 if(!card_left) draw_card_again(); else { card_rand();} } }
[ 本帖最后由 代号215 于 2013-5-9 20:29 编辑 ]