📄 chkmake.c
字号:
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <curses.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#define THREE_CARD 1#define STRAIGHT_CARD 2#define PAIR_CARD 3#include "mjdef.h"#include "qkmj.h"/******************* Denifition of variables **********************/struct nodetype { char info[5]; int end; int type; struct nodetype *three; /* 刻子 */ struct nodetype *straight; /* 顺子 */ struct nodetype *pair; /* 对子 */ struct nodetype *father;};typedef struct nodetype *NODEPTR;struct card_info_type{ int info; int flag; /* 0 if is chosen, 1 if not */};struct card_info_type card_info[20];NODEPTR make_three();NODEPTR make_straight();NODEPTR make_pair();char pool_buf[20];struct component_type{ int type; /* 0:invalid 1:no card 2:normal 3:one pair 4:more than one pair */ char info[10][5];};struct component_type card_component[6][50];int comb_count[6];int count2;int suit;/********************************************************************/NODEPTR getnode(){ NODEPTR p; p=(NODEPTR) malloc(sizeof(struct nodetype)); return(p);}NODEPTR maketree(node)NODEPTR node;{ NODEPTR p; p=getnode(); p->info[0]=0; p->type=0; p->end=0; p->three=NULL; p->straight=NULL; p->pair=NULL; p->father=node; return(p);}build_tree(node)NODEPTR node;{ node->three=make_three(node); /* It will check if it's end */ if(!node->end) node->straight=make_straight(node); else node->straight=NULL; if(!node->end && node->three==NULL) /* add pair into tree if no three */ node->pair=make_pair(node); else node->pair=NULL;}mark_card(node)NODEPTR node;{ int i,j; if(node->father==NULL) /* The top of the tree */ { return; } else { i=0; for(j=0;card_info[j].info;j++) { if(node->info[i]==card_info[j].info && card_info[j].flag) { i++; card_info[j].flag=0; /* Mark this card as chosen */ if(!node->info[i]) /* no more cards */ goto marked; } } /* Can't find the card *//* display_comment("Error at marking"); */ marked:; mark_card(node->father); }}/* Find the leading card */int find_lead(){ int i,lead; for(i=0;card_info[i].info;i++) { if(card_info[i].flag) { lead=card_info[i].info; goto found_leading_card; } } return(0); found_leading_card:; return(lead);}NODEPTR make_three(node)NODEPTR node;{ int i,j; int lead; NODEPTR p; /* Reset the flag */ for(i=0;card_info[i].info;i++) card_info[i].flag=1; mark_card(node); lead=find_lead(); if(lead==0) { /* no more cards can be checked */ node->end=1; return(NULL); } p=maketree(node); /* Get three same cards */ j=0; for(i=0;card_info[i].info;i++) { if(card_info[i].flag && card_info[i].info==lead) { p->info[j++]=lead; card_info[i].flag=0; } if(j==3) goto found_three; } /* can't find 3 cards */ free(p); return(NULL); found_three:; p->info[j]=0; p->type=THREE_CARD; build_tree(p); return(p);}NODEPTR make_straight(node)NODEPTR node;{ int i,j; int lead; NODEPTR p; /* Reset the flag */ for(i=0;card_info[i].info;i++) card_info[i].flag=1; mark_card(node); lead=find_lead(); if(lead==0) { node->end=1; return(NULL); } p=maketree(node); /* Get three straight cards */ j=0; for(i=0;card_info[i].info;i++) { if(card_info[i].flag && card_info[i].info==lead) { p->info[j++]=lead; card_info[i].flag=0; lead++; } if(j==3 && lead<31) goto found_straight; } free(p); return(NULL); found_straight:; p->info[j]=0; p->type=STRAIGHT_CARD; build_tree(p); return(p);}NODEPTR make_pair(node)NODEPTR node;{ int i,j; int lead; NODEPTR p; /* Reset the flag */ for(i=0;card_info[i].info;i++) card_info[i].flag=1; mark_card(node); lead=find_lead(); if(lead==0) { node->end=1; return(NULL); } p=maketree(node); /* Get two same cards */ j=0; for(i=0;card_info[i].info;i++) { if(card_info[i].flag && card_info[i].info==lead) { p->info[j++]=lead; card_info[i].flag=0; } if(j==2) goto found_two; } free(p); return(NULL); found_two:; p->info[j]=0; p->type=PAIR_CARD; build_tree(p); return(p);}/* Find a valid tree and copy it to the array */list_path(p)NODEPTR p;{ int i; i=0; while (p->info[i]) { card_component[suit][comb_count[suit]].info[count2][i]=p->info[i]; i++; } if(p->type==PAIR_CARD) { if(card_component[suit][comb_count[suit]].type>=3) card_component[suit][comb_count[suit]].type=4; else card_component[suit][comb_count[suit]].type=3; } card_component[suit][comb_count[suit]].info[count2][i]=0; count2++; if(p->father->father) list_path(p->father);}pretrav(p)NODEPTR p;{ if(p!=NULL) { if(p->end) /* no more cards */ { count2=0; if(p->father!=NULL) /* not the head */ { card_component[suit][comb_count[suit]].type=2; list_path(p); } else /* no card in this suit */ { card_component[suit][0].type=1; } comb_count[suit]++; } pretrav(p->three); pretrav(p->straight); pretrav(p->pair); }}free_tree(p)NODEPTR p;{ if(p->three) free_tree(p->three); if(p->straight) free_tree(p->straight); if(p->pair) free_tree(p->pair); free(p);}int check_make(sit,card,method)char sit;char card;char method; /* 0 for general check, 1 for complete check */{ NODEPTR p[6]; /* p[0]=万 p[1]=筒 p[2]=索 p[3]=风牌 p[4]=三元牌 */ int i,j,k,l,len,pair,make,tmp; char msg_buf[80]; /* Copy the pool to buffer */ for(i=0;i<pool[sit].num;i++) pool_buf[i]=pool[sit].card[i]; pool_buf[i]=card; pool_buf[i+1]=0; /* Sort buffer */ for(i=0;i<=pool[sit].num;i++) for(j=0;j<pool[sit].num-i;j++) if(pool_buf[j]>pool_buf[j+1]) { tmp=pool_buf[j]; pool_buf[j]=pool_buf[j+1]; pool_buf[j+1]=tmp; } for(i=0;i<5;i++) { for(j=0;j<20;j++) { card_component[i][j].type=0; for(k=0;k<10;k++) card_component[i][j].info[k][0]=0; } comb_count[i]=0; } /****** Build tree for each suits ******/ /* j: the pointer of cards */ /* card_info: each suit of cards */ j=0; for(suit=0;suit<5;suit++) { k=0; while(pool_buf[j]<suit*10+10 && pool_buf[j]>suit*10) { card_info[k].info=pool_buf[j]; card_info[k].flag=1; j++; k++; } card_info[k].info=0; p[suit]=maketree(NULL); /* Root of the tree */ card_component[suit][0].type=0; build_tree(p[suit]); pretrav(p[suit]); free_tree(p[suit]); } pair=0; make=1; for(i=0;i<5;i++) /* Check for 5 suits */ { switch(card_component[i][0].type) { case 0: /* Not a valid path */ make=0; goto finish; break; case 1: /* No cards in this suit */ case 2: /* This path is valid */ break; case 3: /* Found a pair */ pair++; break; case 4: /* Find more than one pair in a suit*/ make=0; goto finish; break; default: break; } } finish:; if(pair!=1) make=0; if(make && method){ full_check(sit,card);/*sprintf(msg_buf,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",pool[sit].num,pool[sit].card[0],pool[sit].card[1],pool[sit].card[2],pool[sit].card[3],pool[sit].card[4],pool[sit].card[5],pool[sit].card[6],pool[sit].card[7],pool[sit].card[8],pool[sit].card[9],pool[sit].card[10],pool[sit].card[11],pool[sit].card[12],pool[sit].card[13],pool[sit].card[14],pool[sit].card[15]);display_comment(msg_buf);*/} return(make);}valid_type(type)int type;{ if(type==1 || type==2 || type==3) return(1); else return(0);}/* Find the combination of the card with the highest score */full_check(sit,make_card)char sit;char make_card;{ int i,j; int suit_count[6],set1,set2,card,count,score; char msg_buf[80]; count=0; for(i=0;i<5;i++) { card_comb[i].set_count=0; suit_count[i]=0; } /* Find all valid combinations */ for(suit_count[0]=0;suit_count[0]<comb_count[0];suit_count[0]++) { if(valid_type(card_component[0][suit_count[0]].type)) for(suit_count[1]=0;suit_count[1]<comb_count[1];suit_count[1]++) { if(valid_type(card_component[1][suit_count[1]].type)) for(suit_count[2]=0;suit_count[2]<comb_count[2];suit_count[2]++) { if(valid_type(card_component[2][suit_count[2]].type)) { set2=0; for(i=0;i<5;i++) { set1=0; while(card_component[i][suit_count[i]].info[set1][0]) { card=0; while(card_component[i][suit_count[i]].info[set1][card]) { card_comb[count].info[set2][card+1]= card_component[i][suit_count[i]].info[set1][card]; card++; } if(card_comb[count].info[set2][1]== card_comb[count].info[set2][2]) card_comb[count].info[set2][0]=2; else card_comb[count].info[set2][0]=1; if(card==2) card_comb[count].info[set2][0]=10; card_comb[count].info[set2][card+1]=0; set1++; set2++; } card_comb[count].info[set2][0]=0; card_comb[count].set_count=set2; } count++; } } } } for(i=0;i<count;i++) { check_tai(sit,i,make_card); card_comb[i].tai_sum=0; for(j=0;j<=52;j++) { card_comb[i].tai_sum+=card_comb[i].tai_score[j]; } }/* for(i=0;i<card_comb[0].set_count;i++) { sprintf(msg_buf,"([%d] %d %d %d %d)",i,card_comb[0].info[i][0],card_comb[0].info[i][1],card_comb[0].info[i][2],card_comb[0].info[i][3]);display_comment(msg_buf); }*/ comb_num=count;}/* return the number of the card found */int exist_card(sit,card)char sit;char card;{ int i,j,exist=0; for(i=0;i<=pool[sit].num;i++) { if(pool_buf[i]==card) { exist++; } } for(i=0;i<pool[sit].out_card_index;i++) { j=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -