#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<bios.h> #include<conio.h>/*编译预处理,定义按键码*/ #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 /*若想在游戏中途退出, 可按 Esc 键*/ #define ESC 0x011b /*SPACE键表示落子*/ #define SPACE 0x3920/*设置偏移量*/ #define OFFSET 20 #define OFFSET_x 4 #define OFFSET_y 3 /*定义数组大小*/ #define N 19 /*定义全局变量*/ int status[N][N]; /*定义的数组,保存状态*/ int step_x,step_y;/*行走的坐标*/ int key ; /*获取按下的键盘的键*/ int flag; /*玩家标志*//*自定义函数原型*/ void DrawBoard(); void DrawCircle(int x,int y,int color); void Alternation(); void JudgePlayer(int x,int y); void Done(); int ResultCheck(int x,int y); void WelcomeInfo(); void ShowMessage();/*定义函数*/ /*显示欢迎信息函数*/ void WelcomeInfo() { char ch ; /*移动光标到指定位置*/ gotoxy(12,4); /*显示欢迎信息*/ printf("Welcome you to gobang word!"); gotoxy(12,6); printf("1.You can use the up,down,left and right key to move the chessman,"); gotoxy(12,8); printf(" and you can press Space key to enter after you move it !"); gotoxy(12,10); printf("2.You can use Esc key to exit the game too !"); gotoxy(12,12); printf("3.Don not move the pieces out of the chessboard !"); gotoxy(12,14); printf("DO you want to continue ?(Y/N)"); ch=getchar(); /*判断程序是否要继续进行*/ if(ch=='n'||ch=='N') /*如果不继续进行,则推出程序*/ exit(0); }/*画棋盘函数*/ void DrawBoard() { int x1,x2; int y1,y2; /*设置背景色*/ setbkcolor(2); /*设置线条颜色*/ setcolor(1); /*设置线条风格、宽度*/ setlinestyle(DOTTED_LINE,1,1); /*按照预设的偏移量开始画棋盘*/ for(x1=1,y1=1,y2=18;x1<=18;x1++) line((x1+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET,(x1+OFFSET_x)*OFFSET,(y2+OFFSET_y)*OFFSET); for(x1=1,y1=1,x2=18;y1<=18;y1++) line((x1+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET,(x2+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET); /*将各个点的状态设置为0*/ for(x1=1;x1<=18;x1++) for(y1=1;y1<=18;y1++) status[x1][y1]=0; /*显示帮助信息*/ setcolor(14); /*设置字体、大小*/ settextstyle(1,0,1); outtextxy((19+OFFSET_x)*OFFSET,(2+OFFSET_y)*OFFSET,"Player key:"); setcolor(9); settextstyle(3,0,1); outtextxy((19+OFFSET_x)*OFFSET,(4+OFFSET_y)*OFFSET,"UP--up "); outtextxy((19+OFFSET_x)*OFFSET,(6+OFFSET_y)*OFFSET,"DOWN--down "); outtextxy((19+OFFSET_x)*OFFSET,(8+OFFSET_y)*OFFSET,"LEFT--left"); outtextxy((19+OFFSET_x)*OFFSET,(10+OFFSET_y)*OFFSET,"RIGHT--right"); outtextxy((19+OFFSET_x)*OFFSET,(12+OFFSET_y)*OFFSET,"ENTER--space"); setcolor(14); settextstyle(1,0,1); outtextxy((19+OFFSET_x)*OFFSET,(14+OFFSET_y)*OFFSET,"Exit:"); setcolor(9); settextstyle(3,0,1); outtextxy((19+OFFSET_x)*OFFSET,(16+OFFSET_y)*OFFSET,"ESC"); }/*画圆函数*/ void DrawCircle(int x,int y,int color) { setcolor(color); /*设置画圆线条的风格,宽度,这里设置为虚线*/ setlinestyle(SOLID_LINE,0,1); x=(x+OFFSET_x)*OFFSET; y=(y+OFFSET_y)*OFFSET; /*以(x,y)为圆心,8为半径画圆*/ circle(x,y,8); } /*交换行棋方函数*/ void Alternation() { if(flag==1) flag=2 ; else flag=1 ; } /*对不同的行棋方画不同颜色的圆函数*/ void JudgePlayer(int x,int y) { if(flag==1) DrawCircle(x,y,15); if(flag==2) DrawCircle(x,y,4); }/*判断当前行棋方是否获胜函数*/ int ResultCheck(int x,int y) { int j,k; int n1,n2 ; while(1) { /*对水平方向进行判断是否有5个同色的圆*/ n1=0; n2=0; /*水平向左数*/ for(j=x,k=y;j>=1;j--) { if(status[j][k]==flag) n1++; else break; } /*水平向右数*/ for(j=x,k=y;j<=18;j++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } /*对垂直方向进行判断是否有5个同色的圆*/ n1=0; n2=0; /*垂直向上数*/ for(j=x,k=y;k>=1;k--) { if(status[j][k]==flag) n1++; else break ; } /*垂直向下数*/ for(j=x,k=y;k<=18;k++) { if(status[j][k]==flag) n2++; else break ; } if(n1+n2-1>=5) { return(1); } /*从左上方到右下方进行判断是否有5个同色的圆*/ n1=0; n2=0; /*向左上方数*/ for(j=x,k=y;(j>=1)&&(k>=1);j--,k--) { if(status[j][k]==flag) n1++; else break; } /*向右下方数*/ for(j=x,k=y;(j<=18)&&(k<=18);j++,k++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } /*从右上方到左下方进行判断是否有5个同色的圆*/ n1=0; n2=0;
http://www.huihui.cn/share/32085516
http://www.huihui.cn/share/32086062
http://www.huihui.cn/share/32086460
http://www.huihui.cn/share/32086902
http://www.huihui.cn/share/32087540
http://www.huihui.cn/share/32088042
http://www.huihui.cn/share/32088540
http://www.huihui.cn/share/32088980
http://www.huihui.cn/share/32089946
http://www.huihui.cn/share/32090314
http://www.huihui.cn/share/32091338
http://www.huihui.cn/share/32091870
http://www.huihui.cn/share/32092320
http://www.huihui.cn/share/32092780
http://www.huihui.cn/share/32093232
http://www.huihui.cn/share/32093726
http://www.huihui.cn/share/32094168
http://www.huihui.cn/share/32094774
http://www.huihui.cn/share/32095230
http://www.huihui.cn/share/32095744
http://www.huihui.cn/share/32096204
http://www.huihui.cn/share/32096586
http://www.huihui.cn/share/32097012
http://www.huihui.cn/share/32097454
http://www.huihui.cn/share/32097908
http://www.huihui.cn/share/32098344
http://www.huihui.cn/share/32098902
http://www.huihui.cn/share/32099278
http://www.huihui.cn/share/32099690
http://www.huihui.cn/share/32100098
http://www.huihui.cn/share/32100502
http://www.huihui.cn/share/32101128
http://www.huihui.cn/share/32101556
http://www.huihui.cn/share/32102210
http://www.huihui.cn/share/32103274
http://www.huihui.cn/share/32103798
http://www.huihui.cn/share/32104280
http://www.huihui.cn/share/32104786
http://www.huihui.cn/share/32105234
http://www.huihui.cn/share/32105826
http://www.huihui.cn/share/32106354
http://www.huihui.cn/share/32106792
http://www.huihui.cn/share/32107316
http://www.huihui.cn/share/32107962
http://www.huihui.cn/share/32108410
http://www.huihui.cn/share/32108974
http://www.huihui.cn/share/32109544
http://www.huihui.cn/share/32110134
http://www.huihui.cn/share/32110740
http://www.huihui.cn/share/32111290
http://www.huihui.cn/share/32111862
http://www.huihui.cn/share/32112468
http://www.huihui.cn/share/32114722
http://www.huihui.cn/share/32117666
http://www.huihui.cn/share/32118256
http://www.huihui.cn/share/32120098
http://www.huihui.cn/share/32120644
http://www.huihui.cn/share/32121382
http://www.huihui.cn/share/32125100
http://www.huihui.cn/share/32125730
http://www.huihui.cn/share/32126162
http://www.huihui.cn/share/32126698
http://www.huihui.cn/share/32151652
http://www.huihui.cn/share/32152160
http://www.huihui.cn/share/32153714
http://www.huihui.cn/share/32154340
http://www.huihui.cn/share/32155058
http://www.huihui.cn/share/32159676
http://www.huihui.cn/share/32172238
http://www.huihui.cn/share/32173152
http://www.huihui.cn/share/32173782
http://www.huihui.cn/share/32174352
http://www.huihui.cn/share/32174920
http://www.huihui.cn/share/32175576
http://www.huihui.cn/share/32176140
http://www.huihui.cn/share/32176780
http://www.huihui.cn/share/32177318
http://www.huihui.cn/share/32177858
http://www.huihui.cn/share/32178394
http://www.huihui.cn/share/32179108
http://www.huihui.cn/share/32187636
http://www.huihui.cn/share/32188162
http://www.huihui.cn/share/32193002
http://www.huihui.cn/share/32198494
http://www.huihui.cn/share/32199012
http://www.huihui.cn/share/32200718
http://www.huihui.cn/share/32201590
http://www.huihui.cn/share/32202668
http://www.huihui.cn/share/32204074
http://www.huihui.cn/share/32206528
http://www.huihui.cn/share/32208680
http://www.huihui.cn/share/32209208
http://www.huihui.cn/share/32209734
http://www.huihui.cn/share/32225096
http://www.huihui.cn/share/32225524
http://www.huihui.cn/share/32226046
http://www.huihui.cn/share/32226410
http://www.huihui.cn/share/32226832
http://www.huihui.cn/share/32227310
http://www.huihui.cn/share/32227720
http://www.huihui.cn/share/32228310
http://www.huihui.cn/share/32229516
http://www.huihui.cn/share/32230042
http://www.huihui.cn/share/32230516
http://www.huihui.cn/share/32230886
http://www.huihui.cn/share/32231390
http://www.huihui.cn/share/32231776
http://www.huihui.cn/share/32232158
http://www.huihui.cn/share/32232526
http://www.huihui.cn/share/32232914
http://www.huihui.cn/share/32233276
http://www.huihui.cn/share/32233754
http://www.huihui.cn/share/32234242
http://www.huihui.cn/share/32234714
http://www.huihui.cn/share/32235140
http://www.huihui.cn/share/32235714
http://www.huihui.cn/share/32236134
http://www.huihui.cn/share/32236642
http://www.huihui.cn/share/32237682
http://www.huihui.cn/share/32241870
http://www.huihui.cn/share/32242318
http://www.huihui.cn/share/32242972
http://www.huihui.cn/share/32244090
http://www.huihui.cn/share/32244552
/*向右上方数*/ for(j=x,k=y;(j<=18)&&(k>=1);j++,k--) { if(status[j][k]==flag) n1++; else break; } /*向左下方数*/ for(j=x,k=y;(j>=1)&&(k<=18);j--,k++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } return(0); } }