一个c 写的发牌程式
2008-02-23 05:32:56来源:互联网 阅读 ()
能够显示一、二、三、四家的牌,主要是为了训练我的牌感(记牌的能力),对有志于赌博或桥牌事业的同志可能更有点用,编译为bd.exe,使用方法输入"bd -h"看帮助。 以下是源代码(处理命令行参数的代码参考了vim): #include #include #include #include using namespace std; #include #include #include 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: "< } static void print_version() { cout< "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< --argc; if (argc<1){ cerr< 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 cards[i]=i; } vector h.assign(cards,cards dim); //redeal bool need_id=number_of_deals>1; for(int k=0;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: "< 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 } 标签: 版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
相关文章
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设 网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源 网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它 服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护 软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷 网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash 热门词条
最新资讯
热门关注
热门标签
|