【文件属性】:
文件名称:用C语言编写的简单五子棋游戏
文件大小:13KB
文件格式:CPP
更新时间:2014-05-14 16:22:07
五子棋
#include
#include
#define N 20
void welcome();
void initqipan();
void showqi(int i);
void save(int p);
void panduan(int p);
void heqi();
void over();
int zouqihang();
int zouqilie();
/******************结构体*****************/
struct zuobiao
{
int x[N*N];
int y[N*N];
}weizhi[N*N];
/******************主函数*****************/
void main()
{
int p=0;
welcome();
initqipan();
for(p=1;p<=N*N;p++)
{
if(p%2==0)
weizhi[p].x[p]=weizhi[p-1].x[p-1];
else
weizhi[p].x[p]=zouqihang();
if(p%2==0)
weizhi[p].y[p]=weizhi[p-1].y[p-1]+1;
else
weizhi[p].y[p]=zouqilie();
save(p);
showqi(p);
panduan(p);
}
if(p==N*N)
heqi();
over();
}
/******************建立棋盘*****************/
void initqipan()
{char f[N]={' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s'};
int i,j;
for(i=0;ip)printf("·");
else continue;
}
printf("\n");
}
}
/******************走棋行*****************/
int zouqihang()
{
int x;
printf("请输入要走棋子所在行数!\n");
printf("x=");
scanf("%d",&x);
while(x>N-1||x<1)
{
printf("error!\n");
printf("请输入要走棋子所在行数!\n");
printf("x=");
scanf("%d",&x);
}
return x;
}
/******************走棋列*****************/
int zouqilie()
{
int y;
printf("请输入要走棋子所在列数!\n");
printf("y=");
scanf("%d",&y);
while(y>N-1||y<1)
{
printf("error!\n");
printf("请输入要走棋子所在列数!\n");
printf("y=");
scanf("%d",&y);
}
return y;
}
/******************文件保存*****************/
void save(int i)
{
FILE *fp;
static int m=0;
fp=fopen("wuzi_list","wb");
fwrite(&weizhi[i],sizeof(struct zuobiao),1,fp);
}
/****************判断输赢*******************/
void panduan(int p)
{
int i,j,k[8]={1,1,1,1,1,1,1,1,};
int a[N*N],b[N*N];
FILE *fp;
fp=fopen("wuzi_list","rb");
for(i=1;i<=p;i++)
{
fread(&weizhi[i],sizeof(struct zuobiao),1,fp);
a[i]=weizhi[i].x[i];
b[i]=weizhi[i].y[i];
}
/*****************判断行******************/
for(i=1;i<=p;i++)
{
if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[0]++;
continue;
}
else if(k[0]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[0]==5)
break;
k[0]=1;
}
else if(k[0]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[1]++;
continue;
}
else if(k[1]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[1]==5)
break;
k[1]=1;
}
}
/**********************判断列************************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if(k[2]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[2]==5)
break;
k[2]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if(k[3]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[3]==5)
break;
k[3]=1;
}
}
/****************判断对角(左上-右下)******************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[4]++;
continue;
}
else if(k[4]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[4]==5)
break;
k[4]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[5]++;
continue;
}
else if(k[5]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[5]==5)
break;
k[5]=1;
}
}
/**********判断对角(左下-右上)************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5||k[4]==5||k[5]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[6]++;
continue;
}
else if(k[6]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[6]==5)
break;
k[6]=1;
}
else if(k[6]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[7]++;
continue;
}
else if(k[7]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[7]==5)
break;
k[7]=1;
}
}
}
/****************和棋*******************/
void heqi()
{
printf("************************************\n");
printf(" Tie!!!\n");
printf("************************************\n");
}
/****************游戏结束*******************/
void over()
{
printf("************************************\n");
printf(" game over!!!\n");
printf("************************************\n");
}
/****************游戏开始*******************/
void welcome()
{
printf("************************************\n");
printf(" Welcome!!!\n");
printf("************************************\n");
}