📄 five.c
字号:
struct stall
{int win; int lose;}
#include "stdio.h"
#include "string.h"
#include <graphics.h>
#include "conio.h"
int flag=0;
int aa,bb;
#define cur_right aa=(aa<19)?++aa:1 /*宏定义算法*/
#define cur_up bb=(bb>1)?--bb:19
#define cur_down bb=(bb<19)?++bb:1
#define cur_left aa=(aa>1)?--aa:19
void move_cursor ( long int key )
{
switch (key) {
case 328: cur_up; break; /* up */
case 336: cur_down;break; /*down */
case 331: cur_left;break; /*left */
case 333: cur_right;break; /*right*/
default: break; /*保持格式完整*/
}
}
int legal(int x,int y,char a[][20])
{if(x>0&&y>0&&x<20&&y<20&&a[x][y]=='.') return 1;
else return 0;
}
void Initial ( char a[][20] )
{int i,j;
for(j=0;j<20;j++) a[0][j]=j;
for(i=1;i<20;i++) a[i][0]=i;
for(i=1;i<20;i++)
for(j=1;j<20;j++) a[i][j]='.';
}
int judge(int x,int y,char a[][20])
{int i,j,num=0;
char c=a[x][y];
if( c=='.' ) return 0;
/*level direction*/
{for(j=y;j<20;j++)
if(a[x][j]==c) num++;
else break;
for(j=y-1;j>0;j--)
if(a[x][j]==c) num++;
else break;
if(num>=5) return 1;
}num=0;
/*perpendicular direction*/
{for(i=x;i<20;i++)
if(a[i][y]==c) num++;
else break;
for(i=x-1;i>0;i--)
if(a[i][y]==c) num++;
else break;
if(num>=5) return 1;
}num=0;
/*up-bevel direction*/
{for(i=x,j=y;i<20&&j>0;i++,j--)
if(a[i][j]==c) num++;
else break;
for(i=x-1,j=y+1;i>0&&j<20;i--,j++)
if(a[i][j]==c) num++;
else break;
if(num>=5) return 1;
}num=0;
/*down-bevel direction*/
{for(i=x,j=y;i<20&&j<20;i++,j++)
if(a[i][j]==c) num++;
else break;
for(i=x-1,j=y-1;i>0&&j>0;i--,j--)
if(a[i][j]==c) num++;
else break;
if(num>=5) return 1; return 0;
}
}
void result(struct stall s,char e[])
{printf("\nPlayer:%s",e);
printf("\nWIN:%d",s.win);
printf("\tLOSE:%d",s.lose);
}
void Initial_show ( char a[][20] )
{ window(67,2,78,21);
textbackground(3);
clrscr();
textcolor(128);
cprintf(" Copyright ");
cprintf("\nZhong Yun De");
window(1,2,64,21);
textbackground(7);
clrscr();
textcolor(WHITE);
for(aa=0;aa<20;aa++) /* */
for(bb=0;bb<20;bb++) {
gotoxy(1+aa*3,1+bb);
{if(aa*bb) cprintf("%3c",a[aa][bb]);
else cprintf("%3d",a[aa][bb]);}
}
}
void record ( int e,int f)/*e,f为输入坐标*/
{window(1,22,80,25);
textbackground(5);
clrscr();
gotoxy(1,22);
if(flag%2) cprintf("Player_B:(%d,%d)",e,f);
else cprintf("Player_A:(%d,%d)",e,f);
}
int main (void)
{ char chessbd[20][20];
long int chessbd_n=0;
char a[12],b[12];
struct stall c={0,0},d={0,0};/*记录输赢*/
Initial ( chessbd );
printf("Input the both names of the players:\nA:");
scanf("%s",a);
printf("\nB:");
scanf("%s",b);
window(1,1,80,25);
textbackground(1);
clrscr();
textcolor(10);
gotoxy(1,1);printf("A:%s\tB:%s",a,b);
Initial_show ( chessbd );
aa=bb=10; /* 预置初始值 */
for( ; ; ){ /*回车键,取值*/
window(1,2,64,21);
textbackground(7);
textcolor(RED);
gotoxy(1+aa*3,1+bb);
cprintf("%3c",chessbd[aa][bb]);
while( chessbd_n!=13 || legal(aa,bb,chessbd)==0 ){
if(0==chessbd_n) chessbd_n=getch()+256;
textcolor(WHITE);
gotoxy(1+aa*3,1+bb);
cprintf("%3c",chessbd[aa][bb]);
move_cursor(chessbd_n);
textcolor(RED);
gotoxy(1+aa*3,1+bb);
cprintf("%3c",chessbd[aa][bb]);
chessbd_n=getch();
if(chessbd_n==13&&legal(aa,bb,chessbd)==1) flag=(++flag)%2;
}
record(aa,bb);
if(flag==0) chessbd[aa][bb]='0';
else chessbd[aa][bb]='*';
if(judge(aa,bb,chessbd) )
{cprintf("%3c",chessbd[aa][bb]);
window(1,22,80,25);
textbackground(5);
clrscr();
if(flag==1) { c.win++;d.lose++;}
else {d.win++;c.lose++;}
printf("Game Over!!!");
break;
}
}result(c,a);result(d,b);
getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -