C++小游戏——井字棋

#include<cstdio>
#include<windows.h>
#include<ctime>
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;
}
posted @ 2018-10-04 20:44  vercont  阅读(472)  评论(0编辑  收藏  举报