求五子棋的C++代码

2024年11月17日 10:04
有2个网友回答
网友(1):

有c语言的,想要可以给你,c++也兼容c语言

#include
#include
#define M 10
#define N 10

void print(int num)
{
switch (num)
{
case 1:printf("┏");break;
case 2:printf("┳");break;
case 3:printf("┓");break;
case 4:printf("┣");break;
case 5:printf("╋");break;
case 6:printf("┫");break;
case 7:printf("┗");break;
case 8:printf("┻");break;
case 9:printf("┛");break;
case 10:printf("●");break;
case 11:printf("○");break;
default : printf("☆");
}
}

void printAll(int **pan,int m,int n)
{
/*
printf("┏━━━━━━━━━━━━━\n");
printf("┃\n");
printf("┃ 五子棋\n");
printf("┃\n");
printf("┗━━━━━━━━━━━━━\n");
*/
int i,j;
printf(" ");
for(j=0;j {
printf("%2d",j);
}
printf("\n");
for(i=0;i {
printf("%2d",i);
for(j=0;j {
print(pan[i][j]);
}
printf("\n");
}
}

int cheakTie(int **pan,int m,int n,int NO) //平局返回1
{
int i,j;
int flag=1;
for(i=0;i {
for(j=0;j {
if(pan[i][j]!=NO && pan[i][j+1]!=NO && pan[i][j+2]!=NO && pan[i][j+3]!=NO && pan[i][j+4]!=NO)
{
//printf("%d\n",NO);
flag=-1;
return flag;
}
}
}
for(i=0;i {
for(j=0;j {
if(pan[i][j]!=NO && pan[i+1][j]!=NO && pan[i+2][j]!=NO && pan[i+3][j]!=NO && pan[i+4][j]!=NO)
{
flag=-1;
return flag;
}
}
}
for(i=0;i {
for(j=0;j {
if(pan[i][j]!=NO && pan[i+1][j+1]!=NO && pan[i+2][j+2]!=NO && pan[i+3][j+3]!=NO && pan[i+4][j+4]!=NO)
{
flag=-1;
return flag;
}
}
}
//printf("%d: %d\n",NO,1);
return 1;
}

int checkWin(int **pan,int m,int n)
{
int i,j;
int flag=3;

if(cheakTie(pan,m,n,10)==1)
{
//printf("22");
return flag;
}

if(cheakTie(pan,m,n,11)==1)
{
//printf("33");
return flag;
}

for(i=0;i {
for(j=0;j {
if(pan[i][j]==10 && pan[i][j+1]==10 && pan[i][j+2]==10 && pan[i][j+3]==10 && pan[i][j+4]==10)
{
flag=-1;
return flag;
}
}
}
for(i=0;i {
for(j=0;j {
if(pan[i][j]==10 && pan[i+1][j]==10 && pan[i+2][j]==10 && pan[i+3][j]==10 && pan[i+4][j]==10)
{
flag=-1;
return flag;
}
}
}
for(i=0;i {
for(j=0;j {
if(pan[i][j]==10 && pan[i+1][j+1]==10 && pan[i+2][j+2]==10 && pan[i+3][j+3]==10 && pan[i+4][j+4]==10)
{
flag=-1;
return flag;
}
}
}

for(i=0;i {
for(j=0;j {
if(pan[i][j]==11 && pan[i][j+1]==11 && pan[i][j+2]==11 && pan[i][j+3]==11 && pan[i][j+4]==11)
{
flag=1;
return flag;
}
}
}

for(i=0;i {
for(j=0;j {
if(pan[i][j]==11 && pan[i+1][j]==11 && pan[i+2][j]==11 && pan[i+3][j]==11 && pan[i+4][j]==11)
{
flag=1;
return flag;
}
}
}
for(i=0;i {
for(j=0;j {
if(pan[i][j]==11 && pan[i+1][j+1]==11 && pan[i+2][j+2]==11 && pan[i+3][j+3]==11 && pan[i+4][j+4]==11)
{
flag=1;
return flag;
}
}
}
return flag;
}

int main()
{
int **pan;
int i,j;
pan=(int **)malloc(M*sizeof(int *));
for(i=0;i {
pan[i]=(int *)malloc(N*sizeof(int));
}

/*
int pan[10][10]={{1,2,2,2,2,2,2,2,2,3},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,6},
{7,8,8,8,8,8,8,8,8,9}};

*/
for(i=0;i {
for(j=0;j {
pan[i][j]=5;
}
}

for(j=0;j {
pan[0][j]=2;
}

for(i=0;i {
pan[i][0]=4;
}

for(j=0;j {
pan[M-1][j]=8;
}

for(i=0;i {
pan[i][N-1]=6;
}

pan[0][0]=1;
pan[0][N-1]=3;
pan[M-1][0]=7;
pan[M-1][N-1]=9;

int n=9;
while(n!=1 && n!=2)
{
printf("请选择先手:1.白子 2.黑子\n");
scanf("%d",&n);
if(n!=1 && n!=2)
{
printf("输入错误,请重新输入\n");
}
}

int x,y;
while(1)
{
if( checkWin(pan,M,N) == 1 )
{
printf("恭喜白方胜利\n");
system("pause");
break;
}
else if( checkWin(pan,M,N) == -1 )
{
printf("恭喜黑方胜利\n");
system("pause");
break;
}
else if( checkWin(pan,M,N) == 0 )
{
printf("平局\n");
system("pause");
break;
}

if(n%2==1)
{
printf("请输入白子坐标,以逗号隔开: ");
scanf("%d,%d",&x,&y);
}
else if(n%2==0)
{
printf("请输入黑子坐标,以逗号隔开: ");
scanf("%d,%d",&x,&y);
}

if(pan[x][y]==10 || pan[x][y]==11)
{
printf("此位置已经有子,请重新输入!\n");
continue;
}

if(n%2==1)
{
pan[x][y]=10;
}
if(n%2==0)
{
pan[x][y]=11;
}

n++;
system("cls");
printAll(pan,M,N);
}
free(pan);
return 0;
}

网友(2):

我这里有vb和matlab的,你多少给点分呀