void Record() //记录棋子的情况 { p->coord.x=point.x; p->coord.y=point.y; ptr=p; p=(struct Piece *)malloc(sizeof(struct Piece)); p->fore=ptr; ShowWho(); Q[point.x][point.y]=player+1; if(player) { player=0; return; } player=1; goto_xy(point.x,point.y); } void PutDown() //显示落子函数 { if(Q[point.x][point.y]==0) //先判断该位置是否有棋子 { if(player) { printf("●"); printf("\a"); //'\a'表示蜂鸣声 Record(); } else { printf("○"); printf("\a"); Record(); } goto_xy(point.x,point.y); } } void Play(char ch) //键盘的操作 移动光标 下棋和悔棋操作 { if(player==1) //甲方 { if(ch== 'W' || ch=='w') //W 光标上移 { if(point.y<=3) point.y=22; else point.y--; goto_xy(point.x,point.y); } if(ch=='A' || ch=='a') //A 光标左移 { if(point.x<=10) point.x=20; else point.x--; goto_xy(point.x,point.y); } if(ch=='D' || ch=='d') //D 光标右移 { if(point.x>=20) point.x=10; else point.x++; goto_xy(point.x,point.y); } if(ch=='S' || ch=='s') //S 光标下移 { if(point.y>=22) point.y=3; else point.y++; goto_xy(point.x,point.y); } if(ch==32) //空格键的ASCLL码 下棋 { PutDown(); } } else { if(ch==72) //↑的ASCLL码 光标上移 { if(point.y<=3) point.y=22; else point.y--; goto_xy(point.x,point.y); } if(ch==75) //←的ASCLL码 光标左移 { if(point.x<=10) point.x=20; else point.x--; goto_xy(point.x,point.y); } if(ch==77) //→的ASCLL码 光标右移 { if(point.x>=20) point.x=10; else point.x++; goto_xy(point.x,point.y); } if(ch==80) //↓的ASCLL码 光标下移 { if(point.y>=22) point.y=3; else point.y++; goto_xy(point.x,point.y); } if(ch==13) //回车键的ASCLL码 下棋 { PutDown(); } } if(button=='b'||button=='B') //悔棋的操作 { ptr1=p; if(p!=head) { p=p->fore; free(ptr1); point.x=p->coord.x; point.y=p->coord.y; go_back(point.x,point.y); } } } int Judge() { int count=0; int pp=player==0?2:1; //三目运算 for(int c=0; c<200; c++) { for(int r=0; r<200; r++) { if(Q[r][c]!=pp) continue; //检查列 int rr=r; int cc=c; while(--cc>=3 &&Q[rr][cc]==pp) count++; cc=c; while(++cc<23 &&Q[rr][cc]==pp) count++; cc=c; if(count>=4) return pp; //检查行 count=0; while(--rr>=10 &&Q[rr][cc]==pp) count++; rr=r; while(++rr<30 &&Q[rr][cc]==pp) count++; rr=r; if(count>=4) return pp; //检查反斜边 count=0; cc--; rr--; while((cc>=3||rr>=10) &&Q[rr][cc]==pp) { count++; cc--; rr--; } rr=r; cc=c; cc++; rr++; while((cc<23||rr<30) &&Q[rr][cc]==pp) { count++; cc++; rr++; } rr=r; cc=c; if(count+1>=5) return pp; //检查正斜边 count=0; cc++; rr--; while((cc<23||rr>=10) &&Q[rr][cc]==pp) { count++; cc++; rr--; } rr=r; cc=c; cc--; rr++; while((cc>=3||rr<30) &&Q[rr][cc]==pp) { count++; cc--; rr++; } rr=r; cc=c; if(count+1>=5) return pp; count=0; } } return 0; } int main(void) { int j=0,k=0; system("color 4E"); //#include 4 背景红色 E 字体淡黄色 printf("\n 欢迎play五子棋!"); goto_xy(15,3); printf("作者:杜盼"); goto_xy(17,5); printf(" 周拓"); Sleep(3000); system("cls"); while(1) { Init(); //初始化 int winner=0; Welcome(); //欢迎界面 while(1) //读取菜单选项 { char choice=getch(); menu_choose(choice); if(startchoice!=0) break; } if(startchoice==1) //选择人人对战 { Draw(); goto_xy(17,24); printf("轮到甲方落子"); goto_xy(point.x,point.y); while(1) { button=getch(); Play(button); //读取键盘的操作 移动光标 下棋和悔棋操作 if(button==27) //ESC的ASCLL码 { if(MessageBox(NULL,TEXT("确定退出?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK) { system("cls"); printf("\n 谢谢使用!\n"); return 0; } } if(button==13 || button==32) //回车键或者空格键ASCLL码 按下后开始判断输赢 winner=Judge(); if(winner!=0) { goto_xy(15,26); if(winner==2) { printf("恭喜!甲方赢!\n"); j=j+1; } if(winner==1) { printf("恭喜!乙方赢!\n"); k=k+1; } goto_xy(29,24); printf("甲 %d : %d 乙",j,k); } if(winner!=1&&winner!=2) { continue; } goto_xy(13,24); printf(" 继续游戏?(Y/N): "); while(1) { button=getch(); if(button=='n'||button=='N'||button=='y'||button=='Y') break; } if(button=='n'||button=='N') { if(MessageBox(NULL,TEXT(" 确定退出?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK) { system("cls"); printf("\n 谢谢使用!\n"); return 0; } } if(button=='y'||button=='Y') { winner=0; system("cls"); break; } } } if(startchoice==2) { if(MessageBox(NULL,TEXT(" 确定退出?"),TEXT(""),MB_ICONQUESTION|MB_OKCANCEL)==IDOK) { printf("\n\n 谢谢使用!\n"); return 0; } } } return 0; }