You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yankun666666/井字棋游戏最终版.cpp

395 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#define col 3
#define row 3
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
return 0;
}
int store[][3]={'_','_','_','_','_','_','_','_','_'},i,j,k,flag=1;
void map()
{
char boundary='|';
system("cls");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
printf("%c%c",boundary,store[i][j]);
if(j==2)
{
putchar(boundary);
putchar('\n');
}
}
}
}
int judge(int f=0)
{
if(store[0][0]+store[0][1]+store[0][2]==3*'X'||store[1][0]+store[1][1]+store[1][2]==3*'X'
||store[2][0]+store[2][1]+store[2][2]==3*'X'||store[0][0]+store[1][1]+store[2][2]==3*'X'
||store[2][0]+store[1][1]+store[0][2]==3*'X'||store[0][0]+store[1][0]+store[2][0]==3*'X'
||store[0][1]+store[1][1]+store[2][1]==3*'X'||store[0][2]+store[1][2]+store[2][2]==3*'X')
return 1;
else if(store[0][0]+store[0][1]+store[0][2]==3*'O'||store[1][0]+store[1][1]+store[1][2]==3*'O'
||store[2][0]+store[2][1]+store[2][2]==3*'O'||store[0][0]+store[1][1]+store[2][2]==3*'O'
||store[2][0]+store[1][1]+store[0][2]==3*'O'||store[0][0]+store[1][0]+store[2][0]==3*'O'
||store[0][1]+store[1][1]+store[2][1]==3*'O'||store[0][2]+store[1][2]+store[2][2]==3*'O')
return -1;
return f;
}
void final(int i)
{
system("cls");
map();
if(i==0)
printf("平局");
else if(i==1)
printf("你赢了");
else if(i==-1)
printf("很遗憾,你输了") ;
}
int mend(int x)
{
switch(x)
{
case 1:x=3;break;
case 3:x=1;break;
default:;
}
return x;
}
void Computer()
{
int sum,t=0,score[3][3];
for(i=0;i<col;i++)
for(j=0;j<row;j++)
score[i][j]=0;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
t+=store[i][j];
if(t==2*'O'+'_')
sum=5;
else if(t=='O'+2*'_')
sum=3;
else if(t=='X'+2*'_')
sum=4;
else if(t==3*'_')
sum=1;
else if(t==2*'X'+'_')
sum=6;
else if(t=='X'+'O'+'_')
sum=2;
switch(sum)
{
case 1:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=1;
}
break;
case 2:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=10;
}
break;
case 3:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=50;
}
break;
case 4:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=200;
}
break;
case 5:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=500;
}
break;
case 6:
for(k=0;k<3;k++)
{
if(store[i][k]=='_')
score[i][k]=1000;
}
break;
}
t=0;
}
for(j=0;j<row;j++)
{
for(i=0;i<col;i++)
t+=store[i][j];
if(t==2*'O'+'_')
sum=5;
else if(t=='O'+2*'_')
sum=3;
else if(t=='X'+2*'_')
sum=4;
else if(t==3*'_')
sum=1;
else if(t==2*'X'+'_')
sum=6;
else if(t=='X'+'O'+'_')
sum=2;
switch(sum)
{
case 1:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=1;
}
break;
case 2:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=10;
}
break;
case 3:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=50;
}
break;
case 4:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=200;
}
break;
case 5:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=500;
}
break;
case 6:
for(k=0;k<3;k++)
{
if(store[k][j]=='_')
score[k][j]+=1000;
}
break;
}
t=0;
}
t=0;
for(i=0;i<col;i++)
t+=store[i][i];
if(t==2*'O'+'_')
sum=5;
else if(t=='O'+2*'_')
sum=3;
else if(t=='X'+2*'_')
sum=4;
else if(t==3*'_')
sum=1;
else if(t==2*'X'+'_')
sum=6;
else if(t=='X'+'O'+'_')
sum=2;
switch(sum)
{
case 1:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=1;
}
break;
case 2:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=10;
}
break;
case 3:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=50;
}
break;
case 4:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=200;
}
break;
case 5:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=500;
}
break;
case 6:
for(k=0;k<3;k++)
{
if(store[k][k]=='_')
score[k][k]+=1000;
}
break;
}
t=0;
for(i=0;i<col;i++)
t+=store[i][2-i];
if(t==2*'O'+'_')
sum=5;
else if(t=='O'+2*'_')
sum=3;
else if(t=='X'+2*'_')
sum=4;
else if(t==3*'_')
sum=1;
else if(t==2*'X'+'_')
sum=6;
else if(t=='X'+'O'+'_')
sum=2;
switch(sum)
{
case 1:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=1;
}
break;
case 2:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=10;
}
break;
case 3:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=50;
}
break;
case 4:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=200;
}
break;
case 5:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=500;
}
break;
case 6:
for(k=0;k<3;k++)
{
if(store[k][2-k]=='_')
score[k][2-k]+=1000;
}
break;
}
int maxRow = 0, maxCol = 0;
for (i=0; i<col; i++)
for (j=0;j<row;j++)
{
if (score[i][j] > score[maxRow][maxCol])
{
maxRow = i;
maxCol = j;
}
}
store[maxRow][maxCol]='O';
map();
maxRow=mend(maxRow+1);
printf("电脑走的棋格行号和列号是:%d %d\n",maxRow,maxCol+1);
}
void scan()
{
int x,y;
printf("请输入要走的棋格行号和列号:");
scanf("%d%d",&x,&y);
printf("%d,%d",x,y) ;
x=mend(x);
while(x>3||x<=0||y>3||y<=0||store[x-1][y-1]!='_')
{
if(x>3||x<=0||y>3||y<=0)
printf("非法输入输入数的范围为1到3请重新输入\n");
else if (store[x-1][y-1]!='_')
printf("该棋格已被占有,请重新输入\n");
getchar();
scanf("%d%d",&x,&y);
x=mend(x);
}
store[x-1][y-1]='X';
map();
}
int main()
{
int count=0,i;
char ch;
color(10);
printf("\t这是一个井字棋游戏!\n在游戏之前请先阅读以下注意事项:\n");
printf("1.你可以选择下棋的先后顺序\n");
printf("2.您输入的第一个数是竖直方向第二个是水平方向输入的正确形式为一个数y空一个数x\n");
printf("3.本程序还有许多不足,请多多包涵!\n") ;
printf("按回车即可进入游戏-_- =_= +_+\n");
getchar();
system("cls");
color(12);
printf("是否先下y/n");
scanf("%c",&ch);
map();
if(ch=='N'||ch=='n')
goto lp;
for(count=0;count<10;count++)
{
i=judge();
if(judge())
break;
if(count==9)
{
color(0);
i=0;
break;
}
color(14);
printf("游戏状态UNLEND\n");
color(3);
scan();
lp: Computer();
count+=1;
}
color(4);
final(i);
return 0;
}