博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++小游戏——井字棋
阅读量:5283 次
发布时间:2019-06-14

本文共 3989 字,大约阅读时间需要 13 分钟。

#include
#include
#include
int main(){srand(time(NULL));HANDLE hOut; hOut=GetStdHandle(STD_OUTPUT_HANDLE);int a[7][7],b[7][7],i,j,n=9,xa,ya,xb,yb,c=0,d=0,k,l,m;printf("请输入两个数字,中间用空格隔开,表示第几行第几列 ");Sleep(3500);COORD pos={0,0}; SetConsoleCursorPosition(hOut,pos); printf(" ");SetConsoleCursorPosition(hOut,pos); printf("人机对战按1,人人对战按2 "); scanf("%d",&k);SetConsoleCursorPosition(hOut,pos); printf(" ");SetConsoleCursorPosition(hOut,pos); if(k==1) {printf("你先手按1,电脑先手按2 "); scanf("%d",&l); if(k==2) l=2;SetConsoleCursorPosition(hOut,pos); printf(" ");SetConsoleCursorPosition(hOut,pos); }printf("\n┌─┬─┬─┐\n\n");printf("├─┼─┼─┤\n\n");printf("├─┼─┼─┤\n\n");printf("└─┴─┴─┘\n\n");for(i=0;i<=6;i++) for(j=0;j<=6;j++) {a[i][j]=b[i][j]=0;}while(n>=1){while(l!=2){AA:COORD posA={0,9}; SetConsoleCursorPosition(hOut,posA); printf(" ");SetConsoleCursorPosition(hOut,posA);SetConsoleTextAttribute(hOut,0x02|0x05);if(k==1) printf("请你下子:"); if(k==2) printf("1号下子:"); scanf("%d %d",&xa,&ya);if(a[xa+1][ya+1]!=0||b[xa+1][ya+1]!=0||(xa>3)||(ya>3)){COORD posa={0,11}; SetConsoleCursorPosition(hOut,posa); printf("不存在此格或此格已有棋子\n"); goto AA; }COORD posa={0,11}; SetConsoleCursorPosition(hOut,posa); printf(" ");COORD pos1={3*ya,2*xa}; SetConsoleCursorPosition(hOut,pos1); SetConsoleTextAttribute(hOut,0x02|0x09);printf("#");if(xa<=3) a[xa+4][ya+1]=1; if(ya<=3) a[xa+1][ya+4]=1;if(xa<=3&&ya<=3) a[xa+4][ya+4]=1;if(xa+ya==4&&ya>=2) a[xa+4][ya-2]=1;a[xa+1][ya+1]=1;b[xa+1][ya+1]=0;n--;for(i=2;i<=4;i++) for(j=2;j<=4;j++) {if((a[i][j]+a[i][j+1]+a[i][j+2]==3)||(a[i][j]+a[i+1][j]+a[i+2][j]==3)||(a[4][4]+a[2][2]+a[3][3]==3)||a[2][4]+a[3][3]+a[4][2]==3){c++; break;}}l=2;} if(c!=0||d!=0||n==0) break;BB:COORD posB={0,10};SetConsoleCursorPosition(hOut,posB);printf(" ");SetConsoleCursorPosition(hOut,posB);SetConsoleTextAttribute(hOut,0x02|0x05);if(k==1){ printf("电脑下子:");m=2;for(i=2;i<=4;i++){if((b[i][6-i]+b[i+1][5-i]+b[i+2][4-i]==2)&&a[i][6-i]==0&&b[i][6-i]==0) {xb=i-1; yb=5-i; m=1; break;} else m=0;if((b[i][i]+b[i+1][i+1]+b[i+2][i+2]==2)&&a[i][i]==0&&b[i][i]==0) {xb=i-1; yb=i-1; m=1; break;} else m=2;for(j=2;j<=4;j++){if((b[i][j]+b[i][j+1]+b[i][j+2]==2)&&a[i][j]==0&&b[i][j]==0) {xb=i-1; yb=i-1; m=1; break;} else{if((b[i][j]+b[i+1][j]+b[i+2][j]==2)&&a[i][j]==0&&b[i][j]==0) {xb=i-1; yb=i-1; m=1; break;} else m=2;}}}if(m==2){for(i=2;i<=4;i++){if((a[i][6-i]+a[i+1][5-i]+a[i+2][4-i]==2)&&a[i][6-i]==0&&b[i][6-i]==0) {xb=i-1; yb=5-i; m=1; break;} else m=0;if((a[i][i]+a[i+1][i+1]+a[i+2][i+2]==2)&&a[i][i]==0&&b[i][i]==0) {xb=i-1; yb=i-1; m=1; break;} else m=0;for(j=2;j<=4;j++){if((a[i][j]+a[i][j+1]+a[i][j+2]==2)&&a[i][j]==0&&b[i][j]==0) {xb=i-1; yb=i-1; m=1; break;} else{if((a[i][j]+a[i+1][j]+a[i+2][j]==2)&&a[i][j]==0&&b[i][j]==0) {xb=i-1; yb=i-1; m=1; break;} else m=0;}}}}if(m==0) {do {xb=rand()%4+1; yb=rand()%4+1;} while(a[xb+1][yb+1]!=0||b[xb+1][yb+1]!=0);}printf("%d %d",xb,yb);}if(k==2){printf("2号下子:"); scanf("%d %d",&xb,&yb);}if(a[xb+1][yb+1]!=0||b[xb+1][yb+1]!=0||(xb>3)||(yb>3)) {COORD posb={0,11}; SetConsoleCursorPosition(hOut,posb); printf("不存在此格或此格已有棋子\n"); goto BB; }COORD posa={0,11}; SetConsoleCursorPosition(hOut,posa); printf(" ");COORD pos2={3*yb,2*xb}; SetConsoleCursorPosition(hOut,pos2); SetConsoleTextAttribute(hOut,0x02|0x07);printf("#"); b[xb+1][yb+1]=1;a[xb+1][yb+1]=0;n--;if(xb<=3) b[xb+4][yb+1]=1; if(yb<=3) b[xb+1][yb+4]=1;if(xb<=3&&yb<=3) b[xb+4][yb+4]=1;if(xb+yb==4&&yb>=2) a[xb+4][yb-2]=1;for(i=2;i<=4;i++) for(j=1;j<=4;j++) {if((b[i][j]+b[i][j+1]+b[i][j+2]==3)||(b[i][j]+b[i+1][j]+b[i+2][j]==3)||(b[4][4]+b[2][2]+b[3][3]==3)||b[3][3]+b[2][4]+b[4][2]==3){d++; break;}}l=3;if(c!=0||d!=0||n==0) break;}COORD poss={0,12}; SetConsoleCursorPosition(hOut,poss);SetConsoleTextAttribute(hOut,0x02|0x05);if(c!=0&&k==2) printf("1号赢了!");else {if(d!=0&&k==2) printf("2号赢了!"); else {if(d!=0&&k==1) printf("你输了!");else {if(c!=0&&k==1) printf("你赢了!");else printf("平局!"); }}}return 0;}

转载于:https://www.cnblogs.com/ShineEternal/p/10834343.html

你可能感兴趣的文章
SPCE061A学习笔记
查看>>
sql 函数
查看>>
hdu 2807 The Shortest Path 矩阵
查看>>
熟悉项目需求,要知道产品增删修改了哪些内容,才会更快更准确的在该项目入手。...
查看>>
JavaScript 变量
查看>>
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>
C# Stream 和 byte[] 之间的转换
查看>>