五子棋源码
#include<graphics.h>#include<stdio.h>
#include<conio.h>
#define MAX 20
#define LOCATE "E:\\TC20H"
#define FLASHCOUNTS 10
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x11b
#define ENTER 0x1c0d
void draw(void);
void drawstone(int,int,int);
void delay(void);
void flash(int,int,int);
int move(int);
int initxy(int*,int*);
void initjudge(void);
typedef struct{
int flag;
int color;
}stone;
stone judge[15][15]; /*定义二维数组,记录位置和颜色*/
main()
{
int a,b,i,x,y;
clrscr();
a=VGA;
b=VGAHI;
initgraph(&a,&b,LOCATE);
cleardevice();
draw();
initjudge();
while(1)
{
move(WHITE);
move(BLACK);
}
getch();
closegraph();
}
void draw() /*画棋盘*/
{
int i;
setfillstyle(1,YELLOW);
bar((640-MAX*15)/2,(480-MAX*15)/2,(640+MAX*15)/2,(480+MAX*15)/2);
setcolor(BLACK);
for(i=0;i<14;i++)
line(0,(480-MAX*15)/2+(i+1)*MAX,640,(480-MAX*15)/2+(i+1)*MAX);
for(i=0;i<14;i++)
line((640-MAX*15)/2+(i+1)*MAX,0,(640-MAX*15)/2+(i+1)*MAX,480);
setcolor(YELLOW);
rectangle((640-MAX*15)/2-2,(480-MAX*15)/2-2,(640+MAX*15)/2+2,(480+MAX*15)/2+2);
}
void drawstone(int x,int y,int color) /*画棋子,x,y为行列数*/
{
int X,Y; /*X,Y为实际坐标*/
X=(640-MAX*15)/2+y*MAX-MAX/2;
Y=(480-MAX*15)/2+x*MAX-MAX/2;
setfillstyle(1,color);
setcolor(color);
circle(X,Y,MAX/2-1);
floodfill(X,Y,color);
}
void drawstone2(int x,int y,int color) /*画棋子,但是有边*/
{
int X,Y;
drawstone(x,y,color);
X=(640-MAX*15)/2+y*MAX-MAX/2;
Y=(480-MAX*15)/2+x*MAX-MAX/2;
setcolor(GREEN);
circle(X,Y,MAX/2-1);
}
void initjudge(void)
{
int i,j;
for(i=0;i<14;i++)
for(j=0;j<14;j++)
{
judge[i][j].flag=0;
judge[i][j].color=1;
}
}
int initxy(int *x,int *y)
{
int i=0,j=0;
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(judge[i][j].flag==0)
{
(*x)=i+1;
(*y)=j+1;
return 1;
}
}
return 0;
}
int move(int color)
{
int key,x,y;
initxy(&x,&y);
drawstone2(x,y,color);
while(bioskey(1)==0)
{
key=bioskey(0);
switch(key)
{
case UP:
if(x!=1&&judge[x-2][y-1].flag!=1)
{
drawstone(x,y,YELLOW);
x--;
drawstone2(x,y,color);
}
break;
case DOWN:
if(x!=15&&judge[x][y-1].flag!=1)
{
drawstone(x,y,YELLOW);
x++;
drawstone2(x,y,color); }
break;
case LEFT:
if(y!=1&&judge[x-1][y-2].flag!=1)
{
drawstone(x,y,YELLOW);
y--;
drawstone2(x,y,color);}
break;
case RIGHT:
if(y!=15&&judge[x-1][y].flag!=1)
{
drawstone(x,y,YELLOW);
y++;
drawstone2(x,y,color);}
break;
case ENTER:
drawstone(x,y,color);
judge[x-1][y-1].flag=1;
judge[x-1][y-1].color=color;
return 1;
case ESC:exit(0);
default:break;
}
}
}