You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pjv4n53i6/五子棋课设 - 1.c

537 lines
7.5 KiB

6 years ago
#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(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
int playchess();
void printfchessmap(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int judge(int x, int y);
void bangzhu(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void gotoxy(int x, int y); //<2F><><EFBFBD><EFBFBD><EABAAF>
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(); //<2F><><EFBFBD><EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
lhdajy=0;
}
}
return 0;
}
void initgame()
{
char choice;
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ(Y/N)<29><><EFBFBD>߰<EFBFBD><DFB0><EFBFBD><EFBFBD><EFBFBD>Z<EFBFBD><5A>");
choice = getchar();
if (choice != 'y' && choice != 'Y'&& choice!='z'&& choice!='Z') //ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>&&,<2C><>Ҫд<D2AA><D0B4>||
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>÷<EFBFBD>:%d\n",dgnb);
printf("%3c",' ');
printf("<EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD><EFBFBD>÷<EFBFBD>:%d\n",jypl);
}
int playchess()
{
int x,y,i,j,sum=0,sum1=0;
int *p=&x,*q=&y;
if (0 == whoturn % 2)
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>");
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>");
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d<><64>ʤ\n",1 + whoturn % 2);
lhdajy=1;
return 1;
}
}
int judge(int x, int y)
{
int i, j;
int count;
int winflag = 1; //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E3B2BB><EFBFBD>ٴζ<D9B4>ȡ
int cur; //<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>
cur = map[x][y] == 1 ? 1 : 2;
printf("cur = %d", cur);
//ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
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;
//<2F><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
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;
//<2F><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD>ж<EFBFBD>
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;
//б<>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD>ж<EFBFBD>
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(" <20><>");
else if(i==5&&j==7)
printf(" <20><>");
else if(i==5&&j==8)
printf(" <20><>");
else if(i==5&&j==9)
printf(" <20><>");
else
printf("%3c",' ');
}
printf("\n");
}
}
int move1(int (*a)[N],int *px,int *py) //<2F><><EFBFBD>̲ٿ<CCB2>
{
char move1;
int hx,hy,i,n;
hx=9;
hy=9;
while(1)
{
move1=getch();
n=1;
if(move1=='d')//<2F><><EFBFBD><EFBFBD>
{
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')//<2F><>
{
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')//<2F><>
{
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')//<2F><>
{
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) //<2F><><EFBFBD>̲ٿ<CCB2>
{
char move2;
int hx,hy,i,n;
hx=9;
hy=9;
while(1)
{
move2=getch();
n=1;
if(move2==77)//<2F><><EFBFBD><EFBFBD>
{
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)//<2F><>
{
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)//<2F><>
{
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)//<2F><>
{
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>÷<EFBFBD>:\n");
scanf("%d",&q);
*pq=q;
fprintf(fp,"%d\n",q);
fflush(stdin);
printf("<EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD><EFBFBD>÷<EFBFBD>:\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);
}