洗牌和发牌模拟程序

DeckofCards.h

class DeckofCards
{public:
   DeckofCards();
   void shuffle();
   void deal();
 private:
   int deck[4][13];
}

DeckofCards.cpp

#include 
using std::cout;
using std::left;
using std::right;
#include 
using std::setw;
#include 
using std::rand;
using std::srand;
#include 
#include "DeckofCards.h"
DeckofCards::DeckofCards()
{for(int row=0;row<=3;row++)
  {for(int column=0;column<=12;column++)
     {deck[row][column]=0;
      }
   }
 srand(time(0));
}
void DeckofCards::shuffle()
{int row,column;
 for(int card=1;card<=52;card++)
   {do//choose a new randomly location until unoccupied slot is found
     {row=rand()%4;
      column=rand()%13;
      }while(deck[row][column]!=0);
    deck[row][column]=card;
    }
}
void DeckofCards::deal()
{static const char *suit[4]={"hearts","diamonds","clubs","spades"};
 static const char *face[13]={"Ace","Deuce","three","four","five","six","seven","eight","nine","ten","Jack","Queen","King"};
 for(int card=1;card<=52;card++)
  {for(int row=0;row<=3;row++)
    {if(deck[row][column]==card)
      {cout<
demo.cpp

#include"DeckofCards.h"
int main()
{DeckofCards deckofcards;
 deckofcards.shuffle();
 deckofcards.deal();
 return 0;
}
setw函数为设置输出字符的宽度,后边的righ为右对齐,如果是left就为左对齐,如果字符长度不够设定的宽度就用空格补齐


你可能感兴趣的:(C++)