#include "conio.h"
#include "graphics.h"
#include "dos.h"
#include "math.h"
#include "stdio.h"
#include "bios.h"
#include "time.h"
#define right 19712
#define left 19200
#define up 18432
#define down 20480
#define enter 7181
#define esc 283
#define n 12654
#define N 12622
#define w 4471
void initgr(void)
{
int i;
int x1=240,y1=180;
int gd=DETECT,gm=0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, " ");
/*-------------画棋盘*/
/*------------------------------------------------------*/
setcolor(5);
setfillstyle(1,LIGHTBLUE);
bar3d(1,1,639,479,0,0);
setcolor(6);
setfillstyle(1,14);
setlinestyle(0,0,3);
bar3d(232,172,368,308,0,0);
rectangle(235,175,365,305);
setlinestyle(0,0,1);
setcolor(0);
rectangle(240,180,360,300);
for(i=1;i<3;i++)
line(x1,y1+i*40,x1+120,y1+i*40);
for(i=1;i<3;i++)
line(x1+i*40,y1,x1+i*40,y1+120);
/*------------------------------------------------------*/
}
drawlight(int x,int y) /*---------------------------“画光标”----函数*/
{
setcolor(LIGHTRED);
line(x-12,y-12,x-1,y-12);line(x-12,y+12,x-1,y+12);
line(x+12,y-12,x+1,y-12);line(x+12,y+12,x+1,y+12);
line(x-12,y-1,x-12,y-12);line(x-12,y+1,x-12,y+12);
line(x+12,y-1,x+12,y-12);line(x+12,y+1,x+12,y+12);
}
movelight(int key,int *x,int *y)/*-----------------------------移光标函数*/
{
if(key==right)
{
drawlight(*x,*y);*x+=40;drawlight(*x,*y);
}
if(key==left)
{
drawlight(*x,*y);*x-=40;drawlight(*x,*y);
}
if(key==up)
{
drawlight(*x,*y);*y-=40;drawlight(*x,*y);
}
if(key==down)
{
drawlight(*x,*y);*y+=40;drawlight(*x,*y);
}
}
limitlight(int directkey,int *pl,int *sl)/*-------------------光标限制函数*/
{
int b0,pi;
if(directkey==right)
{
b0=1;*pl+=b0;
pi=2-abs(*pl);if(*pl==2)*pl=1;
return pi;
}
else if(directkey==left)
{
b0=-1;*pl+=b0;
pi=2-abs(*pl);if(*pl==-2)*pl=-1;
return pi;
}
else if(directkey==up)
{
b0=1;*sl+=b0;
pi=2-abs(*sl);if(*sl==2)*sl=1;
return pi;
}
else if(directkey==down)
{
b0=-1;*sl+=b0;
pi=2-abs(*sl);if(*sl==-2)*sl=-1;
return pi;
}
}
rw(int key,int *c)/*------------------------------记录下棋位置函数*/
{
if(key==right) *c+=1;
if(key==left) *c-=1;
if(key==up) *c-=3;
if(key==down) *c+=3;
}
struct remember/*-------------记录在下过黑棋位置wc和下过白棋个数wm的函数*/
{
int wm;
int wc;
}r[5]={
{0,0},
{0,0},
{0,0},
{0,0},
{0,0}
};
struct rm /*--------------------------------棋盘上每一个点的信息*/
{
int num;
int l1;
int l2;
int l3;
int x;
int y;
int c; /*--------------------------此点的下棋信息:无-0;黑棋-1;白棋-4*/
}m[9]={
{1,1,2,8,260,200,0},
{2,0,2,6,300,200,0},
{3,2,3,7,340,200,0},
{4,0,1,5,260,240,0},
{5,0,0,0,300,240,0},
{6,0,3,5,340,240,0},
{7,1,4,7,260,280,0},
{8,0,4,6,300,280,0},
{9,3,4,8,340,280,0}
};
drawpoint(int *i,struct rm m) /*---------------------画白棋子函数*/
{
int x,y;
x=m.x;
y=m.y;
setcolor(WHITE);
setfillstyle(1,WHITE);
pieslice(x,y,0,360,11);
*i=m.num-1;
}
drawpointc(int *wr1,struct rm m) /*--------------------------画黑棋子函数*/
{
int x,y;
if(m.c==0)
{
x=m.x;
y=m.y;
setcolor(BLACK);
setfillstyle(1,BLACK);
pieslice(x,y,0,360,11);
m.c=1;
}
else *wr1=1;
}
rand2468()
{
int rands;
time_t t;
srand((unsigned) time(&t));
rands=rand()%4;
if(rands==0)return 2;
if(rands==1)return 4;
if(rands==2)return 6;
if(rands==3)return 8;
}
rand1357()
{
int randj;
time_t t;
srand((unsigned) time(&t));
randj=rand()%4;
if(randj==0)return 1;
if(randj==1)return 3;
if(randj==2)return 5;
if(randj==3)return 7;
}
main()/*-----------------------------------------------------main函数---------*/
{
int key,i,rand;
int wr=0; /*----------限制在一点重复下棋*/
int rc=0,rm=0; /*----------记录下棋位置个数*/
int ow=5; /*----------记录最后下的位置*/
int co=1,ad=1,add=1,find; /*----------思考部分的跳转*/
int mx,my; /*----------坐标变化*/
int pl=0,sl=0; /*----------限制光标*/
goto jump;
replay:cleardevice();
jump: initgr(); /*--------------------调用画棋盘函数*/
mx=300,my=240; setwritemode(1);
drawlight(mx,my);
goto jumpi;
start:
r[rm].wm=1; /*-----------------记录下过的白棋个数*/
if(r[3].wm==1)goto ask;
rm+=1;
jumpi:
if(m[0].c+m[3].c+m[6].c==12||m[0].c+m[1].c+m[2].c==12|| /*----检查是否赢了*/
m[2].c+m[5].c+m[8].c==12||m[6].c+m[7].c+m[8].c==12||
m[3].c+m[4].c+m[5].c==12||m[1].c+m[4].c+m[7].c==12||
m[2].c+m[4].c+m[6].c==12||m[0].c+m[4].c+m[8].c==12)
{printf("\n\n\n\n\n I win!");
printf(" ");goto ask;}
else key=bioskey(0); /*----------------------------------------接受控制部分*/
while(key!=esc)
{
if(limitlight(key,&pl,&sl)==0) goto restart;/*------光标限制*/
rw(key,&ow); /*------------------------------位置记录*/
movelight(key,&mx,&my); /*-------------------移动光标*/
if(key==enter)
{
drawpointc(&wr,m[ow-1]);
if(wr!=1) /*---------------------------wr=1为此处已下过棋*/
{
m[ow-1].c=1; /*----------------------------记录下黑棋的位置*/
r[rc].wc=ow;
rc+=1;
goto think;
}
else{wr=0;goto start;}
}
restart:key=bioskey(0);
}
goto end;
/*--------------------下棋子部分*/
drawm:switch(find) /*------------攻击下棋*/
{
case 1:if(m[0].c==0){drawpoint(&i,m[0]);m[0].c=4;add=1;ad=1;goto start;}
if(m[3].c==0){drawpoint(&i,m[3]);m[3].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[6]);m[6].c=4;add=1;ad=1;goto start;}
case 2:if(m[0].c==0){drawpoint(&i,m[0]);m[0].c=4;add=1;ad=1;goto start;}
if(m[1].c==0){drawpoint(&i,m[1]);m[1].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[2]);m[2].c=4;add=1;ad=1;goto start;}
case 3:if(m[2].c==0){drawpoint(&i,m[2]);m[2].c=4;add=1;ad=1;goto start;}
if(m[5].c==0){drawpoint(&i,m[5]);m[5].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[8]);m[8].c=4;add=1;ad=1;goto start;}
case 4:if(m[6].c==0){drawpoint(&i,m[6]);m[6].c=4;add=1;ad=1;goto start;}
if(m[7].c==0){drawpoint(&i,m[7]);m[7].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[8]);m[8].c=4;add=1;ad=1;goto start;}
case 5:if(m[3].c==0){drawpoint(&i,m[3]);m[3].c=4;add=1;ad=1;goto start;}
if(m[4].c==0){drawpoint(&i,m[4]);m[4].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[5]);m[5].c=4;add=1;ad=1;goto start;}
case 6:if(m[1].c==0){drawpoint(&i,m[1]);m[1].c=4;add=1;ad=1;goto start;}
if(m[4].c==0){drawpoint(&i,m[4]);m[4].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[7]);m[7].c=4;add=1;ad=1;goto start;}
case 7:if(m[2].c==0){drawpoint(&i,m[2]);m[2].c=4;add=1;ad=1;goto start;}
if(m[4].c==0){drawpoint(&i,m[4]);m[4].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[6]);m[6].c=4;add=1;ad=1;goto start;}
case 8:if(m[0].c==0){drawpoint(&i,m[0]);m[0].c=4;add=1;ad=1;goto start;}
if(m[4].c==0){drawpoint(&i,m[4]);m[4].c=4;add=1;ad=1;goto start;}
{drawpoint(&i,m[8]);m[8].c=4;add=1;ad=1;goto start;}
}
drawc:switch(find) /*------------防御下棋*/
{
case 1:if(m[0].c!=1){drawpoint(&i,m[0]);m[0].c=4;add=1;ad=1;goto start;}
if(m[3].c!=1){drawp