这是我大一做的一个课程设计
目前阶段只有人人对战
之后还想做人机对战,并且使机器只能一点
现阶段略微有些弱智
勿喷勿喷
#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()
{
/*if(menu()==2)
goto end;*/
menu();
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:");
}
/*end:
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("**************************************\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;
}