增加了一个人机对战功能
不过机器是瞎逼下的
所以人赢起来很容易
感觉智商收到了侮辱(手动捂脸)
勿喷勿喷
#include
#define SIZE 15
#define WIN 5
using namespace std;
char chessboard[SIZE][SIZE];
int heng,zong;
void prch();//打印棋盘
int menu();//游戏选择界面
int judge(char a);//判断黑子白子是否胜利
int main()
{
int p=menu();
if(p==1)//人人对战模式
{
prch();
int flag=0;
for (int i=0; i<SIZE; i++)//对奇葩内容进行初始化
for (int j=0; j<SIZE; j++)
chessboard[i][j] = ' ';
printf("请输入黑子(@)的坐标,形如:4 4: ");
while(~scanf("%d%d",&zong,&heng))
{
while (heng<1||zong<1||heng>SIZE||zong>SIZE||chessboard[zong-1][heng-1] != ' ')
{
printf("输入有误!请重新输入!\n\n");
if (flag%2==0)
printf("请输入黑子(@)坐标,形如:4 4:");
else
printf("请输入白子(O)坐标,形如:4 4:");
scanf("%d%d",&zong,&heng);
}
if (flag%2==0)
chessboard[zong - 1][heng - 1]='@';
else
chessboard[zong - 1][heng - 1]='O';
prch();
//判断哪一方获胜
if(flag%2==0)
{
if(judge('@'))
{
printf("黑棋获胜!!!!\n");
break;
}
}
else
{
if(judge('O'))
{
printf("白棋获胜!!!!\n");
break;
}
}
flag++;
if (flag%2 ==0)
printf("请输入黑子(@)坐标,形如:4 4:");
else
printf("请输入白子(O)坐标,形如:4 4:");
}
}
else if(p==2)//人机对战模式
{
prch();
for (int i=0; i<SIZE; i++)//对奇葩内容进行初始化
for (int j=0; j<SIZE; j++)
chessboard[i][j] = ' ';
printf("请输入黑子(@)的坐标,形如:4 4: ");
while(~scanf("%d%d",&zong,&heng))
{
while (heng<1||zong<1||heng>SIZE||zong>SIZE||chessboard[zong-1][heng-1] != ' ')
{
printf("输入有误!请重新输入!\n\n");
printf("请输入黑子(@)坐标,形如:4 4:");
scanf("%d%d",&zong,&heng);
}
chessboard[zong - 1][heng - 1]='@';
prch();
if(judge('@'))
{
printf("黑棋获胜!!!!\n");
break;
}
zong=rand()%15+1,heng=rand()%15+1;
chessboard[zong - 1][heng - 1]='O';
prch();
if(judge('O'))
{
printf("白棋获胜!!!!\n");
break;
}
printf("请输入黑子(@)坐标,形如:4 4:");
}
}
else//退出游戏
printf("退出游戏");
return 0;
}
void prch()//打印棋盘
{
printf(" ");
for (int i=1; i<=SIZE; i++)
printf(" %-3d",i);
printf("\n");
printf(" ___________________________________________________________\n");
for (int i=0; i<SIZE; i++)
{
if (i>=0)
printf("%-3d",i+1);
else
printf(" ");
printf("|");
for (int j=0; j<SIZE; j++)
printf(" %-2c|", chessboard[i][j]);
printf("\n ___________________________________________________________\n");
}
printf("\n\n");
}
int menu()//游戏选择界面
{
int num;
printf("************ 五子棋小游戏 ************\n");
printf("****** 1.人人对战 2.人机对战 *******\n");
printf("************ 3.退出游戏 *************\n");
printf("***************************************\n");
printf(" 您的选择(1或2)是:");
scanf("%d",&num);
return num;
}
int judge(char a)//判断黑子白子是否胜利
{
for (int i=0; i<SIZE; i++)
{
for (int j=0; j<SIZE; j++)
{
if (chessboard[i][j] == a)
{
int count_a = 1;
//竖直方向
for (int x=i-1,times=0; x>=0; x--,times++)
{
if (chessboard[x][j]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
for (int y=i+1,times=0; y<SIZE; y++,times++)
{
if (chessboard[y][j]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
if (count_a>=WIN)
{
return true;
}
//横方向
count_a=1;
for (int x=j-1,times=0; x>=0; x--,times++)
{
if (chessboard[i][x]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
for (int y=j+1,times=0; y<SIZE; y++,times++)
{
if (chessboard[i][y]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
if (count_a>=WIN)
{
return true;
}
//上斜下方向
count_a=1;
for (int x=i-1,y=j-1,times=0; i>=0&&j>=0; x--,y--,times++)
{
if (chessboard[x][y]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
for (int x=i+1,y=j+1,times=0; i<SIZE&&j<SIZE; x++,y++,times++)
{
if (chessboard[x][y]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
if (count_a>=WIN)
{
return true;
}
//下斜上方向
count_a=1;
for (int x=i+1,y=j-1,times=0; x<SIZE&&y>=0; x++,y--,times++)
{
if (chessboard[x][y]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
for (int x=i-1,y=j+1,times=0; x>=0&&y<SIZE; x--,y++,times++)
{
if (chessboard[x][y]==a)
{
count_a++;
}
else
break;
if (times>WIN-1)
break;
}
if (count_a>=WIN)
{
return true;
}
}
}
}
return false;
}