一个c++写的发牌程序

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

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

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

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

 #include  < iostream > 
#include  < algorithm > 
#include  < functional > 
#include  < vector > 
 using   namespace  std;

#include  < cstdlib > 
#include  < ctime > 
#include  < cctype > 
 
 static   char  program_name[] = " bd " ;
 #define  THE_VERSION "0.1" 
 
 namespace  info {
     enum   { WEST = 0 ,NORTH = 1 ,EAST = 2 ,SOUTH = 3 ,} ;
} ;
 static   char *  deno[] = { " Spade " , " Heart " , " Diamond " , " Cotton " ,} ;
 static   char  card_symbol[] = " 23456789TJQKA " ;

 //  p - pointer to argument
 //  idx - index in argument
 //  default value  
 static   int 
get_number_arg( char *  p, int  def)
 {
     if  (isdigit( * p))
     {
    def  =  atoi(p);
    } 
     return  def;

 
 static   void 
print_help()
 {
    cout << " Usage:  " << program_name << "  [OPTION] " << endl;
    cout << " Bridge Dealer " << endl;
    cout << " Example:  " << program_name << "  -p4 " << endl;
    cout << endl;
    cout << "  -p           number of players, value ranges from 1 to 4 " << endl;
    cout << "               \ "" <<program_name<< " \ "  with NO option equals \ "" <<program_name<< "   - p1\ "   " << endl;
    cout << "  -n           number of deals, default \ " 1 \ "" << endl;
    cout << "  -v,--version print version information and exit " << endl;
    cout << "  -h,--help    display this help and exit " << endl;
    cout << endl;
    exit( 0 );

 
 static   void 
print_version()
 {
    cout << program_name << "  version  " << THE_VERSION << endl;
    exit( 0 );

 
 static   void 
dump_one_hand(vector < int > ::iterator ori, int  sps = 0 )
 {
    vector < int > ::iterator iter = ori;
     for ( int  i = 0 ;i < sizeof (deno) / sizeof (deno[ 0 ]);i ++ )
     {
         for ( int  j = 0 ;j < sps;j ++ ) cout << "   " ;
        cout << deno[i][ 0 ] << " :  " ;
         for (;(iter != ori + 13 )  &&  ( * iter) / 13 == i;iter ++ )
         {
            cout << card_symbol[( * iter) % 13 ] << "   " ;
        } 
        cout << endl;
    } 

 
 
 static   void 
dump_two_hands(vector < int > ::iterator o1, vector < int > ::iterator o2, int  sps = 0 )
 {
    vector < int > ::iterator iter1 = o1;
    vector < int > ::iterator iter2 = o2;
     int  j;
     int  cnt;
     for ( int  i = 0 ;i < sizeof (deno) / sizeof (deno[ 0 ]);i ++ )
     {
        cout << deno[i][ 0 ] << " :  " ;
        cnt = 2 ;
         for (;(iter1 != o1 + 13 )  &&  ( * iter1) / 13 == i;iter1 ++ )
         {
            cout << card_symbol[( * iter1) % 13 ] << "   " ;
            cnt = cnt + 2 ;
        } 
 
         for (j = 0 ;j < sps + sps - cnt;j ++ ) cout << "   " ;
        cout << deno[i][ 0 ] << " :  " ;
         for (;(iter2 != o2 + 13 )  &&  ( * iter2) / 13 == i;iter2 ++ )
         {
            cout << card_symbol[( * iter2) % 13 ] << "   " ;
        } 
        cout << endl;
    } 

 // error message routines---------------begin 
 static   char *  bs_errors[] = 
 {
     " Unknown option " ,
 #define  BS_UNKNOWN_OPTION 0 
     " Too many arguments " ,
 #define  BS_TOO_MANY_ARGS 1 
     " Argument missing after " ,
 #define  BS_ARG_MISSING 2 
     " Garbage after option " ,
 #define  BS_GARBAGE      3 
     " Too many extra commands " ,
 #define  BS_EXTRA_CMD        4 
     " Invalid argument for " ,
 #define  BS_INVALID_ARG      5 
} ;
 // error message routines---------------end 
 
 int 
main( int  argc,  char **  argv)
 {
     int  argv_idx  =   1 ;        /*  active option letter is argv[0][argv_idx]  */ 
     int  players = 1 ;
     int  number_of_deals = 1 ;
     bool  want_argument = false ;

     while (argc > 0 )
     {
         if (argv[ 0 ][ 0 ] == ' - ' )
         {
             char  c = argv[ 0 ][argv_idx ++ ];
             switch (c)
             {
             case   0 :
                 // do nothing 
                 argv_idx =- 1 ;
                 break ;
             case   ' - ' :  //  "--" don't take any more options 
                  if (strcmp(argv[ 0 ] + argv_idx, " version " ) == 0 ) {
                    print_version();
                } 
                  else   if (strcmp(argv[ 0 ] + argv_idx, " help " ) == 0 ) {
                    print_help();
                } 
                argv_idx =- 1 ;
                 break ;
             case   ' h ' :
                print_help();
                 break ;
             case   ' v ' :
                print_version();
                 break ;
             case   ' p ' :  //  1,2,3,4 players, default 1 
                  if  (argv[ 0 ][argv_idx]) {  //  "-p{tag}" 
                     players = get_number_arg(argv[ 0 ] + argv_idx, 1 );
                    argv_idx =- 1 ;
                } 
                  else {  //  "-p {tag}" 
                     want_argument = true ;
                } 
                 break ;
             case   ' n ' :  // number of deals, default 1 
                  if  (argv[ 0 ][argv_idx]) {  //  "-n{tag}" 
                     number_of_deals = get_number_arg(argv[ 0 ] + argv_idx, 1 );
                     if (number_of_deals <= 0 ) number_of_deals = 1 ;
                    argv_idx =- 1 ;
                } 
                  else {  //  "-n {tag}" 
                     want_argument = true ;
                } 
                 break ;
             default :
                 break ;
            } 
 
 
             // handle options with argument 
              if (want_argument)
             {
                 if  (argv[ 0 ][argv_idx]) {
                    cerr << bs_errors[BS_GARBAGE] << " : \ "" <<argv[0]<< " \ "" << endl;
                    exit( 1 );
                } 
 
                 -- argc;
                 if  (argc < 1 ) {
                    cerr << bs_errors[BS_ARG_MISSING] << " : \ "" <<argv[0]<< " \ "" << endl;
                    exit( 1 );
                } 
                 ++ argv;
                argv_idx  =   - 1 ;

                 switch (c)
                 {
                 case   ' p ' :
                    players = get_number_arg(argv[ 0 ], 1 );
                    argv_idx =- 1 ;
                     break ;
                 case   ' n ' :
                    number_of_deals = get_number_arg(argv[ 0 ], 1 );
                     if (number_of_deals <= 0 ) number_of_deals = 1 ;
                    argv_idx =- 1 ;
                     break ;
                 default :
                    ;  // impossible 
                 } 
            } 
 
        } 
          else {
            argv_idx =- 1 ; // do nothing 
         } 
         //  If there are no more letters after the current "-", go to next
         //  argument.  argv_idx is set to -1 when the current argument is to be
         //  skipped. 
          if  (argv_idx  <=   0   ||  argv[ 0 ][argv_idx]  ==   0 )
         {
             -- argc;
             ++ argv;
            argv_idx  =   1 ;
        } 
    } 
     // init 
     srand(time( 0 ));

     // original card
     // value: spade, 0-12; heart, 13-25; diamond 26-38; cotton 39-51;
     // array index: west, 0-12; north, 13-25; east 26-38; south 39-51; 
      const   int  dim = 52 ;
     int  cards[dim];

     for ( int  i = 0 ;i < dim;i ++ )
     {
        cards[i] = i;
    } 
    vector < int >  h(dim);
    h.assign(cards,cards + dim);

     // redeal 
      bool  need_id = number_of_deals > 1 ;
     for ( int  k = 0 ;k < number_of_deals;k ++ )
     {
        random_shuffle(h.begin(),h.end());
        sort(h.begin() + info::WEST * 13 ,h.begin() + info::WEST * 13 + 13 );
        sort(h.begin() + info::NORTH * 13 ,h.begin() + info::NORTH * 13 + 13 );
        sort(h.begin() + info::EAST * 13 ,h.begin() + info::EAST * 13 + 13 );
        sort(h.begin() + info::SOUTH * 13 ,h.begin() + info::SOUTH * 13 + 13 );

         // dump 
          if (need_id) {
            cout << " ID:  " << k + 1 << endl;
        } 
         switch (players)
         {
         case   1 :
            dump_one_hand(h.begin() + info::SOUTH * 13 );
             break ;
         case   2 :
            dump_two_hands(h.begin() + info::EAST * 13 ,h.begin() + info::WEST * 13 , 26 );
             break ;
         case   3 :
            dump_one_hand(h.begin() + info::NORTH * 13 , 26 );
            dump_one_hand(h.begin() + info::EAST * 13 );
            dump_one_hand(h.begin() + info::SOUTH * 13 , 26 );
             break ;
         case   4 :
            dump_one_hand(h.begin() + info::NORTH * 13 , 26 );
            dump_two_hands(h.begin() + info::EAST * 13 ,h.begin() + info::WEST * 13 , 26 );
            dump_one_hand(h.begin() + info::SOUTH * 13 , 26 );
             break ;
         default :
            ; // impossible 
         } 
        cout << endl;
    } 
     return   0 ;

时间: 2024-09-16 13:53:37

一个c++写的发牌程序的相关文章

c#中使用wpf做一个手写识别的程序

问题描述 在WPF中怎么删除当前在面板上显示已经写好的字?在点击事件中的代码是什么?学生求教,谢谢哦. 解决方案 解决方案二:Clear?解决方案三:你用的是WPF提供的InkCanvas吗?如果是,可以通过清除其Strokes集合来实现.比如:inkCanvas.Strokes.Clear();

javascript写的一个模拟阅读小说的程序

 这篇文章主要介绍了用javascript写了一个模拟阅读小说的程序,需要的朋友可以参考下  代码如下: <html>  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />  <head>  <title></title>  <script type="text/javascript"> 

急求:如何将vs的一个已经写好的mfc含有界面的主程序封装成mfcdll,供其他程序调用?

问题描述 急求:如何将vs的一个已经写好的mfc含有界面的主程序封装成mfcdll,供其他程序调用? 两个项目同时进行,一个是3d,一个是2d,现在需要把两个合在一起,需要把3d的mfc项目封装成dll供2d的mfc调用. 解决方案 一个完整的程序封装成dll是一个复杂的事情,尤其是你的程序使用了mfc框架,并且有大量的全局变量.你的团队要有足够的资源去做这件事情,它不亚于从头开发. 如果你们没有足够的资源,最好还是采用进程通讯和进程调用的方法,避免源代码的大改. 解决方案二: 直接利用VS的工

微软-用c# 写的一个解压文件的程序 解压之后里面的文件不见了 贴上代码~

问题描述 用c# 写的一个解压文件的程序 解压之后里面的文件不见了 贴上代码~ string the_rar; RegistryKey the_Reg; object the_Obj; string the_Info; try { the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.exe"); the_Obj = the_Reg.GetVa

c#-C# 编写一个获得单词翻译的程序,思路是用在线词典,请问怎么写比较好?

问题描述 C# 编写一个获得单词翻译的程序,思路是用在线词典,请问怎么写比较好? C# 编写一个获得单词翻译的程序,思路是用在线词典,请问怎么写比较好? 解决方案 http://blog.csdn.net/apollokk/article/details/9341463 解决方案二: 要具体代码?那请你先采纳我的回答. 解决方案三: http://tech.ddvip.com/2011-11/1321509098169886.html 解决方案四: http://blog.csdn.net/ap

我是一个JAVA写程序员,配置一台笔记本电脑,求建议

问题描述 我是一个JAVA写程序员,配置一台笔记本电脑,求建议 我是一个JAVA写程序员,配置一台笔记本电脑,求建议.具体什么牌子型号现在买了一台THINKPAD E565是A核A卡,感觉被坑了.想换一台. 解决方案 感觉坑,就自己组装一台~ 解决方案二: 我用的是Y480 配置刚刚的 解决方案三: 我用的华硕,I7处理器.4k多点.用起来很流畅. 解决方案四: 处理器I7吧,运行内存可以买个4G的,然后买个4G的内存条按上.绝对很流畅

vc 如何创建服务-VC 开机自动启动。不用写注册表的方法。想写一个服务来启动这个程序。

问题描述 VC 开机自动启动.不用写注册表的方法.想写一个服务来启动这个程序. 有个EXE程序想要它开机自动启动.我不想用写注册表的方法启动.看能不能够写一个服务来启动这个程序.如果可以麻烦大家能够提供源码.谢谢!!!!! 解决方案 创建服务,CreateService()

vsto 写 excel 外接程序,如何实现鼠标滑过一个单元格时,触发一个事件?

问题描述 vsto 写 excel 外接程序,如何实现鼠标滑过一个单元格时,触发一个事件? vsto 写 excel 外接程序,如何实现鼠标滑过一个单元格时,触发一个事件?

一个java写的程序,求详细每一行的解释

问题描述 一个java写的程序,求详细每一行的解释 import java.util.Scanner; public class Test{ public static void main(String args[]){ Scanner input = new Scanner(System.in); final int a =50; final int b =10; int c = 0; int d = 2; while(c<a){ boolean e =true ; for(int divis