commit 47204a515b4326283b586ee209c7180c5f12399c Author: unknown <2801978061@qq.com> Date: Fri Jan 11 21:22:51 2019 +0800 lhdajy diff --git a/五子棋课设 - 1.c b/五子棋课设 - 1.c new file mode 100644 index 0000000..c21c34b --- /dev/null +++ b/五子棋课设 - 1.c @@ -0,0 +1,536 @@ +#include +#include +#include +#include +#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