您好,欢迎访问三七文档
#include<stdio.h>#include<string.h>#include<stdlib.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║|II|║\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;j<15;j++)for(i=0;i<15;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((a<0||a>14)||(b<0||b>14)){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((a<0||a>14)||(b<0||b>14)||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;j<15;j++)for(i=0;i<15;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((a<0||a>14)||(b<0||b>14)||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((c<0||c>14)||(d<0||d>14)||qipan[d][c]!=SPA){printf("坐标错误或该位置已有子!请重新输入:");scanf("%d%d",&c,&d);}c1=c;d1=d;x--;qipan[d][c]=COM;system("cls");d
本文标题:五子棋C语言代码
链接地址:https://www.777doc.com/doc-4924346 .html