#include "class_head.h" void set_pai(); //初始化赋值函数 void print_screen(); //初始化屏幕语句函数 char std_input_handle(); void rand_pai(); //随机分配函数 void jiemian(); //菜单 extern player,p_win,pp; void main() { ///////菜单///////////////////////////////// jiemian(); system("cls"); system("color 0F"); ///////为136张牌初始化—//随机分组/////////// set_pai(); rand_pai(); User user[4]; ///////初始化屏幕////////////////////////// print_screen(); /////////给牌排序///////////////////////// for(int i=0;i<4;i++) user[i].index_pai(); //////////玩牌//////////////////////////// while(pp<136 && !p_win) { if(player == 4)player=0; if(player == 0)user[player].user_play_pai(1); else { user[player].computer_pai(); if(user[0].chi_paiTF()) user[0].chi_pai_print(); } player++; } //////////胜利判断//////////////////////// system("cls"); switch (p_win) { case 0:printf("流局\n");break; case 1:printf("玩家胜利\n");break; case 2:printf("左家胜利\n");break; case 3:printf("对家胜利\n");break; case 4:printf("右家胜利\n");break; } system("pause"); main(); } //—————————————————–fun.cpp————————————————// #include<iostream.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<conio.h> #include<stdio.h> #include<windows.h> void memu(int); void jiemian(); char pai[136][8]; extern pp,po,pl,player,p_win; ///////////////////////复制函数//////////////////////////// void strcopy(char *str1,char str2[8]) { for(int i=0;i<8;i++) { str1[i]=str2[i]; } } ///////////////////随机分配函数///////////////////////////// void rand_pai() { srand(time(0)); int x;char temp[10]; for(int j=0;j<136;j++) { x=rand()%136; if(x!=j) { strcopy(temp,pai[j]); strcopy(pai[j],pai[x]); strcopy(pai[x],temp); } } } ////////////////初始化赋值函数////////////////////////////// void set_pai() { pp=0;po=0;player=0;p_win=0; char fenpei[][3]={{"条"},{"筒"},{"万"}}; for(int k=0;k<3;k++) { for(int j=0;j<4;j++) { for(int i=0;i<9;i++) { char temp[10]; temp[0]=i+49; temp[1]=’\0’; strcat(temp,fenpei[k]); strcpy(pai[i+j*9+k*36],temp); pai[i+j*9+k*36][6]=k+1;pai[i+j*9+k*36][7]=i+1; } } } char fenpei1[7][5]={"东","南","西","北","中","發","白"}; for(int j=0;j<4;j++) { for(int i=0;i<7;i++) { char temp[10]; temp[0]=’ ‘; temp[1]=’\0’; strcat(temp,fenpei1[i]); strcpy(pai[108+j*7+i],temp);pai[108+j*7+i][6]=4;pai[108+j*7+i][7]=i+1; } } } ////////////////////////读取键盘按键/////////////////////////////////////////////// char std_input_handle() { return getch(); } //////////////////定位输出函数std_out_handle(x,y,背景,前景,字符串)///////////////// void std_output_handle(int x,int y,char str[],int bj) { HANDLE hOut; COORD pos= {x, y}; hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); SetConsoleTextAttribute(hOut, bj); printf("%s",str); SetConsoleTextAttribute(hOut, 0×07); } ///////////////////初始化屏幕语句函数//////////////////////////////////////////////// void print_screen() { for(int i=0;i<13;i++) { std_output_handle(7+i*4-1,2," |",0×70); } for(i=3;i<19;i++) { std_output_handle(2,i," |",0×70); std_output_handle(60,i," |",0×70); } } /////////////////用户操作区清屏函数//////////////////////////////////// void clear_user_pai() { for(int i=0;i<13;i++) { std_output_handle(7+i*4-1,20," ",0×70); } } ///////////////////标识码复制函数/////////////////// void bscopy(char *p,char p1[2]) { p[1]=p1[1]; p[0]=p1[0]; } /////////////////////判断三连牌函数////////////////////////// int sanlian (char lian_pai1[14][3],int l1,int l2) { ///////////////////定义一个局部变量做判断////////////////// char lian_pai[14][3],temp[2]; // printf("\n【%d-%d】",l1,l2); for(int i=0;i<14;i++) { lian_pai[i][0]=lian_pai1[i][0]; lian_pai[i][1]=lian_pai1[i][1]; lian_pai[i][2]=’\0’; } ///////////////////消对///////////////////////////////// for(int k=l2;k<13;k++) { lian_pai[k][0]=lian_pai[k+1][0]; lian_pai[k][1]=lian_pai[k+1][1]; } for( k=l1;k<12;k++) { lian_pai[k][0]=lian_pai[k+1][0]; lian_pai[k][1]=lian_pai[k+1][1]; } ///////////////////排序////////////////////////////// for(k=0;k<11;k++) for(int j=k+1;j<12;j++) if(lian_pai[j][0]<lian_pai[k][0] || lian_pai[j][0]==lian_pai[k][0] && lian_pai[j][1]<lian_pai[k][1]) {bscopy(temp,lian_pai[j]);bscopy(lian_pai[j],lian_pai[k]);bscopy(lian_pai[k],temp);} ///////////////////三连牌判断不是则返回0,是则返回1///这里还有逻辑问题 // for(k=0;k<12;k++) // printf("%c-%c,",lian_pai[k][0]+48,lian_pai[k][1]+48); k=0; while(k<12) { if(lian_pai[k][0] == lian_pai[k+5][0] && (lian_pai[k][1] == lian_pai[k+1][1] && lian_pai[k+2][1] == lian_pai[k+3][1] && lian_pai[k+4][1] ==lian_pai[k+5][1] && lian_pai[k+1][1] == lian_pai[k+2][1]-1 && lian_pai[k+3][1] == lian_pai[k+4][1]-1 || lian_pai[k][1] == lian_pai[k+1][1]-1 && lian_pai[k+1][1] == lian_pai[k+2][1] && lian_pai[k+2][1] == lian_pai[k+3][1]-1 && lian_pai[k+3][1] == lian_pai[k+4][1] && lian_pai[k+4][1] == lian_pai[k+5][1]-1)) { k=k+6; } else { if(lian_pai[k][0] != lian_pai[k+1][0] || lian_pai[k+1][0] != lian_pai[k+2][0]) {return 0;} //如果排序后连续三个类型不一样,肯定不是连对,返回假 //printf("\n(%c,%c)(%c,%c)(%c,%c)",lian_pai[k][0]+48,lian_pai[k][1]+48,lian_pai[k+1][0]+48,lian_pai[k+1][1]+48,lian_pai[k+2][0]+48,lian_pai[k+2][1]+48); if((lian_pai[k][1] != lian_pai[k+1][1]-1 || lian_pai[k][1] != lian_pai[k+2][1]-2) && (lian_pai[k][1] != lian_pai[k+1][1] || lian_pai[k][1] != lian_pai[k+2][1])) { return 0; } //如果类型一样,三张牌没挨着不是连对,或者不一样也不是连对,返回假值; k+=3; } } return 1; } //////////////////////下面是开始菜单/////////////////////////////////// void go(int x) { switch(x) { case 0:break; case 1:system("cls");system("type guize.txt");system("pause");jiemian();break; case 2:system("cls");system("type caozuo.txt");system("pause");jiemian();break; case 3:system("cls");system("type guanyu.txt");system("pause");jiemian();break; case 4:system("cls");exit(0);break; } } void memu(int digui) { char menu[5][10]={{" 开始 "},{"规则说明"},{"操作说明"},{" 关于 "},{" 退出 "}},ch; for(int i=0;i<5;i++) { std_output_handle(30,10+i*2,menu[i],0×70); } std_output_handle(30,10+digui*2,menu[digui],0×90); ch=std_input_handle(); switch (ch) { case 50: case 52:if(digui == 4) digui=-1;memu(digui+1);break; case 54: case 56:if(digui == 0) digui=5;memu(digui-1);break; case 53:go(digui);break; default:memu(digui); } } void jiemian() { int ch; char WELCOME[5][52]= { {‘?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’}, {‘?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘}, {‘?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’}, {‘?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’ ‘,’ ‘,’ ‘,’ ‘}, {‘?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’ ‘,’?’,’ ‘,’?’,’?’,’?’,’?’,’?’,’?’}, }; system("cls"); for(int i=0;i<5;i++) for(int j=0;j<52;j++) if(WELCOME[i][j] != 32) { for(int k=52;k>0;k-=3) if(j<i+k)ch=(k%4+4)*16; std_output_handle(j+10,i+4," ",ch); } memu(0); } //—————————————————–class_head.h————————————————// #include<iostream.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<stdio.h> class User { char U_pai[14][8]; //用户手里的牌 int short curric; //高亮光标 public: User(); void print_pai(); //打印摸的牌 void index_pai(); //为牌排序 void accept_pai(); //接收牌函数 void out_pai(int); //出牌函数 void print_user_pai(); //打印用户手里边的牌 void move_pai(char); //移动选中牌模块 void computer_pai(); //电脑出牌模块 int win_pai(char,char); //赢牌判断模块 void user_play_pai(int); //用户玩牌模块 int chi_paiTF(); //吃牌判断模块 void chi_pai_print(); //吃牌打印模块 }; //—————————————————–class_body.cpp————————————————// #include "class_head.h" extern char pai[136][8]; char chi_pai[8]; int pp=0,po=0,pl,player=0,p_win=0; //pp牌指针标记, //po出牌打印标记, //pl用户出牌否标记, //player打牌玩家标记 //玩家胜利标记 void strcopy(char *str1,char str2[8]); //字符复制函数 void std_output_handle(int x,int y,char str[],int bj); //定位输出函数 char std_input_handle(); //读取键盘按键 void clear_user_pai(); //清除用户显示区内容 int sanlian (char lian_pai[14][3],int i,int j); //判断三连牌 void bscopy(char *p,char p1[2]); //标识码复制 //////////////以下是类里面的函数///////////////////////////// //////////////构造函数– 分牌用//////////////////////////// User::User() {curric=13; for(int i=0;i<13;i++) { strcopy(U_pai[i],pai[pp]); pp++; } strcopy(U_pai[13]," \0"); } /////////////打印摸牌/////////// void User::print_pai() { char str[8]; strcpy(str,U_pai[13]); strcat(str,"|"); std_output_handle(58,20,str,240); } ////////////////////////排序函数///////////////////////////////// void User::index_pai() { char temp[8]; for(int i=0;i<13;i++) for(int j=i+1;j<13;j++) if(U_pai[j][6]<U_pai[i][6] || U_pai[j][6]==U_pai[i][6] && U_pai[j][7]<U_pai[i][7]) {strcopy(temp,U_pai[j]);strcopy(U_pai[j],U_pai[i]);strcopy(U_pai[i],temp);} } ////////////////////////摸牌函数//////////////////////////////////////// void User::accept_pai() { strcopy(U_pai[13],pai[pp]); pp++; if(win_pai(U_pai[13][6],U_pai[13][7]))p_win=player+1; } ////////////////////////出牌函数/////////////////////////////////////// void User::out_pai(int num) { char str[8]; strcpy(str,U_pai[num]); strcat(str,"|"); strcopy(chi_pai,U_pai[num]); std_output_handle(10+po%40,5+po/40,str,0×70); po+=4; std_output_handle(58,20," ",0x0F); for(int i=num;i<13;i++) strcopy(U_pai[i],U_pai[i+1]); strcopy(U_pai[13],"\0"); } ////////////////移动选中牌模块 ///////////////////// void User::move_pai(char ch) { char str[8]; strcpy(str,U_pai[curric]); strcat(str,"|"); std_output_handle(7+curric*4-1,20,str,0×70); switch (ch) { case 52: if(curric<=0)curric=13;else curric–;break; case 53: out_pai(curric);index_pai();clear_user_pai();print_user_pai();accept_pai();print_pai();curric=13;pl=0;break; case 54: if(curric>=13)curric=0;else curric++;break; } strcpy(str,U_pai[curric]); strcat(str,"|"); std_output_handle(7+curric*4-1,20,str,0xF0); } ///////////////////打印用户手里边的牌////////////////////// void User::print_user_pai() { for(int i=0;i<13;i++) { char str[8]; strcpy(str,U_pai[i]); strcat(str,"|"); std_output_handle(7+i*4-1,20,str,0×70); } } //////////////////赢牌判断模块(摸牌判断-自家判断)/////////////////////////// int User::win_pai(char x,char y) { char win_pai[14][3]; for(int i=0;i<13;i++) { win_pai[i][0]=U_pai[i][6]; win_pai[i][1]=U_pai[i][7]; win_pai[i][2]=’\0’; } win_pai[13][0]=x; win_pai[13][1]=y; win_pai[13][2]=’\0’; int p=0; for( i=0;i<13;i++) { for(int j=i+1;j<14;j++) { if(win_pai[i][0] == win_pai[j][0] && win_pai[i][1] == win_pai[j][1]) if(sanlian(win_pai,i,j)) {return 1 ;} } } return 0; } /////////////////电脑出牌模块///////////////////////////////////// void User::computer_pai() { accept_pai();out_pai(rand()%14); } //////////////////玩家出牌函数//////////////////////////////////// void User::user_play_pai(int l=1) { print_user_pai(); //显示用户版区域 if(l)accept_pai(); //用户摸牌 print_pai(); //打印摸牌 pl=1; while(pl) { char ch; ch=std_input_handle(); move_pai(ch); } } ///////////////////吃牌判断模块///////////////////////////////// int User::chi_paiTF() { char win_pai[14][3],temp[2]; for(int i=0;i<13;i++) { win_pai[i][0]=U_pai[i][6]; win_pai[i][1]=U_pai[i][7]; win_pai[i][2]=’\0’; } win_pai[13][0]=chi_pai[6]; win_pai[13][1]=chi_pai[7]; for(int k=0;k<13;k++) for(int j=k+1;j<14;j++) if(win_pai[j][0]<win_pai[k][0] || win_pai[j][0]==win_pai[k][0] && win_pai[j][1]<win_pai[k][1]) {bscopy(temp,win_pai[j]);bscopy(win_pai[j],win_pai[k]);bscopy(win_pai[k],temp);} for(k=0;k<16;k++) if(win_pai[k][0] == chi_pai[6] && win_pai[k][1] == chi_pai[7]){break;} //printf("\n%d",k); if(win_pai[k-1][0] == win_pai[k][0] && win_pai[k+1][0] == win_pai[k][0]) if(win_pai[k-1][1]+1 == win_pai[k][1] && win_pai[k+1][1]-1 == win_pai[k][1]) return 1; if(win_pai[k-2][0] == win_pai[k][0]) if(win_pai[k-2][1]+2 == win_pai[k][1] && win_pai[k-1][1]+1 == win_pai[k][1]) return 1; if(win_pai[k+2][0] == win_pai[k][0]) if(win_pai[k+2][1]-2 == win_pai[k][1] && win_pai[k+1][1]-1 == win_pai[k][1]) return 1; if(win_pai[k+2][0] == win_pai[k][0]) if(win_pai[k+2][1] == win_pai[k][1] && win_pai[k+1][1] == win_pai[k][1]) return 1; return 0; } //////////////////清屏吃牌除打印//////////////////////// void chi_pai_clear() { std_output_handle(2+2*10-1,18," ",0x0F); std_output_handle(2+3*10-1,18," ",0x0F); } ///////////////////吃牌打印模块//////////////////////////////// void User::chi_pai_print() { char ch; std_output_handle(2+2*10-1,18,"吃牌【7】",0×90); std_output_handle(2+3*10-1,18,"取消【9】",0×90); ch=1; while(ch) { ch=std_input_handle(); switch (ch) { case 55:chi_pai_clear(); strcopy(U_pai[13],chi_pai); if(win_pai(U_pai[13][6],U_pai[13][7])) {p_win=1;} else {user_play_pai(0);} ch=0;break; case 57:chi_pai_clear();ch=0;break; } } }
这是我的第一个完整点的项目,大一的时候参赛写的,有点烂哈~
时间: 2024-10-16 01:49:01