#includestdio.h#includestring.h#includestdlib.h#defineSPA0#defineMAN1#defineCOM2/*空位置设为0,玩家下的位置设为1,电脑下的位置设为2*/intqipan[15][15];/*15*15的棋盘*/inta,b,c,d,x;/*ab为玩家下子坐标,cd为电脑下子坐标x为剩余空位置*/voidstart();/*程序的主要控制函数*/voiddraw();/*画棋盘*/intwin(intp,intq);/*判断胜利pq为判断点坐标*/voidAI(int*p,int*q);/*电脑下子pq返回下子坐标*/intvalue(intp,intq);/*计算空点pq的价值*/intqixing(intn,intp,intq);/*返回空点pq在n方向上的棋型n为1-8方向从右顺时针开始数*/voidyiwei(intn,int*i,int*j);/*在n方向上对坐标ij移位n为1-8方向从右顺时针开始数*/voidmain(){chark;do{x=225;start();printf(还要再来一把吗?输入y或n:);getchar();scanf(%c,&k);while(k!='y'&&k!='n'){printf(输入错误,请重新输入\n);scanf(%c,&k);}system(cls);}while(k=='y');printf(谢谢使用!\n);}voidstart(){inti,j,a1,b1,c1,d1,choice;/*a1b1储存玩家上手坐标c1d1储存电脑上手坐标*/charch;printf(\t╔═══════════════════════════════╗\n);printf(\t║║\n);printf(\t║欢迎使用五子棋对战程序祝您玩的愉快挑战无极限║\n);printf(\t║║\n);printf(\t║._______________________.║\n);printf(\t║|_____________________|║\n);printf(\t║|II|║\n);printf(\t║|I五子棋I|║\n);printf(\t║|II|║\n);printf(\t║|Imadeby晓之蓬I|║\n);printf(\t║|I___________________I|║\n);printf(\t║!_______________________!║\n);printf(\t║._[__________]_.║\n);printf(\t║.___|_______________|___.║\n);printf(\t║|:::____|║\n);printf(\t║|~~~~[CD-ROM]|║\n);printf(\t║!_____________________!║\n);printf(\t║║\n);printf(\t║║\n);printf(\t║寒星溪月疏星首,花残二月并白莲。║\n);printf(\t║雨月金星追黑玉,松丘新宵瑞山腥。║\n);printf(\t║星月长峡恒水流,白莲垂俏云浦岚。║\n);printf(\t║黑玉银月倚明星,斜月明月堪称朋。║\n);printf(\t║二十六局先弃二,直指游星斜彗星。║\n);printf(\t║║\n);printf(\t║║\n);printf(\t║1.人机对战2.人人对战║\n);printf(\t║║\n);printf(\t╚═══════════════════════════════╝\n);printf(\t\t\t请输入1或2:);scanf(%d,&choice);/*选择模式:人机或人人*/while(choice!=1&&choice!=2){printf(输入错误,请重新输入:);scanf(%d,&choice);}if(choice==1){/*人机模式*/system(cls);printf(欢迎使用五子棋人机对战!下子请输入坐标(如136)。悔棋请输入1515。\n\n\n);for(j=0;j15;j++)for(i=0;i15;i++)qipan[j][i]=SPA;/*置棋盘全为空*/draw();printf(先下请按1,后下请按2:);scanf(%d,&i);while(i!=1&&i!=2){printf(输入错误,请重新输入:);scanf(%d,&i);}if(i==1){/*如果玩家先手下子*/printf(请下子:);scanf(%d%d,&a,&b);while((a0||a14)||(b0||b14)){printf(坐标错误!请重新输入:);scanf(%d%d,&a,&b);}a1=a;b1=b;x--;qipan[b][a]=MAN;system(cls);draw();}while(x!=0){if(x==225){c=7;d=7;qipan[d][c]=COM;x--;system(cls);draw();}/*电脑先下就下在77*/else{AI(&c,&d);qipan[d][c]=COM;x--;system(cls);draw();}/*电脑下子*/c1=c;d1=d;/*储存电脑上手棋型*/if(win(c,d)){/*电脑赢*/printf(要悔棋吗?请输入y或n:);getchar();scanf(%c,&ch);while(ch!='y'&&ch!='n'){printf(输入错误,请重新输入:);scanf(%c,&ch);}if(ch=='n'){printf(下不过电脑很正常,请不要灰心!!!\n);return;}else{x+=2;qipan[d][c]=SPA;qipan[b1][a1]=SPA;system(cls);draw();}/*悔棋*/}printf(电脑下在%d%d\n请输入:,c,d);scanf(%d%d,&a,&b);/*玩家下子*/if(a==15&&b==15){x+=2;qipan[d][c]=SPA;qipan[b1][a1]=SPA;system(cls);draw();printf(请输入:);scanf(%d%d,&a,&b);}/*悔棋*/while((a0||a14)||(b0||b14)||qipan[b][a]!=SPA){printf(坐标错误或该位置已有子!请重新输入:);scanf(%d%d,&a,&b);}a1=a;b1=b;x--;qipan[b][a]=MAN;system(cls);draw();if(win(a,b)){printf(电脑神马的都是浮云!!!\n);return;}/*玩家赢*/}printf(和局\n);}if(choice==2){system(cls);printf(欢迎使用五子棋人人对战!下子请输入坐标(如136)。悔棋请输入1515。\n\n\n);for(j=0;j15;j++)for(i=0;i15;i++)qipan[j][i]=SPA;/*置棋盘全为空*/draw();while(x!=0){printf(1P请输入:);scanf(%d%d,&a,&b);if(a==15&&b==15){x+=2;qipan[d][c]=SPA;qipan[b1][a1]=SPA;system(cls);draw();printf(1P请输入:);scanf(%d%d,&a,&b);}while((a0||a14)||(b0||b14)||qipan[b][a]!=SPA){printf(坐标错误或该位置已有子!请重新输入:);scanf(%d%d,&a,&b);}a1=a;b1=b;x--;qipan[b][a]=MAN;system(cls);draw();printf(1P下在%d%d。\n,a,b);if(win(a,b)){printf(你真棒!!!\n);return;}/*玩家1赢*/printf(2P请输入:);scanf(%d%d,&c,&d);if(c==15&&d==15){x+=2;qipan[b][a]=SPA;qipan[d1][c1]=SPA;system(cls);draw();printf(2P请输入:);scanf(%d%d,&c,&d);}while((c0||c14)||(d0||d14)||qipan[d][c]!=SPA){printf(坐标错误或该位置已有子!请重新输入:);scanf(%d%d,&c,&d);}c1=c;d1=d;x--;qipan[d][c]=COM;system(cls);draw();printf(2P下在%d%d。\n,c,d);if(win(c,d)){printf(你真棒!!!\n);return;}/*玩家2赢*/}printf(和局\n);}}voiddraw()/*画棋盘*/{inti,j;charp[15][15][4];for(j=0;j15;j++)for(i=0;i15;i++){if(qipan[j][i]==SPA)strcpy(p[j][i],\0);if(qipan[j][i]==MAN)strcpy(p[j][i],●\0);if(qipan[j][i]==COM)strcpy(p[j][i],◎\0);}printf(01234567891011121314\n);printf(┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐\n);for(i=0,j=0;i14;i++,j++){printf(%2d│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%d\n,j,p[i][0],p[i][1],p[i][2],p[i][3],p[i][4],p[i][5],p[i][6],p[i][7],p[i][8],p[i][9],p[i][10],p[i][11],p[i][12],p[i][13],p[i][14],j);printf(├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤\n);}printf(14│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│0\n,p[14][0],p[14][1],p[14][2],p[14][3],p[14][4],p[14][5],p[14][6],p[14][7],p[14][8],p[14][9],p[14][10],p[14][11],p[14][12],p[14][13],p[14][14]);printf(└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘\n);printf(01234567891011121314\n);}intwin(intp,intq)/*判断胜利pq为判断点坐标,胜利返回1,否则返回0*/{intk,n=1,m,P,Q;/*k储存判断点pq的状态COM或MAN。PQ储存判断点坐标。n为判断方向。m为个数。*/P=p;Q=q;k=qipan[q][p];while(n!=5){m=0;while(k==qipan[q][p]){m++;if(m==5)return1;yiwei(n,&p,&q);if(p0||p14||q0||q14)break;}n+=4;m-=1;p=P;q=Q;/*转向判断*/while(k==qipan[q][p]){m++;if(m==5)return1;yiwei(n,&p,&q);if(p0||p14||q0||q14)break;}n-=3;p=P;q=Q;/*不成功则判断下一组方向*/}return0;}voidAI(int*p,int*q)/*电脑下子*p*q返回下子坐标*/{inti,j,k,max=0,I,J;/*IJ为下点坐标*/for(j=0;j