|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <conio.h>
|
|
|
#include <windows.h>
|
|
|
#define N 20
|
|
|
#define OK 1
|
|
|
#define NO 0
|
|
|
int map[N][N]={};
|
|
|
int whoturn = 0;
|
|
|
int lhdajy=0;
|
|
|
int dgnb=0,jypl=0;
|
|
|
int *pn=&dgnb,*pk=&jypl;
|
|
|
int bianguang[20][20];
|
|
|
void initgame(); //进入游戏函数
|
|
|
int playchess();
|
|
|
void printfchessmap(); //棋表函数
|
|
|
int judge(int x, int y);
|
|
|
void bangzhu(); //帮助函数
|
|
|
void gotoxy(int x, int y); //坐标函数
|
|
|
int move1(int (*)[N],int *px,int *py);
|
|
|
int move2(int (*a)[N],int*pg,int *pm);
|
|
|
void SetPos(int x,int y);
|
|
|
void addstu(int *pq,int *pb);
|
|
|
int main()
|
|
|
{
|
|
|
char flag='s';
|
|
|
initgame();
|
|
|
|
|
|
while(flag=='s')
|
|
|
{
|
|
|
playchess();
|
|
|
whoturn++;
|
|
|
if(lhdajy==1)
|
|
|
{
|
|
|
addstu(pn,pk);
|
|
|
flag = getch(); //输入s建继续
|
|
|
lhdajy=0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
void initgame()
|
|
|
{
|
|
|
char choice;
|
|
|
printf("是否进入游戏(Y/N)或者帮助(Z)");
|
|
|
choice = getchar();
|
|
|
if (choice != 'y' && choice != 'Y'&& choice!='z'&& choice!='Z') //注意这里的&&,不要写成||
|
|
|
exit(0);
|
|
|
//getchar();
|
|
|
if(choice=='z'||choice=='Z')
|
|
|
{
|
|
|
printf("/n");
|
|
|
system("cls");
|
|
|
bangzhu();
|
|
|
system("pause");
|
|
|
}
|
|
|
printf("\n");
|
|
|
system("cls");
|
|
|
printfchessmap();
|
|
|
}
|
|
|
|
|
|
void printfchessmap()
|
|
|
{
|
|
|
int i, j;
|
|
|
FILE *fp=NULL;
|
|
|
int q=0,b=0;
|
|
|
|
|
|
for (i = 0; i < N; i++)
|
|
|
{
|
|
|
for (j = 0; j < N; j++)
|
|
|
{
|
|
|
if (i == 0)
|
|
|
printf("%3d", j);
|
|
|
else if (j == 0)
|
|
|
printf("%3d", i);
|
|
|
else if (1 == map[i][j])
|
|
|
printf("%3c", 'O');
|
|
|
else if (2 == map[i][j])
|
|
|
printf("%3c", 'X');
|
|
|
else
|
|
|
printf("%3c", '*');
|
|
|
}
|
|
|
printf("\n");
|
|
|
}
|
|
|
for(i=0;i<10;i++)
|
|
|
{
|
|
|
for(j=0;j<15;j++)
|
|
|
{
|
|
|
if(i==0||j==0||i==9||j==14)
|
|
|
printf("%2c",'+');
|
|
|
else
|
|
|
printf("%2c",' ');
|
|
|
}
|
|
|
printf("\n");
|
|
|
}
|
|
|
SetPos(10,25);
|
|
|
|
|
|
printf("玩家一得分:%d\n",dgnb);
|
|
|
printf("%3c",' ');
|
|
|
printf("玩家二得分:%d\n",jypl);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
int playchess()
|
|
|
{
|
|
|
int x,y,i,j,sum=0,sum1=0;
|
|
|
int *p=&x,*q=&y;
|
|
|
|
|
|
if (0 == whoturn % 2)
|
|
|
{
|
|
|
printf("现在轮到玩家1,请落子:");
|
|
|
for(i=0;i<20;i++)
|
|
|
for(j=0;j<20;j++)
|
|
|
sum1+=map[i][j];
|
|
|
move1(map,p,q);
|
|
|
for(i=0;i<20;i++)
|
|
|
for(j=0;j<20;j++)
|
|
|
sum+=map[i][j];
|
|
|
if(sum==sum1)
|
|
|
map[9][9]=1;
|
|
|
if(sum==sum1-1)
|
|
|
map[9][9]=2;
|
|
|
}
|
|
|
else if (1 == whoturn % 2)
|
|
|
{
|
|
|
printf("现在轮到玩家2,请落子:");
|
|
|
for(i=0;i<20;i++)
|
|
|
for(j=0;j<20;j++)
|
|
|
sum1+=map[i][j];
|
|
|
move2(map,p,q);
|
|
|
for(i=0;i<20;i++)
|
|
|
for(j=0;j<20;j++)
|
|
|
sum+=map[i][j];
|
|
|
if(sum%3==1)
|
|
|
map[9][9]=2;
|
|
|
if(sum==sum1+1)
|
|
|
map[9][9]=1;
|
|
|
}
|
|
|
|
|
|
system("cls");
|
|
|
printfchessmap();
|
|
|
if (judge(x, y)){
|
|
|
printf("玩家%d获胜\n",1 + whoturn % 2);
|
|
|
lhdajy=1;
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int judge(int x, int y)
|
|
|
{
|
|
|
int i, j;
|
|
|
int count;
|
|
|
int winflag = 1; //第一个点不用再次读取
|
|
|
int cur; //记录当前所下的棋
|
|
|
cur = map[x][y] == 1 ? 1 : 2;
|
|
|
printf("cur = %d", cur);
|
|
|
//水平方向判断
|
|
|
count = 0;
|
|
|
for (i = x, j = y - 1; j > 0 && count++ < 5; j--)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
count = 0;
|
|
|
for (i = x, j = y + 1; j < N && count++ < 5; j++)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
if (winflag >= 5)
|
|
|
return OK;
|
|
|
else
|
|
|
winflag = 1;
|
|
|
//垂直方向判断
|
|
|
count = 0;
|
|
|
for (i = x - 1, j = y; i > 0 && count++ < 5; i--)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
count = 0;
|
|
|
for (i = x + 1, j = y; i < N && count++ < 5; i++)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
if (winflag >= 5)
|
|
|
return OK;
|
|
|
else
|
|
|
winflag = 1;
|
|
|
//主对角线判断
|
|
|
count = 0;
|
|
|
for (i = x - 1, j = y - 1; i > 0 && j > 0 && count++ < 5; j--, i--)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
count = 0;
|
|
|
for (i = x + 1, j = y + 1; i < N && j < N && count++ < 5; j++, i++)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
if (winflag >= 5)
|
|
|
return OK;
|
|
|
else
|
|
|
winflag = 1;
|
|
|
//斜对角线判断
|
|
|
count = 0;
|
|
|
for (i = x + 1, j = y - 1; x < N && j > 0 && count++ < 5; j--, i++)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
count = 0;
|
|
|
for (i = x - 1, j = y + 1; i > 0 && j < N && count++ < 5; j++, i--)
|
|
|
{
|
|
|
if (map[i][j] == cur)
|
|
|
{
|
|
|
winflag++;
|
|
|
}
|
|
|
else
|
|
|
break;
|
|
|
}
|
|
|
if (winflag >= 5)
|
|
|
return OK;
|
|
|
else
|
|
|
winflag = 1;
|
|
|
|
|
|
return NO;
|
|
|
}
|
|
|
|
|
|
void bangzhu()
|
|
|
{
|
|
|
int i,j;
|
|
|
for(i=0;i<15;i++)
|
|
|
{
|
|
|
for(j=0;j<15;j++)
|
|
|
{
|
|
|
if(i==0||j==0||i==14||j==14)
|
|
|
printf("%3c",'+');
|
|
|
else if(i==4&&j==5)
|
|
|
printf("%3c",'1');
|
|
|
else if(i==4&&j==6)
|
|
|
printf("%3c",'w');
|
|
|
else if(i==4&&j==7)
|
|
|
printf("%3c",'a');
|
|
|
else if(i==4&&j==8)
|
|
|
printf("%3c",'s');
|
|
|
else if(i==4&&j==9)
|
|
|
printf("%3c",'d');
|
|
|
else if(i==5&&j==5)
|
|
|
printf("%3c",'2');
|
|
|
else if(i==5&&j==6)
|
|
|
printf(" ↑");
|
|
|
else if(i==5&&j==7)
|
|
|
printf(" ←");
|
|
|
else if(i==5&&j==8)
|
|
|
printf(" ↓");
|
|
|
else if(i==5&&j==9)
|
|
|
printf(" →");
|
|
|
else
|
|
|
printf("%3c",' ');
|
|
|
}
|
|
|
printf("\n");
|
|
|
}
|
|
|
}
|
|
|
int move1(int (*a)[N],int *px,int *py) //键盘操控
|
|
|
{
|
|
|
char move1;
|
|
|
int hx,hy,i,n;
|
|
|
hx=9;
|
|
|
hy=9;
|
|
|
while(1)
|
|
|
{
|
|
|
move1=getch();
|
|
|
n=1;
|
|
|
if(move1=='d')//右移
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx][hy+i]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hy+=n;
|
|
|
if(hy>19)
|
|
|
{
|
|
|
hy=19;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=1;
|
|
|
map[hx][hy-n]=0;
|
|
|
}
|
|
|
if(move1=='w')//上
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx-i][hy]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hx-=n;
|
|
|
if(hx<1)
|
|
|
{
|
|
|
hx=1;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=1;
|
|
|
map[hx+n][hy]=0;
|
|
|
}
|
|
|
if(move1=='s')//下
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx+i][hy]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hx+=n;
|
|
|
if(hx>19)
|
|
|
{
|
|
|
hx=19;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=1;
|
|
|
map[hx-n][hy]=0;
|
|
|
}
|
|
|
if(move1=='a')//左
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx][hy-i]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hy-=n;
|
|
|
if(hy<1)
|
|
|
{
|
|
|
hy=1;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=1;
|
|
|
map[hx][hy+n]=0;
|
|
|
}
|
|
|
if(move1==' ')
|
|
|
{
|
|
|
if(map[hx][hy]!=0&&hx==9&&hy==9)
|
|
|
continue;
|
|
|
map[hx][hy]=1;
|
|
|
*px=hx;
|
|
|
*py=hy;
|
|
|
return map[hx][hy];
|
|
|
}
|
|
|
printf("\n");
|
|
|
system("cls");
|
|
|
printfchessmap();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int move2(int (*a)[N],int *pg,int *pm) //键盘操控
|
|
|
{
|
|
|
char move2;
|
|
|
int hx,hy,i,n;
|
|
|
hx=9;
|
|
|
hy=9;
|
|
|
while(1)
|
|
|
{
|
|
|
move2=getch();
|
|
|
n=1;
|
|
|
if(move2==77)//右移
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx][hy+i]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hy+=n;
|
|
|
if(hy>19)
|
|
|
{
|
|
|
hy=19;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=2;
|
|
|
map[hx][hy-n]=0;
|
|
|
}
|
|
|
if(move2==72)//上
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx-i][hy]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hx-=n;
|
|
|
if(hx<1)
|
|
|
{
|
|
|
hx=1;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=2;
|
|
|
map[hx+n][hy]=0;
|
|
|
}
|
|
|
if(move2==80)//下
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx+i][hy]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hx+=n;
|
|
|
if(hx>19)
|
|
|
{
|
|
|
hx=19;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=2;
|
|
|
map[hx-n][hy]=0;
|
|
|
}
|
|
|
if(move2==75)//左
|
|
|
{
|
|
|
for(i=1;i<5;i++)
|
|
|
{
|
|
|
if(map[hx][hy-i]!=0)
|
|
|
n=i+1;
|
|
|
else break;
|
|
|
}
|
|
|
hy-=n;
|
|
|
if(hy<1)
|
|
|
{
|
|
|
hy=1;
|
|
|
continue;
|
|
|
}
|
|
|
map[hx][hy]=2;
|
|
|
map[hx][hy+n]=0;
|
|
|
}
|
|
|
if(move2==' ')
|
|
|
{
|
|
|
if(map[hx][hy]!=0&&hx==9&&hy==9)
|
|
|
continue;
|
|
|
map[hx][hy]=2;
|
|
|
*pg=hx;
|
|
|
*pm=hy;
|
|
|
return map[hx][hy];
|
|
|
}
|
|
|
printf("\n");
|
|
|
system("cls");
|
|
|
printfchessmap();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void addstu(int *pq,int *pb)
|
|
|
{
|
|
|
FILE *fp=NULL;
|
|
|
int q,b,i,j;
|
|
|
system("cls");
|
|
|
for(i=0;i<N;i++)
|
|
|
for(j=0;j<N;j++)
|
|
|
map[i][j]=0;
|
|
|
if((fp = fopen("xsxx.txt","a+"))==NULL)
|
|
|
{
|
|
|
printf("Error\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
|
|
|
printf("玩家一得分:\n");
|
|
|
scanf("%d",&q);
|
|
|
*pq=q;
|
|
|
fprintf(fp,"%d\n",q);
|
|
|
fflush(stdin);
|
|
|
|
|
|
printf("玩家二得分:\n");
|
|
|
scanf("%d",&b);
|
|
|
*pb=b;
|
|
|
fprintf(fp,"%d\n",b);
|
|
|
fflush(stdin);
|
|
|
fclose(fp);
|
|
|
system("cls");
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
void SetPos(int x,int y)
|
|
|
{
|
|
|
|
|
|
COORD pos;
|
|
|
|
|
|
HANDLE handle;
|
|
|
|
|
|
pos.X=x;
|
|
|
|
|
|
pos.Y=y;
|
|
|
|
|
|
handle=GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
SetConsoleCursorPosition(handle,pos);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|