一个c++写的发牌程序

可以显示一,二,三,四家的牌

主要是为了训练我的牌感(记牌的能力),对有志于赌博或者桥牌事业的同志可能还有点用

编译为bd.exe,使用方法输入"bd -h"看帮助

以下是源代码(处理命令行参数的代码参考了vim):

None.gif #include  < iostream > 
None.gif#include  < algorithm > 
None.gif#include  < functional > 
None.gif#include  < vector > 
None.gif  using    namespace  std;
None.gif
None.gif#include  < cstdlib > 
None.gif#include  < ctime > 
None.gif#include  < cctype > 
None.gif 
None.gif  static    char  program_name[] = " bd " ;
None.gif  #define  THE_VERSION "0.1" 
None.gif 
ExpandedBlockStart.gif  namespace  info  {
ExpandedSubBlockStart.gif     enum   { WEST = 0 ,NORTH = 1 ,EAST = 2 ,SOUTH = 3 ,} ;
ExpandedBlockEnd.gif
;
ExpandedBlockStart.gif  static    char *  deno[] =  { " Spade " , " Heart " , " Diamond " , " Cotton " ,} ;
None.gif  static    char  card_symbol[] = " 23456789TJQKA " ;
None.gif
None.gif  //   p - pointer to argument
None.gif
  //   idx - index in argument
None.gif
  //   default value  
None.gif 
static    int 
None.gifget_number_arg(  char *  p,  int  def)
ExpandedBlockStart.gif  {
InBlock.gif     if  (isdigit( * p))
ExpandedSubBlockStart.gif     {
InBlock.gif    def  =  atoi(p);
ExpandedSubBlockEnd.gif    } 

InBlock.gif     return  def;
ExpandedBlockEnd.gif

None.gif 
None.gif  static    void 
None.gifprint_help()
ExpandedBlockStart.gif  {
InBlock.gif    cout << " Usage:  " << program_name << "  [OPTION]dot.gif " << endl;
InBlock.gif    cout << " Bridge Dealer " << endl;
InBlock.gif    cout << " Example:  " << program_name << "  -p4 " << endl;
InBlock.gif    cout << endl;
InBlock.gif    cout << "  -p           number of players, value ranges from 1 to 4 " << endl;
InBlock.gif    cout << "               \ "" <InBlock.gif    cout << "  -n           number of deals, default \ " 1 \ "" << endl;
InBlock.gif    cout << "  -v,--version print version information and exit " << endl;
InBlock.gif    cout << "  -h,--help    display this help and exit " << endl;
InBlock.gif    cout << endl;
InBlock.gif    exit( 0 );
ExpandedBlockEnd.gif

None.gif 
None.gif  static    void 
None.gifprint_version()
ExpandedBlockStart.gif  {
InBlock.gif    cout << program_name << "  version  " << THE_VERSION << endl;
InBlock.gif    exit( 0 );
ExpandedBlockEnd.gif

None.gif 
None.gif  static    void 
None.gifdump_one_hand(vector <  int > ::iterator ori,  int  sps = 0 )
ExpandedBlockStart.gif  {
InBlock.gif    vector < int > ::iterator iter = ori;
InBlock.gif     for ( int  i = 0 ;i < sizeof (deno) / sizeof (deno[ 0 ]);i ++ )
ExpandedSubBlockStart.gif     {
InBlock.gif         for ( int  j = 0 ;j < sps;j ++ ) cout << "   " ;
InBlock.gif        cout << deno[i][ 0 ] << " :  " ;
InBlock.gif         for (;(iter != ori + 13 )  &&  ( * iter) / 13 == i;iter ++ )
ExpandedSubBlockStart.gif         {
InBlock.gif            cout << card_symbol[( * iter) % 13 ] << "   " ;
ExpandedSubBlockEnd.gif        } 

InBlock.gif        cout << endl;
ExpandedSubBlockEnd.gif    } 

ExpandedBlockEnd.gif

None.gif 
None.gif 
None.gif  static    void 
None.gifdump_two_hands(vector <  int > ::iterator o1, vector <  int > ::iterator o2,  int  sps = 0 )
ExpandedBlockStart.gif  {
InBlock.gif    vector < int > ::iterator iter1 = o1;
InBlock.gif    vector < int > ::iterator iter2 = o2;
InBlock.gif     int  j;
InBlock.gif     int  cnt;
InBlock.gif     for ( int  i = 0 ;i < sizeof (deno) / sizeof (deno[ 0 ]);i ++ )
ExpandedSubBlockStart.gif     {
InBlock.gif        cout << deno[i][ 0 ] << " :  " ;
InBlock.gif        cnt = 2 ;
InBlock.gif         for (;(iter1 != o1 + 13 )  &&  ( * iter1) / 13 == i;iter1 ++ )
ExpandedSubBlockStart.gif         {
InBlock.gif            cout << card_symbol[( * iter1) % 13 ] << "   " ;
InBlock.gif            cnt = cnt + 2 ;
ExpandedSubBlockEnd.gif        } 

InBlock.gif 
InBlock.gif         for (j = 0 ;j < sps + sps - cnt;j ++ ) cout << "   " ;
InBlock.gif        cout << deno[i][ 0 ] << " :  " ;
InBlock.gif         for (;(iter2 != o2 + 13 )  &&  ( * iter2) / 13 == i;iter2 ++ )
ExpandedSubBlockStart.gif         {
InBlock.gif            cout << card_symbol[( * iter2) % 13 ] << "   " ;
ExpandedSubBlockEnd.gif        } 

InBlock.gif        cout << endl;
ExpandedSubBlockEnd.gif    } 

ExpandedBlockEnd.gif

None.gif  //  error message routines---------------begin 
None.gif 
static    char *  bs_errors[] = 
ExpandedBlockStart.gif  {
InBlock.gif     " Unknown option " ,
InBlock.gif #define  BS_UNKNOWN_OPTION 0 
InBlock.gif     " Too many arguments " ,
InBlock.gif #define  BS_TOO_MANY_ARGS 1 
InBlock.gif     " Argument missing after " ,
InBlock.gif #define  BS_ARG_MISSING 2 
InBlock.gif     " Garbage after option " ,
InBlock.gif #define  BS_GARBAGE      3 
InBlock.gif     " Too many extra commands " ,
InBlock.gif #define  BS_EXTRA_CMD        4 
InBlock.gif     " Invalid argument for " ,
InBlock.gif #define  BS_INVALID_ARG      5 
ExpandedBlockEnd.gif
;
None.gif  //  error message routines---------------end 
None.gif 

None.gif  int 
None.gifmain(  int  argc,   char **  argv)
ExpandedBlockStart.gif  {
ExpandedSubBlockStart.gif     int  argv_idx  =   1 ;        /*  active option letter is argv[0][argv_idx]  */ 
InBlock.gif     int  players = 1 ;
InBlock.gif     int  number_of_deals = 1 ;
InBlock.gif     bool  want_argument = false ;
InBlock.gif
InBlock.gif     while (argc > 0 )
ExpandedSubBlockStart.gif     {
InBlock.gif         if (argv[ 0 ][ 0 ] == ' - ' )
ExpandedSubBlockStart.gif         {
InBlock.gif             char  c = argv[ 0 ][argv_idx ++ ];
InBlock.gif             switch (c)
ExpandedSubBlockStart.gif             {
InBlock.gif             case   0 :
InBlock.gif                 // do nothing 
InBlock.gif 
                argv_idx =- 1 ;
InBlock.gif                 break ;
InBlock.gif             case   ' - ' :  //  "--" don't take any more options 
ExpandedSubBlockStart.gif 
                 if (strcmp(argv[ 0 ] + argv_idx, " version " ) == 0 ) {
InBlock.gif                    print_version();
ExpandedSubBlockEnd.gif                } 

ExpandedSubBlockStart.gif                  else   if (strcmp(argv[ 0 ] + argv_idx, " help " ) == 0 ) {
InBlock.gif                    print_help();
ExpandedSubBlockEnd.gif                } 

InBlock.gif                argv_idx =- 1 ;
InBlock.gif                 break ;
InBlock.gif             case   ' h ' :
InBlock.gif                print_help();
InBlock.gif                 break ;
InBlock.gif             case   ' v ' :
InBlock.gif                print_version();
InBlock.gif                 break ;
InBlock.gif             case   ' p ' :  //  1,2,3,4 players, default 1 
ExpandedSubBlockStart.gif 
                 if  (argv[ 0 ][argv_idx]) {  //  "-p{tag}" 
InBlock.gif 
                    players = get_number_arg(argv[ 0 ] + argv_idx, 1 );
InBlock.gif                    argv_idx =- 1 ;
ExpandedSubBlockEnd.gif                } 

ExpandedSubBlockStart.gif                  else {  //  "-p {tag}" 
InBlock.gif 
                    want_argument = true ;
ExpandedSubBlockEnd.gif                } 

InBlock.gif                 break ;
InBlock.gif             case   ' n ' :  // number of deals, default 1 
ExpandedSubBlockStart.gif 
                 if  (argv[ 0 ][argv_idx]) {  //  "-n{tag}" 
InBlock.gif 
                    number_of_deals = get_number_arg(argv[ 0 ] + argv_idx, 1 );
InBlock.gif                     if (number_of_deals <= 0 ) number_of_deals = 1 ;
InBlock.gif                    argv_idx =- 1 ;
ExpandedSubBlockEnd.gif                } 

ExpandedSubBlockStart.gif                  else {  //  "-n {tag}" 
InBlock.gif 
                    want_argument = true ;
ExpandedSubBlockEnd.gif                } 

InBlock.gif                 break ;
InBlock.gif             default :
InBlock.gif                 break ;
ExpandedSubBlockEnd.gif            } 

InBlock.gif 
InBlock.gif 
InBlock.gif             // handle options with argument 
InBlock.gif 
             if (want_argument)
ExpandedSubBlockStart.gif             {
ExpandedSubBlockStart.gif                 if  (argv[ 0 ][argv_idx]) {
InBlock.gif                    cerr << bs_errors[BS_GARBAGE] << " : \ "" <InBlock.gif                    exit( 1 );
ExpandedSubBlockEnd.gif                } 

InBlock.gif 
InBlock.gif                 -- argc;
ExpandedSubBlockStart.gif                 if  (argc < 1 ) {
InBlock.gif                    cerr << bs_errors[BS_ARG_MISSING] << " : \ "" <InBlock.gif                    exit( 1 );
ExpandedSubBlockEnd.gif                } 

InBlock.gif                 ++ argv;
InBlock.gif                argv_idx  =   - 1 ;
InBlock.gif
InBlock.gif                 switch (c)
ExpandedSubBlockStart.gif                 {
InBlock.gif                 case   ' p ' :
InBlock.gif                    players = get_number_arg(argv[ 0 ], 1 );
InBlock.gif                    argv_idx =- 1 ;
InBlock.gif                     break ;
InBlock.gif                 case   ' n ' :
InBlock.gif                    number_of_deals = get_number_arg(argv[ 0 ], 1 );
InBlock.gif                     if (number_of_deals <= 0 ) number_of_deals = 1 ;
InBlock.gif                    argv_idx =- 1 ;
InBlock.gif                     break ;
InBlock.gif                 default :
InBlock.gif                    ;  // impossible 
ExpandedSubBlockEnd.gif 
                } 

ExpandedSubBlockEnd.gif            } 

InBlock.gif 
ExpandedSubBlockEnd.gif        } 

ExpandedSubBlockStart.gif          else {
InBlock.gif            argv_idx =- 1 ; // do nothing 
ExpandedSubBlockEnd.gif 
        } 

InBlock.gif         //  If there are no more letters after the current "-", go to next
InBlock.gif        
 //  argument.  argv_idx is set to -1 when the current argument is to be
InBlock.gif        
 //  skipped. 
InBlock.gif 
         if  (argv_idx  <=   0   ||  argv[ 0 ][argv_idx]  ==   0 )
ExpandedSubBlockStart.gif         {
InBlock.gif             -- argc;
InBlock.gif             ++ argv;
InBlock.gif            argv_idx  =   1 ;
ExpandedSubBlockEnd.gif        } 

ExpandedSubBlockEnd.gif    } 

InBlock.gif     // init 
InBlock.gif 
    srand(time( 0 ));
InBlock.gif
InBlock.gif     // original card
InBlock.gif    
 // value: spade, 0-12; heart, 13-25; diamond 26-38; cotton 39-51;
InBlock.gif    
 // array index: west, 0-12; north, 13-25; east 26-38; south 39-51; 
InBlock.gif 
     const   int  dim = 52 ;
InBlock.gif     int  cards[dim];
InBlock.gif
InBlock.gif     for ( int  i = 0 ;i < dim;i ++ )
ExpandedSubBlockStart.gif     {
InBlock.gif        cards[i] = i;
ExpandedSubBlockEnd.gif    } 

InBlock.gif    vector < int >  h(dim);
InBlock.gif    h.assign(cards,cards + dim);
InBlock.gif
InBlock.gif     // redeal 
InBlock.gif 
     bool  need_id = number_of_deals > 1 ;
InBlock.gif     for ( int  k = 0 ;k < number_of_deals;k ++ )
ExpandedSubBlockStart.gif     {
InBlock.gif        random_shuffle(h.begin(),h.end());
InBlock.gif        sort(h.begin() + info::WEST * 13 ,h.begin() + info::WEST * 13 + 13 );
InBlock.gif        sort(h.begin() + info::NORTH * 13 ,h.begin() + info::NORTH * 13 + 13 );
InBlock.gif        sort(h.begin() + info::EAST * 13 ,h.begin() + info::EAST * 13 + 13 );
InBlock.gif        sort(h.begin() + info::SOUTH * 13 ,h.begin() + info::SOUTH * 13 + 13 );
InBlock.gif
InBlock.gif         // dump 
ExpandedSubBlockStart.gif 
         if (need_id) {
InBlock.gif            cout << " ID:  " << k + 1 << endl;
ExpandedSubBlockEnd.gif        } 

InBlock.gif         switch (players)
ExpandedSubBlockStart.gif         {
InBlock.gif         case   1 :
InBlock.gif            dump_one_hand(h.begin() + info::SOUTH * 13 );
InBlock.gif             break ;
InBlock.gif         case   2 :
InBlock.gif            dump_two_hands(h.begin() + info::EAST * 13 ,h.begin() + info::WEST * 13 , 26 );
InBlock.gif             break ;
InBlock.gif         case   3 :
InBlock.gif            dump_one_hand(h.begin() + info::NORTH * 13 , 26 );
InBlock.gif            dump_one_hand(h.begin() + info::EAST * 13 );
InBlock.gif            dump_one_hand(h.begin() + info::SOUTH * 13 , 26 );
InBlock.gif             break ;
InBlock.gif         case   4 :
InBlock.gif            dump_one_hand(h.begin() + info::NORTH * 13 , 26 );
InBlock.gif            dump_two_hands(h.begin() + info::EAST * 13 ,h.begin() + info::WEST * 13 , 26 );
InBlock.gif            dump_one_hand(h.begin() + info::SOUTH * 13 , 26 );
InBlock.gif             break ;
InBlock.gif         default :
InBlock.gif            ; // impossible 
ExpandedSubBlockEnd.gif 
        } 

InBlock.gif        cout << endl;
ExpandedSubBlockEnd.gif    } 

InBlock.gif     return   0 ;
ExpandedBlockEnd.gif

None.gif

你可能感兴趣的:(一个c++写的发牌程序)